droftrah
droftrah

Reputation: 3

paint method causing other components not to show up

I have a simple GUI program I'm trying to get to work. When the user presses the bottom button I'm trying to get some shapes to paint. When I get rid of the if(buttonClicked) in paint() everything shows up fine but paint() seems to be auto-executed and the shapes appear without any button click. When I add surround the paint() body with if(buttonClicked), regardless of how the ButtonHandler class handles it, the rest of my components do not even show up in the frame. I have no idea why this is happening. Test the code with and without the if logic in paint() and you will see what's going on.

 public class GUI extends JFrame {

      //declare components
     Container container;
     JPanel centerPanel, northPanel, southPanel, eastPanel, westPanel, mouseClickPanel;
     JLabel topLabel;
     JTextArea textArea;
     JButton buttonA, buttonB, drawButton;
     boolean buttonClicked;

     public GUI(String title) {

         super(title); // invoke JFrame Constructor

         container = getContentPane();
         container.setLayout(new BorderLayout());

         centerPanel = new JPanel();
         centerPanel.setBackground(Color.CYAN);
         northPanel = new JPanel();
         northPanel.setBackground(Color.GREEN);
         southPanel = new JPanel();
         southPanel.setBackground(Color.MAGENTA);
         eastPanel = new JPanel();
         eastPanel.setBackground(Color.ORANGE);
         westPanel = new JPanel();
         westPanel.setBackground(Color.YELLOW);
         mouseClickPanel = new JPanel();
         mouseClickPanel.setBackground(Color.GRAY);
         mouseClickPanel.setPreferredSize(new Dimension(400, 400));

         topLabel = new JLabel("Press either button to make something happen");
         topLabel.setFont(new Font("Calibri", Font.PLAIN, 16));
         topLabel.setHorizontalAlignment(JLabel.CENTER);

         textArea = new JTextArea(3, 20);
         textArea.setEditable(false);
         buttonA = new JButton("Press Here");
         buttonB = new JButton("Press Here");
         drawButton = new JButton("Press here");

         container.add(centerPanel, BorderLayout.CENTER);
         container.add(northPanel, BorderLayout.NORTH);
         container.add(southPanel, BorderLayout.SOUTH);
         container.add(eastPanel, BorderLayout.EAST);
         container.add(westPanel, BorderLayout.WEST);

         northPanel.add(topLabel, BorderLayout.NORTH);
         centerPanel.add(buttonA);
         centerPanel.add(textArea);
         centerPanel.add(buttonB);
         centerPanel.add(mouseClickPanel);
         centerPanel.add(drawButton);

         buttonClicked = false;
         ButtonHandler buttonHandler = new ButtonHandler(buttonA, drawButton, textArea, buttonClicked);



         buttonA.addActionListener(buttonHandler); // add actionListeners to buttonA and B
         buttonB.addActionListener(buttonHandler);
         drawButton.addActionListener(buttonHandler);

         setSize(525, 600);
         setVisible(true);
     }



     public void paint(Graphics g) {


        if (buttonClicked) {
            super.paint(g);

            g.setColor(Color.RED);
            g.fillOval(150, 150, 50, 50);
            g.draw3DRect(200, 200, 50, 50, true);
        }
     }
 }

Handler class:

 public class ButtonHandler extends JFrame implements ActionListener {

     private JButton buttonA, drawButton;
     private JTextArea textArea;
     boolean buttonClicked;

 public ButtonHandler(JButton buttonA, JButton drawButton, JTextArea textArea, boolean buttonClicked) {
    this.buttonA = buttonA;
    this.textArea = textArea;
    this.drawButton = drawButton;
    this.buttonClicked = buttonClicked;

}

@Override
public void actionPerformed(ActionEvent e) {

    if (e.getSource() == buttonA) {
        textArea.setText(" <- You pressed left button");
    }
    else if (e.getSource() == drawButton) {

        textArea.setText("You pressed button to draw rectangle");
        buttonClicked = true;
        repaint();
    }
    else {
        textArea.setText("You pressed right button ->");
    }
}
}

Upvotes: 0

Views: 128

Answers (1)

user489041
user489041

Reputation: 28294

Take super.paint(g); out of the if statement. Have it as the first line. Otherwise, no painting at all (including the JPanel internals such as background) will happen unless the button is clicked.

Upvotes: 2

Related Questions