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

时间:2015-12-09 00:20:29

标签: java eclipse swing user-interface

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

应如下图所示:

enter image description here

到目前为止,我已经知道如何创建圆圈并在选择该选项时打印它。另外我如何重新组织标签和按钮?

感谢任何帮助

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

1 个答案:

答案 0 :(得分:4)

首先将您的"管理层"分开。来自你的代码"绘画"代码

你应该有一个只处理形状绘画的课程,没有别的,它只是做了它所说的。

然后你应该有第二个类从用户那里获取输入,当他们按下 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();
        }

    }
}

我将让您添加色彩管理。

仔细看看:

了解更多详情