GUI应用程序,允许用户选择图形的形状和颜色 [英] GUI Application that allows the user to choose the shape and color of a drawing

查看:42
本文介绍了GUI应用程序,允许用户选择图形的形状和颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要创建一个程序,该程序允许用户从复选框列表中选择颜色,红色和蓝色,然后从单选按钮列表(正方形或圆形).当按下绘图"按钮时,选定的绘制形状和颜色.如果同时选择了红色和蓝色,则形状将显示为紫色.

I need to create a program that allows the user to select a color from a list of checkboxes, red and blue, and then a shape from a list of radio buttons, square or circle. When the "Draw" button is pressed the selected shape and color are drawn. If both red and blue are chosen, the shape is drawn in purple.

应如下图所示:

这是我所了解的,关于如何创建圆并在选择该选项时进行打印感到困惑.还有如何重新组织标签和按钮?

This is about as far i've gotten, stumped as to how to create the circle and print it when that option is chosen. Also how do I reorganize the labels and buttons?

感谢您的帮助

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

推荐答案

首先将管理"代码与绘画"代码分开

Start by separating your "management" code from you "painting" code

您应该有一个仅处理形状绘制的类,没有别的,它只是按照所告诉的内容进行操作.

You should have a single class that only handles the painting of the shape, nothing else, it just does what it's told.

然后,您应该有第二个类,该类从用户那里接受输入,当他们按下 Draw 按钮时,它告诉"paint"类应该绘画的内容,例如...

You should then have a second class which takes input from the user and when they press the Draw button, it tells the "paint" class what it should be paint, for example...

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

    }
}

我让您添加颜色管理.

仔细看看:

了解更多详情

这篇关于GUI应用程序,允许用户选择图形的形状和颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆