Search code examples
javaswinggraphicsrepaintpaintcomponent

Keeping draw graphics - removing super.paintComponent


I have a class named Foo that extends a class named Bar that extends JPanel and implements ActionListener. When I select Circle and click the draw button, I draw a circle, and when I press rectangle and click draw, it erases the previous shape and draws a rectangle.

However, I want to keep all the shapes on the JPanel until I choose to click the erase button. So I removed the super.paintComponent(g) and it works, but it also causes buttons of class Bar to reappear in a glitchy manner. How can I stop the buttons from painting again? I was thinking not to extend Bar and make Foo extend JPanel.

  public class Bar extends JPanel implements ActionListener
    {
    public void actionPerformed(ActionEvent e)
    {

        if (e.getActionCommand() == "Draw")
        {
            this.requestDraw = true;
            repaint();
        }
            if (e.getActionCommand() == "Circle")
            {
                requestRectangle = false;
                requestTriangle = false;
                requestCircle = true;
            }
            if (e.getActionCommand() == "Rectangle")
            {
                requestCircle = false;
                requestTriangle = false;
                requestRectangle = true;
            }
            if (e.getActionCommand() == "Right Triangle")
            {
                requestCircle = false;
                requestRectangle = false;
                requestTriangle = true;
            }
    }


    public class Foo extends Bar
    {    
        @Override
        public void paintComponent(Graphics g)
        {
            //super.paintComponent(g);
            if(RequestDraw())
            {

                if(RequestCircle())
                    circle.draw(g);
                if(RequestRectangle())
                    rectangle.draw(g);
                if(RequestTriangle())
                    rightTriangle.draw(g);



            }

            if(!RequestDraw())
            {                    


                g.setColor(getBackground());
                g.fillRect(0,0,getWidth(), getHeight());
            }        
        }
    }
}

enter image description here


Solution

  • Suggestions:

    • Don't remove super.paintComponent(g) as it has a necessary important role to play.
    • Instead why not draw to a BufferedImage and then draw that BufferedImage in the paintComponent(...) method override.
    • Then if you want to erase drawn images, simply create a new BufferedImage, or draw over it.

    As an aside, don't compare Strings using ==. Use the equals(...) or the equalsIgnoreCase(...) method instead. Understand that == checks if the two objects are the same which is not what you're interested in. The methods on the other hand check if the two Strings have the same characters in the same order, and that's what matters here. So instead of

    if (fu == "bar") {
      // do something
    }
    

    do,

    if ("bar".equals(fu)) {
      // do something
    }
    

    or,

    if ("bar".equalsIgnoreCase(fu)) {
      // do something
    }