0

Say the size of my JPanel is 200 x 200. Is the following pseudo code possible?:

for(int x=0; x< 200;x++){
for(int y=0;y< 200;y++){
    if(x%2==0 && y%2==0){
         panel.setColor(x-Coord, y-Coord, Color a);
            }else{
          panel.setColor(x-Coord, y-Coord, Color b);
              }
     }
}

How can I paint specific pixels of a JPanel? As you can see I used setColor() in my pseudo code because I didn't find any predefined method in Java for that. How do I do that?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
John Doe
  • 65
  • 9
  • Yeah, override `paintComponent()` of `JPanel` method instead –  Jan 18 '17 at 14:02
  • What is the purpose for wanting to set the color of specific pixels? There are various ways to achieve that, some better suited to one purpose than another. – Andrew Thompson Jan 18 '17 at 14:11
  • @Arvind Do you have any example how to do it? So how does paintComponent() paint pixels gradually? – John Doe Jan 18 '17 at 14:11
  • @Berger but I dont want to change the colors of an image but of a JPanel – John Doe Jan 18 '17 at 14:17
  • @JohnDoe : in the linked answer, the generated image is drawn on the `Graphics` of the `JPanel` . At least if your panel won't change size, it will be far more efficient to precompute the image, and just paint it in `paintComponent`, instead of recomputing the whole thing on each repaint . – Arnaud Jan 18 '17 at 14:21
  • 1
    @Berger Ah okay that helped! Thanks :) – John Doe Jan 18 '17 at 14:24

0 Answers0