parrot15
parrot15

Reputation: 329

How to repaint a JPanel on button click?

I am new to swing and I am currently trying to make a drawing program. This is what I have so far.

Here is the main class:

import java.util.*;
import static java.lang.System.*;
import java.awt.*;
import javax.swing.*;
public class DrawingBoard {
    static JPanel buttonPanel;
    static JPanel drawingPanel;
    public static void main(String[] args) {
        JFrame window = new JFrame();
        window.setSize(500, 500);
        window.setTitle("Drawing Board");
        window.getContentPane().setBackground(Color.black);
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        window.setLayout(new FlowLayout());

        buttonPanel = new JPanel();
        buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.Y_AXIS));
        buttonPanel.setPreferredSize(new Dimension(80, 500));
        buttonPanel.setBackground(Color.lightGray);
        DrawingComponent drawingComp = new DrawingComponent();
        drawingComp.setButtonPanel();
        window.getContentPane().add(buttonPanel);

        drawingPanel = new JPanel();
        drawingPanel.setPreferredSize(new Dimension(420, 500));
        drawingPanel.setBackground(Color.white);
        drawingPanel.add(drawingComp);
        window.getContentPane().add(drawingPanel);

        window.pack();
        window.setVisible(true);
    }
}

Here is the drawing class:

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import static java.lang.System.*;
public class DrawingComponent extends JComponent implements ActionListener {
    private static final long serialVersionUID = 1L;
    public void paint(Graphics g) {
        super.paintComponents(g);
        g.setColor(Color.red);
        g.fillRect(50, 50, 150, 150);
    }
    public  void setButtonPanel() {
        JLabel colors = new JLabel("Colors", SwingConstants.CENTER);
        DrawingBoard.buttonPanel.add(colors);

        JButton red = new JButton("Red");
        DrawingBoard.buttonPanel.add(red);
        red.addActionListener(this);

        JButton blue = new JButton("Blue");
        DrawingBoard.buttonPanel.add(blue);
        blue.addActionListener(this);

        JButton green = new JButton("Green");
        DrawingBoard.buttonPanel.add(green);
        green.addActionListener(this);

        JButton yellow = new JButton("Yellow");
        DrawingBoard.buttonPanel.add(yellow);
        yellow.addActionListener(this);

        JButton orange = new JButton("Orange");
        DrawingBoard.buttonPanel.add(orange);
        orange.addActionListener(this);

        JButton pink = new JButton("Pink");
        DrawingBoard.buttonPanel.add(pink);
        pink.addActionListener(this);

        JButton purple = new JButton("Purple");
        DrawingBoard.buttonPanel.add(purple);
        purple.addActionListener(this);

        JButton black = new JButton("Black");
        DrawingBoard.buttonPanel.add(black);
        black.addActionListener(this);

        JLabel tools = new JLabel("Tools", SwingConstants.CENTER);
        DrawingBoard.buttonPanel.add(tools);

        JButton erase = new JButton("Erase");
        DrawingBoard.buttonPanel.add(erase);
        erase.addActionListener(this);

        JButton drawLine = new JButton("Lines");
        DrawingBoard.buttonPanel.add(drawLine);
        drawLine.addActionListener(this);

        JButton clear = new JButton("Clear");
        DrawingBoard.buttonPanel.add(clear);
        clear.addActionListener(this);
    }
    public void actionPerformed(ActionEvent e) {
        if(e.getActionCommand().equals("Red")) {
            out.println("red");
            repaint();
        }
        else if(e.getActionCommand().equals("Blue")) {
            out.println("blue");
            repaint();
        }
        else if(e.getActionCommand().equals("Green")) {
            out.println("green");
            repaint();
        }
        else if(e.getActionCommand().equals("Yellow")) {
            out.println("yellow");
            repaint();
        }
        else if(e.getActionCommand().equals("Orange")) {
            out.println("orange");
            repaint();
        }
        else if(e.getActionCommand().equals("Pink")) {
            out.println("pink");
            repaint();
        }
        else if(e.getActionCommand().equals("Purple")) {
            out.println("purple");
            repaint();
        }
        else if(e.getActionCommand().equals("Black")) {
            out.println("black");
            repaint();
        }
        else if(e.getActionCommand().equals("Erase")) {
            out.println("erase");
            repaint();
        }
        else if(e.getActionCommand().equals("Lines")) {
            out.println("lines");
            repaint();
        }
        else if(e.getActionCommand().equals("Clear")) {
            out.println("clear");
            repaint();
        }
    }
}

Here is the link a picture of the output. https://i.sstatic.net/GEwga.png

For now, I just want the drawing board to display some kind of graphic at the click of a button. Everything else is fine, but when I click any button, no graphics are displayed in the drawingPanel (the white area) like its supposed to. The println statements for the buttons still get executed, but the repaint()'s are just straight up not working. How do I make it so that when I click a button that the paintComponent method gets called and a graphic is displayed on the drawingPanel which is the white area on the window? I have been stuck on this problem for quite a while so help would greatly be appreciated.

Upvotes: 3

Views: 2162

Answers (1)

MadProgrammer
MadProgrammer

Reputation: 347334

Change drawingPanel = new JPanel(); to `drawingPanel = new JPanel(new BorderLayout());

DrawingComponent has a default preferred size of 0x0, so when you add it to the JPanel (which is using a FlowLayout by default), it's getting sized to 0x0, so you won' see anything painted

Then change...

public void paint(Graphics g) {
    super.paintComponents(g);
    g.setColor(Color.red);
    g.fillRect(50, 50, 150, 150);
}

to...

@Override
protected void paintComponent(Graphics g) {
    System.out.println("Hello");
    super.paintComponent(g); 
    g.setColor(Color.red);
    g.fillRect(10, 10, 150, 150);
}

There's a bunch of other stuff about your code which scares me, but I don't have time to address it all

Upvotes: 2

Related Questions