hi there,
im trying to read the color of a pixel in a jpanel. the application is
like a paint program; the user can draw using the mouse anywhere on a
jpanel. what i'd like to do is to read the color value at each point of
the panel to determine which places have been painted. is there any
direct way by using jpanels? or even awt panels?
thanks in advance
Ryan Stewart - 17 Feb 2004 03:55 GMT
> hi there,
> im trying to read the color of a pixel in a jpanel. the application is
[quoted text clipped - 4 lines]
>
> thanks in advance
If you're doing your own double buffering, why not just use your back
buffer? If you're not, then... not that I know of. You could kind of halfway
double buffer: create a BufferedImage and draw to it whenever you draw to
the JPanel. Of course, you're doing twice the work then. Might as well fully
implement your own double buffering and cut out the panel painting.
ak - 17 Feb 2004 08:15 GMT
> im trying to read the color of a pixel in a jpanel. the application is
> like a paint program; the user can draw using the mouse anywhere on a
> jpanel. what i'd like to do is to read the color value at each point of
> the panel to determine which places have been painted. is there any
> direct way by using jpanels? or even awt panels?
you _must_ to draw first to BufferedImage, this image you draw then to
panel.
without using such image the content gets lost for example after you drag
another window over you panel.
to determine pixel color under mouse pointer you should add
MouseMotionListener to your (j)panel:
at every mouseMoved event you get first the mouse coords and then get pixel
at this coords in your BufferedImage.
BufferedImage bi;
JPanel panel;
panel.addMouseMotionListener(new MouseMotionAdapter() {
public void mouseMoved(MouseEvent e) {
Point p = e.getPoint();
int rgb = bi.getRGB(p.x, p.y);
}
}
--
____________
http://reader.imagero.com the best java image reader.