Search code examples
javaswinggraphicsjpanelgraphics2d

Updating graphics in a jpanel


I am struggling with refreshing the graphics in a jpanel.

I tried to make the JPanel extending class "ShadeSelectPane" update it's Graphics object everytime I use a JColorChooser to choose a new colour, stored in the BaseColor object, but it doesn't. Can one of you tell me why? I am calling repaint after all. Thank you in advance. Their are two files in my code, Main.java and CompressedColor.java. Main.java extends JFrame and just serves as a entry point and storage for most of the code. CompressedColor stores a colour and some more shades of it in rgb565 and as a Color Object. I stripped the code of all the initialisation code for Swing components, which resides in Mains constructor. When ran, the code draws all the shades of the initial Color CompressedColor is set to, but it doesn't update the ShadeSelectPane when changing the BaseColor Object.

This is my Main.java file

 public class Main extends JFrame implements ActionListener 

 { 

     CompressedColor BaseColor; 

     JMenuBar bar; 

     JMenu file; 

     JMenu edit; 

     JMenuItem save; 

     JMenuItem exportdata; 

     JMenuItem exportfunction; 

     JMenuItem bgcolor; 

     ImagePane canvas; 

     ShadeSelectPane shadeselector; 

  

     
     // main-Methode 

     public static void main(String[] args) 

     {                                                 

         Main frame = new Main(); 

         frame.setVisible(true);    

  

     } 

     public void actionPerformed (ActionEvent ae){ 

  

         if(ae.getSource() == this.bgcolor){ 

             BaseColor = new CompressedColor(JColorChooser.showDialog(null, "Farbauswahl", null)); 

             BaseColor.updateShades(); 

             System.out.println(Integer.toHexString(BaseColor.rgb565)); 

             shadeselector.repaint(); 

         } 

         else if(ae.getSource() == this.save){ 

             System.out.println("Save was pressed!"); 

         } 

         else if(ae.getSource() == this.exportdata){ 

             System.out.println("Export Data was pressed!"); 

         } 

         else if(ae.getSource() == this.exportfunction){ 

             System.out.println("Export Function was pressed"); 

         } 

     } 

 } 

 class ImagePane extends JPanel { 

  

     @Override 

     public void paintComponent(Graphics g) { 

         super.paintComponent(g); 

         g.drawString("BLAH", 20, 20); 

         g.drawRect(200, 200, 200, 200); 

     } 

 } 

 class ShadeSelectPane extends JPanel { 

     CompressedColor BaseColor; 

     ShadeSelectPane(CompressedColor baseColor){ 

         super(); 

         BaseColor = baseColor; 

     } 

     @Override 

     public void paintComponent(Graphics g) { 

         super.paintComponent(g); 

         for (int i = 0; i < 8; i++) { 

             g.setColor(BaseColor.shades[i].color); 

            g.fillRect(0, i*(this.getHeight()/8), this.getWidth(), i*(this.getHeight()/8)+(this.getHeight()/8));  

         } 

  

  

     } 

 } 

 This is CompressedColor.java 

  

 import java.awt.Color; 

  

 public class CompressedColor { 

     Color color; 

     short rgb565; 

     CompressedColor[] shades; 

     void updateShades(){ 

         for (int i = 0; i < shades.length; i++) { 

             shades[i]  = new CompressedColor(new Color((color.getRed()-(i<<3))&0xFF, (color.getGreen()-(i<<3))&0xff, (color.getBlue()-(i<<3))&0xff)); 

         } 

     } 

     void setColor(Color clr){ 

         color = clr; 

         rgb565=(short)((((clr.getRed() & 0b11111000) << 8) | ((clr.getGreen() & 0b11111100) << 3) | (clr.getBlue() >> 3))&0xFFFF); 

  

     } 

     CompressedColor(short clr){ 

         shades = new CompressedColor[8]; 

         rgb565 = clr; 

         color= new Color((clr>>8)&0xF8, (clr>>3)&0xFC, (clr<<3)&0xF8); 

     } 

     CompressedColor(Color clr){ 

         shades = new CompressedColor[8]; 

         color = clr; 

         rgb565=(short)((((clr.getRed() & 0b11111000) << 8) | ((clr.getGreen() & 0b11111100) << 3) | (clr.getBlue() >> 3))&0xFFFF); 

     } 

 }```

Solution

  • Figured it out, I was creating a new CompressedColor instead of setting BaseColors Color to a new Color, which made BaseColor point to a different object locally, then globally. Repainting the JPanel just repainted it with the old, global value for BaseColor. I just didn't have a good overview of my pointers.