Java GUI应用程序,允许用户选择图形的形状和颜色

Java GUI应用程序,允许用户选择图形的形状和颜色,java,eclipse,swing,user-interface,Java,Eclipse,Swing,User Interface,我需要创建一个程序,允许用户从复选框列表中选择颜色,红色和蓝色,然后从 单选按钮列表,正方形或圆形。当按下“绘制”按钮时,选择的 绘制形状和颜色。如果同时选择了红色和蓝色,则形状将以紫色绘制 应如下图所示: 这大概是我所知道的,关于如何创建圆并在选择该选项时打印它,我感到困惑。还有,如何重新组织标签和按钮 谢谢你的帮助 import java.awt.GridBagLayout; import java.io.PrintWriter; import java.util.Scanner; im

我需要创建一个程序,允许用户从复选框列表中选择颜色,红色和蓝色,然后从 单选按钮列表,正方形或圆形。当按下“绘制”按钮时,选择的 绘制形状和颜色。如果同时选择了红色和蓝色,则形状将以紫色绘制

应如下图所示:

这大概是我所知道的,关于如何创建圆并在选择该选项时打印它,我感到困惑。还有,如何重新组织标签和按钮

谢谢你的帮助

import java.awt.GridBagLayout;
import java.io.PrintWriter;
import java.util.Scanner;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class Shapes
{
    public static JFrame window = new JFrame("Shapes");
    public static JPanel panel = new JPanel(new GridBagLayout());

    public static void main(String[] args) 
    {

        window.setBounds(0, 0,300, 300);
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        window.add(panel);
        MApp m = new MApp();
        m.setBounds(100,100,100,100);
        window.add(m);

        Draw d = new Draw(panel) ;
        d.setBounds(0, 0, window.getWidth(), 90);
        window.add(d);

        window.setVisible(true);
    }

}
import java.awt.Color; 
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.*; 
import java.awt.event.*;
import javax.swing.*;

import javax.swing.JPanel;

public class MApp extends JPanel implements MouseListener 
{
    private boolean clicked; 
    private Rectangle r; 
    public MApp()
    {
        clicked = false;
        r = new Rectangle(15, 15, 50, 50); 
        addMouseListener(this);
    }
    public void paintComponent(Graphics g)
    {
        if(clicked)
        {
            g.setColor(Color.BLUE);
        }
        else
        {
            g.setColor(Color.RED);
        } 
        g.fillRect((int)r.getX(), (int)r.getY(),
        (int)r.getWidth(), (int)r.getHeight()); 
    }
    public void mouseClicked (MouseEvent e) 
    {
        Point p = new Point(e.getX(),e.getY()); 
        if(r.contains(p))
        {
            clicked = !clicked; 
        }
        repaint(); 
    }
    public void Circle()
    {
         g.fillOval(0, 0, s, s);
    }
    public void mousePressed (MouseEvent evnt) {}
    public void mouseReleased (MouseEvent evnt) {}
    public void mouseEntered (MouseEvent evnt) {}
    public void mouseExited (MouseEvent evnt) {} 
}

    import java.awt.Color;
    import java.awt.FlowLayout;
    import java.awt.GridBagConstraints;
    import java.awt.GridBagLayout;
    import java.awt.Panel;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;

    import javax.swing.ButtonGroup;
    import javax.swing.GroupLayout;
    import javax.swing.JButton;
    import javax.swing.JCheckBox;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JRadioButton;
    import javax.swing.JTextField;

    public class Draw extends JPanel implements ActionListener
    {
        JTextField tfInfo;
        JLabel lblColor, lblShapes;
        JCheckBox cbRed, cbBlue;
        ButtonGroup shapes;
        JRadioButton rbCircle, rbSquare;
        JButton btnSubmit; 
        public Draw(JPanel panel) 
        {
            GridBagConstraints c = new GridBagConstraints();
            tfInfo = new JTextField("Color", 15);
            tfInfo = new JTextField("Shapes", 50);
            lblColor = new JLabel("Colors:");
            cbRed = new JCheckBox("Red");
            cbBlue = new JCheckBox("Blue");
            lblShapes = new JLabel("Shapes:");
            shapes = new ButtonGroup();
            rbCircle = new JRadioButton("Circle");
            rbSquare = new JRadioButton("Square");
            btnSubmit = new JButton("Draw"); 
            btnSubmit.addActionListener(this);
            this.setBackground(Color.WHITE);

            add(lblColor);
            add(cbRed); 
            add(cbBlue); 
            add(lblShapes);
            add(rbCircle);
            add(rbSquare);
            add(btnSubmit);
            shapes.add(rbCircle);
            shapes.add(rbSquare); 
        }
        public void actionPerformed(ActionEvent a)
        {
            if(a.getSource() == btnSubmit)
            { 
                if(cbRed.isSelected()&&cbBlue.isSelected())
                {
                    if(rbCircle.isSelected())
                    {

                    }
                    else if(rbSquare.isSelected())
                    {

                    } 
                }
                else if(cbRed.isSelected())
                {   
                    if(rbCircle.isSelected())
                    { 

                    }
                    else if(rbSquare.isSelected())
                    {

                    }    
                }
                else if(cbBlue.isSelected())
                {  
                    if(rbCircle.isSelected())
                    {

                    } 
                }
                else if(rbSquare.isSelected())
                {

                } 
            }
            repaint();
        }
    }
首先,将“管理”代码与“绘制”代码分开

您应该有一个单独的类,它只处理形状的绘制,其他什么都不处理,它只执行它所告诉的

然后应该有第二个类,它接受用户的输入,当用户按下Draw按钮时,它会告诉“paint”类应该绘制什么,例如

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 javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class DrawStuff extends JFrame {

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

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

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new ControlPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class ControlPane extends JPanel {

        private JRadioButton circle;
        private JRadioButton square;

        private DrawPane drawPane;

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

            ButtonGroup bg = new ButtonGroup();
            circle = new JRadioButton("Circle");
            square = new JRadioButton("Square");

            bg.add(circle);
            bg.add(square);

            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridwidth = GridBagConstraints.REMAINDER;
            gbc.weightx = 1;

            JPanel shape = new JPanel();
            shape.add(circle);
            shape.add(square);
            add(shape, gbc);

            JButton draw = new JButton("Draw");
            draw.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    if (circle.isSelected()) {
                        drawPane.setDrawableShape(DrawableShape.CIRCLE);
                    } else if (square.isSelected()) {
                        drawPane.setDrawableShape(DrawableShape.SQUARE);
                    }
                }
            });

            gbc.gridwidth = GridBagConstraints.REMAINDER;
            add(draw, gbc);

            drawPane = new DrawPane();

            gbc.weightx = 1;
            gbc.weighty = 1;
            gbc.fill = gbc.BOTH;
            add(drawPane, gbc);
        }

    }

    public enum DrawableShape {
        CIRCLE,
        SQUARE
    }

    public class DrawPane extends JPanel {

        private DrawableShape drawableShape;

        public DrawPane() {
        }

        public void setDrawableShape(DrawableShape drawableShape) {
            this.drawableShape = drawableShape;
            repaint();
        }

        public DrawableShape getDrawableShape() {
            return drawableShape;
        }

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

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g.create();
            DrawableShape shape = getDrawableShape();
            if (shape != null) {
                int width = getWidth() - 20;
                int height = getHeight() - 20;
                int size = Math.min(width, height);

                int x = (getWidth() - size) / 2;
                int y = (getHeight() - size) / 2;
                if (shape == DrawableShape.CIRCLE) {
                    g2d.fillOval(x, y, size, size);
                } else if (shape == DrawableShape.SQUARE) {
                    g2d.fillRect(x, y, size, size);
                }
            }
            g2d.dispose();
        }

    }
}
我将留给您添加颜色管理

仔细看看:

欲知详情