get the pixel of a panel

hi..i have a canvas which is a JPanel of white background. this canvas can be drawn. I want to get every pixel of the canvas. how do i get it? This is a JPanel and not an image. can i get the pixel?

[205 byte] By [don.juan9a] at [2007-11-27 10:58:52]
# 1

You can do this by overriding the JPanel component and creating a custom class that tracks its pixel contents.

One way of doing this is to override paint(), to draw to a BufferedImage instead of the normal Graphics context. Perhaps something like:

private int lastWidth,lastHeight;

private BufferedImage buffImage=null;

private Graphics buffGraphics=null;

public void paint(Graphics g) {

if (buffImage==null || lastWidth!=getWidth() || lastHeight!=getHeight()) {

if (buffGraphics!=null)

buffGraphics.dispose();

lastWidth=getWidth();

lastHeight=getHeight();

Graphics2D g2=(Graphics2D)g;

buffImage=g2.getDeviceConfiguration().createCompatibleImage(lastWidth,lastHeight)

buffGraphics=buffImage.getGraphics();

}

super.paint(buffGraphics);

g.drawImage(buffImage,0,0,null);

}

So with this code, one would query buffImage.getRGB(x,y) inside another method of the class to get the color of any pixel inside that component, including subcomponents.

The code above won't work in all cases, by the way... particularly in cases where the component is not set up as opaque. One way to resolve this would be to clear all the pixels in buffImage before calling the super.paint() method.

markt1964a at 2007-7-29 12:19:07 > top of Java-index,Security,Cryptography...
# 2

ahhh...thank you very much..

really appreciate it

don.juan9a at 2007-7-29 12:19:07 > top of Java-index,Security,Cryptography...