How do I draw a rectangle every time I click on a JCheckBox?

866 views Asked by At

I am trying to draw a filled rectangle whose color corresponds to the checkbox. The rectangle is to be randomly sized and placed on the JPanel. So far, I have gotten the rectangle to draw to the JPanel with a random size and random location, but I am having trouble with drawing a rectangle when a checkbos is selected. The code in my ActionListener serves no purpose to the running of my project and I have tried other things, but am having trouble.

package colorViewerCheck;

public class ColorViewerCheckFrame extends JFrame
{
private static final int FRAME_WIDTH = 300;
private static final int FRAME_HEIGHT = 400;

private JPanel colorPanel;
private JCheckBox redCheckBox;
private JCheckBox greenCheckBox;
private JCheckBox blueCheckBox;

 public ColorViewerCheckFrame()
 { 
     createColorPanel();
 createControlPanel();
 setSize(FRAME_WIDTH, FRAME_HEIGHT);
 }

 public void createControlPanel()
 {
     class ColorListener implements ActionListener
        {
            public void actionPerformed(ActionEvent event)
            {
                if (redCheckBox.isSelected()) {createColorPanel();}
                if (greenCheckBox.isSelected()) {createColorPanel();}
                if (blueCheckBox.isSelected()) {createColorPanel();}
            }
        }

     ActionListener listener = new ColorListener();

     redCheckBox = new JCheckBox("Red");
     greenCheckBox = new JCheckBox("Green");
     blueCheckBox = new JCheckBox("Blue");
     redCheckBox.setSelected(true);

     JPanel controlPanel = new JPanel();

     controlPanel.add(redCheckBox);
     controlPanel.add(greenCheckBox);
     controlPanel.add(blueCheckBox);

     redCheckBox.addActionListener(listener);
     greenCheckBox.addActionListener(listener);
     blueCheckBox.addActionListener(listener);

     add(controlPanel, BorderLayout.SOUTH); 


 }

public void createColorPanel()
{
     class RectangleComponent extends JComponent
     {
         private int x;
         private int y;
         private int width;
         private int height;
         private Random rand;

         public void paintComponent(Graphics g)
         {
             Graphics2D g2 = (Graphics2D) g;

             rand = new Random();


             x = rand.nextInt(250);
             y = rand.nextInt(350);
             width = rand.nextInt(50);
             height = rand.nextInt(50);

             Rectangle rect = new Rectangle(x, y, width, height);
             g2.setColor(getColor());
             g2.fill(rect);
             g2.draw(rect);
         }
     }

     colorPanel = new JPanel(new GridLayout());

     RectangleComponent rect = new RectangleComponent();
     colorPanel.add(rect);
     add(colorPanel, BorderLayout.CENTER);
     colorPanel.setVisible(true);
}

public Color getColor()
{
    Color color = null;
    if (redCheckBox.isSelected()) {color = Color.RED;}
    else if (greenCheckBox.isSelected()) {color = Color.GREEN;}
    else if (blueCheckBox.isSelected()) {color = Color.blue;}
    return color;
}
}
1

There are 1 answers

1
MadProgrammer On

Instead of trying to create a new JComponent each time, simply create a simple drawing surface which is capable of adding new "rectangles" and painting them onto itself

BorderLayout can only display a single component within in each of it's five available positions...

Updated...

Before you go to much further, you're going to need to have a look at...

Basically, you want to create a single, custom component, extending from something like JPanel, which is responsible for painting the rectangles.

Next, you'll need some kind of concept of a "rectangle", which knows it's position, size and color.

Then, when the user selects a color item, you want to create a "rectangle" and "add" it to the drawing container, for example...

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Rectangle2D;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import javax.swing.ButtonGroup;
import javax.swing.ButtonModel;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Test {

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

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                final DrawPane drawPane = new DrawPane();
                final ControlPane controlPane = new ControlPane();
                controlPane.addActionListener(new ActionListener() {
                    private Random rand = new Random();

                    @Override
                    public void actionPerformed(ActionEvent e) {

                        int x = rand.nextInt(drawPane.getWidth() - 50);
                        int y = rand.nextInt(drawPane.getHeight() - 50);
                        int width = rand.nextInt(50);
                        int height = rand.nextInt(50);

                        ColorRectangle rectangle = new ColorRectangle(x, y, width, height, controlPane.getSelectedColor());
                        drawPane.add(rectangle);

                    }
                });

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(drawPane);
                frame.add(controlPane, BorderLayout.WEST);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class ColorRectangle extends Rectangle2D.Double {

        private Color color;

        public ColorRectangle(double x, double y, double w, double h, Color color) {
            super(x, y, w, h);
            this.color = color;
        }

        public Color getColor() {
            return color;
        }

    }

    public class ControlPane extends JPanel {

        private JRadioButton red;
        private JRadioButton green;
        private JRadioButton blue;

        private ButtonGroup bg;

        public ControlPane() {
            setLayout(new GridBagLayout());

            bg = new ButtonGroup();

            red = createButton("Red");
            green = createButton("Green");
            blue = createButton("Blue");
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridwidth = GridBagConstraints.REMAINDER;
            gbc.anchor = GridBagConstraints.WEST;

            add(red, gbc);
            add(green, gbc);
            add(blue, gbc);
        }

        public void addActionListener(ActionListener listener) {
            red.addActionListener(listener);
            green.addActionListener(listener);
            blue.addActionListener(listener);
        }

        public void removeActionListener(ActionListener listener) {
            red.removeActionListener(listener);
            green.removeActionListener(listener);
            blue.removeActionListener(listener);
        }

        protected JRadioButton createButton(String text) {
            JRadioButton btn = new JRadioButton(text);
            btn.setActionCommand(text);
            bg.add(btn);
            return btn;
        }

        protected Color getSelectedColor() {
            Color color = Color.MAGENTA;
            ButtonModel model = bg.getSelection();
            if (model != null) {
                switch (model.getActionCommand()) {
                    case "Red":
                        color = Color.RED;
                        break;
                    case "Green":
                        color = Color.GREEN;
                        break;
                    case "Blue":
                        color = Color.BLUE;
                        break;
                }
            }
            return color;
        }

    }

    public class DrawPane extends JPanel {

        private List<ColorRectangle> rectangles;

        public DrawPane() {
            rectangles = new ArrayList<>(25);
        }

        public void add(ColorRectangle rect) {
            rectangles.add(rect);
            repaint();
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(200, 200);
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g.create();
            for (ColorRectangle rectangle : rectangles) {
                g2d.setColor(rectangle.getColor());
                g2d.fill(rectangle);
            }
            g2d.dispose();
        }

    }

}