每次单击JCheckBox时如何绘制矩形?

时间:2014-11-12 04:41:26

标签: java swing jcheckbox

我正在尝试绘制一个填充矩形,其颜色对应于复选框。矩形应随机调整大小并放在JPanel上。到目前为止,我已经将矩形绘制到具有随机大小和随机位置的JPanel,但是在选中checkbos时我无法绘制矩形。我ActionListener中的代码没有用于运行我的项目,我尝试过其他的东西,但遇到了麻烦。

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 个答案:

答案 0 :(得分:2)

不是每次都尝试创建一个新的JComponent,而是创建一个简单的绘图表面,它能够添加新的"矩形"并将它们涂在自己身上

BorderLayout只能在其中的五个可用位置中显示单个组件...

<强>更新...

在你走得更远之前,你需要看看......

基本上,您希望创建一个自定义组件,从JPanel扩展,该组件负责绘制矩形。

接下来,您需要某种&#34;矩形&#34;的概念,它知道它的位置,大小和颜色。

然后,当用户选择一个颜色项时,你想要创建一个&#34;矩形&#34;和&#34;添加&#34;它到绘图容器,例如......

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();
        }

    }

}