can you tell me how to correctly update the color of already drawn shapes in this component, thank you in advance.
Not really so tough:
- Declare a private Color field in your class context.
- Declare a public
setShapeColor(Color color)
to set the color to the component - invoke
repaint()
to reflect the color changes And as a warning: don't forget to call
super.paintComponent(g);
inside thepaitnComponent(Graphics)
function, which you haven't done.class MyComponent extends JPanel { private Color shapeColor = Color.RED; public void setShapeColor(Color color) { this.shapeColor = color; } @Override public void paintComponent(Graphics g){ super.paintComponent(g); g.setColor(shapeColor); g.drawString("this is text one", 10, 10); //here I draw some shapes based on the //vector size and integers } } }
Though as OOP principle, you should actually declare a MyShape
class with Color
attribute and before drawing use the setter method as the example to set the color to the shape.