Search code examples
javaswinguser-interfacejpanelgraphic

Change the color of a graphic not drawn in a paint method


I have a JPanel that I want circles in to appear as I left click and for them to disappear when I right click. I can make them appear on screen by drawing ellipses by getting the source from the mouse event click. However when I right click - to make them disappear I am just coloring the circle with the background colour of the JPanel and calling repaint(). This I know is wrong as the initial object wasn't drawn using the paint method. Is there a way of doing this?

Thanks


Solution

  • In the panel add List<Shape>. Override paintComponent(Graphics g). Call super and then iterate through the list painting each Shape.

    On click add a new Ellipse to the shapes list and call repaint();