Search code examples
javaswingjbuttononclicklistenerpaintcomponent

oval not changing color when clicking a JButton - Java Swing


I got 3 JPanel's inside a JFrame. Two for JButton's and the other one to a drawing canvas. I generate a random color for each JButton,painting an oval in the middle of the JPanel and in each click in the JButton's i change the color of the oval. The problem is the color is not changed.

CirclePanel.java:

public class CirclePanel extends JPanel
{
    private static final long serialVersionUID = 1L;

    private Color color;

    public Color getColor()
    {
        return this.color;
    }

    public void setColor(Color color)
    {
        this.color = color;
        repaint();
    }

    public CirclePanel()
    {

    }

    @Override
    protected void paintComponent(Graphics g) 
    {
        super.paintComponent(g);

        final int OVAL_WIDTH = this.getWidth() / 2;
        final int OVAL_HEIGHT = this.getHeight() / 2;
        final int xPosition =  OVAL_WIDTH / 2;
        final int yPosition =  OVAL_HEIGHT / 2;

        g.setColor(this.color);
        g.fillOval(xPosition,yPosition,OVAL_WIDTH,OVAL_WIDTH);
    }
}

RightPanel.java and LeftPanel.java:

public class RightPanel extends JPanel implements ActionListener
{
    private static final long serialVersionUID = 1L;

    private static final int BUTTONS = 6;

    private CirclePanel circlePanel;

    public RightPanel(CirclePanel circlePanel)
    {
        this.circlePanel = circlePanel;

        this.setLayout(new GridLayout(BUTTONS,0));

        // Adding buttons to the layout
        for (int i = 0;i < RightPanel.BUTTONS;i++)
        {
            JButton button = new JButton();
            button.setBackground(generateColor());
            button.addActionListener(this);
            this.add(button);
        }

        this.setVisible(true);
    }

    public Color generateColor()
    {
        Random random = new Random();
        // 255 - maximum value. 0 - minimum value
        int randomNumber1 = random.nextInt((255 - 0) + 1) + 0;
        int randomNumber2 = random.nextInt((255 - 0) + 1) + 0;
        int randomNumber3 = random.nextInt((255 - 0) + 1) + 0;
        Color color = new Color(randomNumber1,randomNumber2,randomNumber3);
        return color;
    }

    @Override
    public void actionPerformed(ActionEvent e)
    {
        Color color = ((JButton)e.getSource()).getBackground();
        this.circlePanel.setColor(color);
    }
}

public class LeftPanel extends JPanel implements ActionListener
{
    private static final long serialVersionUID = 1L;

    private static final int BUTTONS = 6;

    private CirclePanel circlePanel;

    public LeftPanel(CirclePanel circlePanel)
    {
        this.circlePanel = circlePanel;

        this.setLayout(new GridLayout(BUTTONS,0));

        // Adding buttons to the layout
        for (int i = 0;i < LeftPanel.BUTTONS;i++)
        {
            JButton button = new JButton();
            button.setBackground(generateColor());
            button.addActionListener(this);
            this.add(button);
        }

        this.setVisible(true);
    }

    public Color generateColor()
    {
        Random random = new Random();
        // 255 - maximum value. 0 - minimum value
        int randomNumber1 = random.nextInt((255 - 0) + 1) + 0;
        int randomNumber2 = random.nextInt((255 - 0) + 1) + 0;
        int randomNumber3 = random.nextInt((255 - 0) + 1) + 0;
        Color color = new Color(randomNumber1,randomNumber2,randomNumber3);
        return color;
    }

    @Override
    public void actionPerformed(ActionEvent e)
    {
        Color color = ((JButton)e.getSource()).getBackground();
        this.circlePanel.setColor(color);
    }
}

MyFrame.java:

public class MyFrame extends JFrame 
{
    private static final long serialVersionUID = 1L;
    private CirclePanel circlePanel;

    public MyFrame()
    {
        super("paintComponent");

        this.circlePanel = new CirclePanel();
        this.setLayout(new BorderLayout());
        this.setSize(400,400);
        this.setResizable(false);
        this.add(new LeftPanel(this.circlePanel),BorderLayout.WEST);
        this.add(new RightPanel(this.circlePanel),BorderLayout.EAST);
        this.add(new CirclePanel(),BorderLayout.CENTER);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);
    }

    public static void main(String[] args)
    {
        new MyFrame();
    }
}

I checked to see if the program get's to each actionPerformed() method and it does.

So what's wrong with it?


Solution

  • The CirclePanel you display, is not the one that you passed to the left/right panels, see :

    this.add(new LeftPanel(this.circlePanel),BorderLayout.WEST);
    this.add(new RightPanel(this.circlePanel),BorderLayout.EAST);
    this.add(new CirclePanel(),BorderLayout.CENTER);
    

    You probably wanted to do :

    this.add(new LeftPanel(this.circlePanel),BorderLayout.WEST);
    this.add(new RightPanel(this.circlePanel),BorderLayout.EAST);
    this.add(this.circlePanel,BorderLayout.CENTER);