javaswingjbuttonjcolorchooser

Rectangle Color Chooser program


I am having two problems here. I'm making a simple program to test, basically when you click the button JColorChooser will pop up and you can choose what color you want your rectangle to be. And the second problem is i cannot position my buttons at BorderLayout.SOUTH or BorderLayout.NORTH Or anywhere. These are my code

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class GUI extends JPanel {
    private Color a = (Color.WHITE);
    private Color b = (Color.WHITE);
    private final JPanel panel;
    private final JButton ab;
    private final JButton bb;
    private int x = 1;
    private int y = 1;

    public GUI() {
        panel = new JPanel();
        panel.setBackground(Color.WHITE);

        ab = new JButton("Choose your first Rectangle color");
        ab.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent event) {
                a = JColorChooser.showDialog(null, "Pick a Color", a);
                x = 2;
            }
        });
        bb = new JButton("Choose your second Rectangle color");
        bb.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent event) {
                b = JColorChooser.showDialog(null, "Pick a Color", b);
                y = 2;
            }
        });
        add(ab, BorderLayout.NORTH);
        add(panel, BorderLayout.CENTER);
        add(bb, BorderLayout.SOUTH);

    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        this.setBackground(Color.WHITE);
        if (x == 2)
            g.setColor(a);
        g.fillRect(50, 50, 100, 20);
        if (y == 2)
            g.setColor(b);
        g.fillRect(50, 200, 100, 20);
    }
}

Solution

  • And the second problem is i cannot position my buttons at BorderLayout.SOUTH or BorderLayout.NORTH Or anywhere.

    JPanel uses a FlowLayout by default, try adding setLayout(new BorderLayout()) before you add any components

    setLayout(new BorderLayout());
    add(ab, BorderLayout.NORTH);
    add(panel, BorderLayout.CENTER);
    add(bb, BorderLayout.SOUTH);
    

    I'm making a simple program to test, basically when you click the button jcolorchooser will pop up and you can choose what color you want your rectangle to be

    Okay, I'm "guessing" that once you've selected a color, it's not changing the rectangle color.

    Simply add a call to repaint after you've changed the color

    ab.addActionListener(
        new ActionListener() {
            public void actionPerformed(ActionEvent event) {
                a = JColorChooser.showDialog(null, "Pick a Color", a);
                x = 2;
                repaint();
            }
        }
    );
    bb = new JButton("Choose your second Rectangle color");
    bb.addActionListener(
        new ActionListener() {
            public void actionPerformed(ActionEvent event) {
                b = JColorChooser.showDialog(null, "Pick a Color", b);
                y = 2;
                repaint();
            }
        }
    );