Java--bufferedimage Operation Example

Source: Internet
Author: User

Change image ColorModel Object index by mouse position to realize automatic image pixel change

BufferedImageThe composition of the image data ColorModel Raster . Rasterthe SampleModel number and type of band must match the ColorModel required quantity and type to represent its color and alpha component. The BufferedImage upper-left corner coordinates for all objects are (0, 0). Therefore, anything used to construct BufferedImage Raster must satisfy: minx=0 and miny=0.

This class relies on Raster data acquisition methods, data-set methods, and ColorModel color-characterization methods.

Package chapter2;

Import javax.swing.*;
Import java.awt.*;
Import java.awt.event.MouseEvent;
Import Java.awt.event.MouseMotionListener;
Import java.awt.image.*;

/**
* Created by LENOVO on 18-1-27.
*/
public class Bufferedimagedemo extends JPanel implements mousemotionlistener{
Private BufferedImage image = null;
private int width = 350;
private int height = 350;
Public Bufferedimagedemo () {
Image = CreateImage ();
Addmousemotionlistener (this);
}

@Override
public void mousedragged (MouseEvent e) {

}

@Override
public void mousemoved (MouseEvent e) {
//Create new picture
image = New BufferedImage (Createcolormodel (e.ge TX ()), Image.getraster (), false,null);
Repaint ();
}
public void Paintcomponent (Graphics g) {
Graphics2D g2d = (graphics2d) g.create ();
if (image = null) {
G2d.drawimage (image,2,2,width,height,null);
}
}
//color feature get
private static ColorModel Createcolormodel (int n) {
byte[] r = new BYTE[16];
Byte[] G = new BYTE[16];
Byte[] b = new BYTE[16];
for (int i=0;i<r.length;i++) {
R[i] = (byte) n;
G[i] = (byte) n;
B[i] = (byte) n;
}
return new IndexColorModel (4,16,R,G,B);
}

Private BufferedImage CreateImage () {
byte[] pixels = new Byte[width*height];
/*
* public databufferbyte (by Te[] DataArray, int size, int offset)
Constructs a byte-based databuffer with a single storage unit using the specified array, size, and offset. DataArray
must have an offset + size element at least. The accessor for this databuffer can only use offset to offset + size-1 elements.
* */
DataBuffer databuffer = new DataBufferByte (pixels,width*height,0);
//samplemodel is abstract class, Compressed Sample data: Singlepixelpackedsamplemodel Implementation class
//package multiple samples into an element
/*
* public Singlepixelpackedsamplemodel (int Datatype,int W, int h,int[] bitmasks)
* DataType-stores the data type used by the sample.
W-the width, in pixels, of the image data region described.
H-the height, in pixels, of the image data region described.
Bitmasks-Bitmask of all band.
* */
SampleModel SampleModel = new Singlepixelpackedsamplemodel (databuffer.type_byte,width,height,new int[]{( byte) 0xf});
//null represents the position (0,0)
WritableRaster raster = Raster.createwritableraster (samplemodel,databuffer,null);
return new BufferedImage (Createcolormodel (0), raster,false,null);
}

public static void Main (String args[]) {
JFrame JF = new JFrame ("BufferedImage Demo");
Jf.setdefaultcloseoperation (Jframe.exit_on_close);
Jf.getcontentpane (). setlayout (New BorderLayout ());
Jf.getcontentpane (). Add (New Bufferedimagedemo (), borderlayout.center);
Jf.setpreferredsize (New Dimension (380,380));
Jf.pack ();
Jf.setvisible (TRUE);
}
}

Java--bufferedimage Operation Example

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.