• Post Reply Bookmark Topic Watch Topic
  • New Topic

get pixels of a jPanel

 
samira samadi
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi.
I have a jPanel and I want to change colors of it's pixels.
how can I get one pixel of it?

thnaks.
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you performing custom painting on that JPanel? If not, then the coloring is determined by the JPanel's background color, and the colors of whatever other GUI elements have been placed on that JPanel.

If, on the other hand, you *are* performing custom painting, then you can just paint in whichever color you like in the paintComponent method.
 
samira samadi
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes,I am doing painting on it,i wrote the pen and eraser,and now I want to write brush for it,for this reason,I need to know the color of the pixel that mouse is clicked,if the color is the same as color of it's neighbors then I change their color....
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'd advise to keep track of all pixels you paint in an array or some other data structure, so that you can repaint it if necessary - otherwise all custom painting is lost if the window gets obscured by some other window or application (at which point paintComponent is called, and is supposed to restore all window contents).
 
Rob Camick
Ranch Hand
Posts: 2699
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Custom Painting Approaches

Try the Draw on Image approach. You can then use the getRGB(...) method of the BufferedImage to determine which pixel you clicked.
 
samira samadi
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for your replies.
you mean I should convert my jPanel to BufferedImage?how?
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
samira samadi wrote:how?

That's what the article explains, and what the accompanying source code demonstrates, isn't it?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!