Java Swing 绘图小程序

标签 java swing

我正在做一项作业,但遇到了困难,确实需要一些指导。该程序启动一个 Java GUI 小程序(我有这个工作),在该小程序中,您可以在几个选项之间进行选择,例如形状类型(椭圆形或矩形)、填充类型(实心或空心)和颜色(一些颜色选项)。

应用程序中有一个“绘制”按钮,此绘制按钮应根据上面所做的选择绘制图像。

对于形状类型,我使用“if”语句查看选择,并根据所选内容将绘图静态设置为椭圆形或矩形。

                for (String value : shapeType) {
                    if (value.equals("Rectangle")) {
                        shapeDimensions = new Rectangle(0, 0, 200, 200);
                    } else {
                        shapeDimensions = new Rectangle(40, 30, 100, 125);
                    }
                }

这同样适用于颜色,如果选择的是红色,那么我将“颜色”变量设置为 color = new Color(255, 0, 0)

我遇到的问题是填充类型(实心还是空心)?我不知道该怎么办。我已将所有代码放在下面以供引用。正如您将看到的,我有一个自定义 Shape 抽象类和构造函数,我也应该传递此信息。然后,我有诸如 setColorgetSolid 之类的方法以及两个名为 Rectangle 和 Oval 的子类,其中取决于我选择的形状类型(椭圆形或矩形),我应该画出其中一个形状。

我不知道如何处理填充类型或如何将其传递给某些东西。我也不确定我在 Shape 类下使用了正确的参数类型。我也不确定我应该进行计算来确定颜色和形状类型,就像我在绘制按钮 actionListener 下的方式一样。任何和所有的帮助将不胜感激!

package PRJ3;

import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Rectangle2D;

public class PRJ3 extends JFrame {
    public PRJ3() {
    }

    abstract static class Shape extends Rectangle {
        private static Color shapeColor;
        private static Shape shape;
        private static Rectangle shapeType;

        Shape(Rectangle shapeType, Color shapeColor, Shape shapeFill) {
            Shape.shapeType = shapeType;
            Shape.shapeColor = shapeColor;
            Shape.shape = shapeFill;
        } // end Shape constructor

        public void setColor(Color shapeColor) {
            /** should accept the graphics object as a parameter and set the color for the next
             *  draw operation to the color of the current shape.
             */
            Shape.shapeColor = shapeColor;
        }

        public Shape getSolid() {
            return shape;
        }

        static int getNoOfShapes() {
            return 1;
        }

        abstract void draw (Graphics graphicObject);

        static class Oval extends Shape {
        Dimension objectDimension;
        Rectangle graphicObject;
        Color shapeColor;
        Shape shapeFill;

        Oval(Rectangle graphicObject, Color shapeColor, Shape shapeFill) {
            super(graphicObject, shapeColor, shapeFill);
        }

            @Override
            void draw(Graphics graphicObject) {

            }

        } // end over Oval subClass

        static class Rectangular extends Shape {

        Rectangular(Rectangle graphicObject, Color shapeColor, Shape shapeFill) {
            super(graphicObject, shapeColor, shapeFill);
        }

        @Override
        void draw(Graphics graphicObject) {
            Drawing drawRectangle = new Drawing();
        }

    } // end of Rectangle subClass

        static class Drawing extends JPanel {
            private Shape shape;

            @Override
            public void paintComponent(Graphics g) {
                super.paintComponent(g);
                g.drawString("Shapes: " + Shape.getNoOfShapes(), 10, 20);
                /** Shape isn't initialized when this is called the first time */
                if (shape != null) {
                    shape.draw(g);
                }
            }

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

            public void drawShape() throws OutsideBounds {
                // check provided size vs preferred size
                if (shape.getWidth() > this.getPreferredSize().getWidth() || shape.getHeight() > this.getPreferredSize().getHeight()) {
                    throw new OutsideBounds();
                } else {
                    this.shape = shape;
                    repaint();
                }

            } // end drawShape

        } // end of Drawing subClass

        static class OutsideBounds extends Throwable {
}

    } // end of Shape parent class

    static public void main(String[] args) {

        JPanel contentPane;
        JTextField tfWidth;
        JTextField tfHeight;
        JTextField tfxCoordinate;
        JTextField tfyCoordinate;
        JComboBox cbxShapeType;
        JLabel lblShapeType;
        JPanel panelShapeDrawing;
        JLabel lblFillType;
        JComboBox cbxFillType;
        JLabel lblColor;
        JComboBox cbxColor;
        JLabel lblWidth;
        JLabel lblHeight;
        JLabel lblxCoordinate;
        JLabel lblyCoordinate;
        JButton btnDraw;
        final String[] shapeType = new String[1];
        final String[] shapeColor = new String[1];
        final String[] shapeFill = new String[1];

        JFrame Frame = new JFrame("Geometric Drawing");
        Frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Frame.setBounds(100, 100, 601, 330);
        contentPane = new JPanel();
        Frame.setContentPane(contentPane);
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        GridBagLayout gbl_contentPane = new GridBagLayout();
        gbl_contentPane.columnWidths = new int[] {20, 106, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 0};
        gbl_contentPane.rowHeights = new int[] {30, 30, 30, 30, 29, 30, 29, 30, 19, 30, 0};
        gbl_contentPane.columnWeights = new double[]{0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, Double.MIN_VALUE};
        gbl_contentPane.rowWeights = new double[]{0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
        contentPane.setLayout(gbl_contentPane);

        lblShapeType = new JLabel("Shape Type");
        GridBagConstraints gbc_lblShapeType = new GridBagConstraints();
        gbc_lblShapeType.fill = GridBagConstraints.HORIZONTAL;
        gbc_lblShapeType.insets = new Insets(0, 0, 5, 5);
        gbc_lblShapeType.gridx = 1;
        gbc_lblShapeType.gridy = 1;
        contentPane.add(lblShapeType, gbc_lblShapeType);

        String[] shapeTypes = {"" ,"Oval", "Rectangle"};
        cbxShapeType = new JComboBox(shapeTypes);
        GridBagConstraints gbc_cbxShapeType = new GridBagConstraints();
        gbc_cbxShapeType.fill = GridBagConstraints.HORIZONTAL;
        gbc_cbxShapeType.insets = new Insets(0, 0, 5, 5);
        gbc_cbxShapeType.gridx = 2;
        gbc_cbxShapeType.gridy = 1;
        contentPane.add(cbxShapeType, gbc_cbxShapeType);

        panelShapeDrawing = new JPanel();
        panelShapeDrawing.setLayout(new BorderLayout());
        String title = "Shape Drawing";
        Border border = BorderFactory.createTitledBorder(title);
        panelShapeDrawing.setBorder(border);
        GridBagConstraints gbc_panelShapeDrawing = new GridBagConstraints();
        gbc_panelShapeDrawing.insets = new Insets(0, 0, 5, 0);
        gbc_panelShapeDrawing.gridwidth = 10;
        gbc_panelShapeDrawing.gridheight = 8;
        gbc_panelShapeDrawing.fill = GridBagConstraints.BOTH;
        gbc_panelShapeDrawing.gridx = 3;
        gbc_panelShapeDrawing.gridy = 1;
        contentPane.add(panelShapeDrawing, gbc_panelShapeDrawing);

        lblFillType = new JLabel("Fill Type");
        GridBagConstraints gbc_lblFillType = new GridBagConstraints();
        gbc_lblFillType.fill = GridBagConstraints.HORIZONTAL;
        gbc_lblFillType.insets = new Insets(0, 0, 5, 5);
        gbc_lblFillType.gridx = 1;
        gbc_lblFillType.gridy = 2;
        contentPane.add(lblFillType, gbc_lblFillType);

        String[] fillTypes = {"", "Solid", "Hollow"};
        cbxFillType = new JComboBox(fillTypes);
        GridBagConstraints gbc_cbxFillType = new GridBagConstraints();
        gbc_cbxFillType.insets = new Insets(0, 0, 5, 5);
        gbc_cbxFillType.fill = GridBagConstraints.BOTH;
        gbc_cbxFillType.gridx = 2;
        gbc_cbxFillType.gridy = 2;
        contentPane.add(cbxFillType, gbc_cbxFillType);

        lblColor = new JLabel("Color");
        GridBagConstraints gbc_lblColor = new GridBagConstraints();
        gbc_lblColor.fill = GridBagConstraints.HORIZONTAL;
        gbc_lblColor.insets = new Insets(0, 0, 5, 5);
        gbc_lblColor.gridx = 1;
        gbc_lblColor.gridy = 3;
        contentPane.add(lblColor, gbc_lblColor);

        String[] supportedColors = {"", "Red", "Black", "Orange", "Yellow", "Green", "Blue", "Magenta"};
        cbxColor = new JComboBox(supportedColors);
        GridBagConstraints gbc_cbxColor = new GridBagConstraints();
        gbc_cbxColor.insets = new Insets(0, 0, 5, 5);
        gbc_cbxColor.fill = GridBagConstraints.HORIZONTAL;
        gbc_cbxColor.gridx = 2;
        gbc_cbxColor.gridy = 3;
        contentPane.add(cbxColor, gbc_cbxColor);

        lblWidth = new JLabel("Width");
        GridBagConstraints gbc_lblWidth = new GridBagConstraints();
        gbc_lblWidth.fill = GridBagConstraints.HORIZONTAL;
        gbc_lblWidth.insets = new Insets(0, 0, 5, 5);
        gbc_lblWidth.gridx = 1;
        gbc_lblWidth.gridy = 4;
        contentPane.add(lblWidth, gbc_lblWidth);

        tfWidth = new JTextField();
        GridBagConstraints gbc_tfWidth = new GridBagConstraints();
        gbc_tfWidth.fill = GridBagConstraints.BOTH;
        gbc_tfWidth.insets = new Insets(0, 0, 5, 5);
        gbc_tfWidth.gridx = 2;
        gbc_tfWidth.gridy = 4;
        contentPane.add(tfWidth, gbc_tfWidth);
        tfWidth.setColumns(10);

        lblHeight = new JLabel("Height");
        GridBagConstraints gbc_lblHeight = new GridBagConstraints();
        gbc_lblHeight.fill = GridBagConstraints.HORIZONTAL;
        gbc_lblHeight.insets = new Insets(0, 0, 5, 5);
        gbc_lblHeight.gridx = 1;
        gbc_lblHeight.gridy = 5;
        contentPane.add(lblHeight, gbc_lblHeight);

        tfHeight = new JTextField();
        tfHeight.setColumns(10);
        GridBagConstraints gbc_tfHeight = new GridBagConstraints();
        gbc_tfHeight.insets = new Insets(0, 0, 5, 5);
        gbc_tfHeight.fill = GridBagConstraints.BOTH;
        gbc_tfHeight.gridx = 2;
        gbc_tfHeight.gridy = 5;
        contentPane.add(tfHeight, gbc_tfHeight);

        lblxCoordinate = new JLabel("x coordinate");
        GridBagConstraints gbc_lblxCoordinate = new GridBagConstraints();
        gbc_lblxCoordinate.fill = GridBagConstraints.HORIZONTAL;
        gbc_lblxCoordinate.insets = new Insets(0, 0, 5, 5);
        gbc_lblxCoordinate.gridx = 1;
        gbc_lblxCoordinate.gridy = 6;
        contentPane.add(lblxCoordinate, gbc_lblxCoordinate);

        tfxCoordinate = new JTextField();
        tfxCoordinate.setColumns(10);
        GridBagConstraints gbc_tfxCoordinate = new GridBagConstraints();
        gbc_tfxCoordinate.insets = new Insets(0, 0, 5, 5);
        gbc_tfxCoordinate.fill = GridBagConstraints.BOTH;
        gbc_tfxCoordinate.gridx = 2;
        gbc_tfxCoordinate.gridy = 6;
        contentPane.add(tfxCoordinate, gbc_tfxCoordinate);

        lblyCoordinate = new JLabel("y coordinate");
        GridBagConstraints gbc_lblyCoordinate = new GridBagConstraints();
        gbc_lblyCoordinate.fill = GridBagConstraints.HORIZONTAL;
        gbc_lblyCoordinate.insets = new Insets(0, 0, 5, 5);
        gbc_lblyCoordinate.gridx = 1;
        gbc_lblyCoordinate.gridy = 7;
        contentPane.add(lblyCoordinate, gbc_lblyCoordinate);

        tfyCoordinate = new JTextField();
        tfyCoordinate.setColumns(10);
        GridBagConstraints gbc_tfyCoordinate = new GridBagConstraints();
        gbc_tfyCoordinate.insets = new Insets(0, 0, 5, 5);
        gbc_tfyCoordinate.fill = GridBagConstraints.BOTH;
        gbc_tfyCoordinate.gridx = 2;
        gbc_tfyCoordinate.gridy = 7;
        contentPane.add(tfyCoordinate, gbc_tfyCoordinate);

        btnDraw = new JButton("Draw");
        GridBagConstraints gbc_btnDraw = new GridBagConstraints();
        gbc_btnDraw.anchor = GridBagConstraints.EAST;
        gbc_btnDraw.fill = GridBagConstraints.VERTICAL;
        gbc_btnDraw.insets = new Insets(0, 0, 5, 5);
        gbc_btnDraw.gridx = 2;
        gbc_btnDraw.gridy = 8;
        contentPane.add(btnDraw, gbc_btnDraw);

        btnDraw.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                Shape.Drawing customShape = new Shape.Drawing();
                shapeType[0] = (String) cbxShapeType.getSelectedItem();
                shapeFill[0] = (String) cbxFillType.getSelectedItem();
                shapeColor[0] = (String) cbxColor.getSelectedItem();
                Rectangle shapeDimensions = null;
                Color color = null;
                Shape shape = null;


                for (String value : shapeType) {
                    if (value.equals("Rectangle")) {
                        shapeDimensions = new Rectangle(0, 0, 200, 200);
                    } else {
                        shapeDimensions = new Rectangle(40, 30, 100, 125);
                    }
                }

                for (String value : shapeFill) {
                    if (value.equals("Fill")) {
                        Shape fill = new Rectangle2D()
                    }
                }

                for (String s : shapeColor) {
                    if (s.equals("Red")) {
                        color = new Color(255, 0, 0);
                    } else if (s == "Black") {
                        color = new Color(0, 0, 0);
                    } else if (s == "Orange") {
                        color = new Color(255, 102, 0);
                    } else if (s == "Yellow") {
                        color = new Color(255, 255, 0);
                    } else if (s == "Green") {
                        color = new Color(0, 204, 0);
                    } else if (s == "Blue") {
                        color = new Color(0, 0, 255);
                    } else if (s == "Magenta") {
                        color = new Color(255, 0, 255);
                    }
                }

                Color finalColor = color;
                Shape test = new Shape(shapeDimensions, finalColor, shape) {
                    @Override
                    void draw(Graphics graphicObject) {
                    }
                };
                test.setColor(color);
            }
        });

        panelShapeDrawing.setVisible(true);
        contentPane.setVisible(true);
        Frame.setVisible(true);

    } // end main

} // end PRJ3

最佳答案

Also, the classes and methods you see here are the ones that we were advised to use,

好吧,我不知道给了你什么以及你自己写了什么,但我在上面看到的内容令人困惑。

对我来说,你似乎有 3 个属性来控制绘画:

  1. 矩形,包含要绘制的形状的位置和大小。
  2. 颜色
  3. Filled,它应该是一个 boolean 属性,用于控制是否应将形状绘制为填充或仅绘制轮廓。

因此,您的 Shape 类不应扩展 Rectangle。它应该只是一个具有上述 3 个属性和您的 draw(Graphics g) 方法的类。

请注意,JDK 有一个 Shape 类,因此我不喜欢使用相同的名称,因为它会导致困惑。

所以我会做这样的事情:

public abstract class DrawableShape
{
    protected Rectangle rectangle;
    protected Color color;
    protected boolean filled;

    public DrawableShape(Rectangle rectangle, Color color, boolean filled)
    {
        this.rectangle = rectangle;
        this.color = color;
        this.filled = filled;
    }

    abstract void draw(Graphics g);
}

现在您需要为矩形和椭圆形对象实现绘制代码。像这样的东西:

public class DrawableRectangle extends DrawableShape
{
    @Override
    public void draw(Graphics g)
    {
        g.setColor( color );

        if (filled)
            g.fillRect(rectangle.x, rectangle.y, rectangle.width, rectangle.height);
        else
            g.drawRect(rectangle.x, rectangle.y, rectangle.width, rectangle.height);
    }
}

现在在您的 DrawingPanel 中您需要一个如下方法:

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

Ant 绘制代码需要绘制这个形状:

@Override
protected void paintComponent(Graphics g)
{
    super.paintComponent( g );
    drawableShape.draw();
}

然后在 ActionListener 中,您需要将 DrawableShape 添加到您的 Drawing 类中:

DrawableShape drawableShape = null;

if (value.equals("Rectangle"))
    drawableShape = new DrawableRectangle(….);
else
    drawableShape = new DrawableOval(...);

drawing.setDrawableShape( drawableShape );

上述结构将允许您在绘图面板上仅绘制单个形状。如果您需要多个形状,那么您需要修改您的 Drawing 类。我在上面为您提供的“自定义绘画方法”链接展示了如何执行此操作。

关于Java Swing 绘图小程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60414740/

相关文章:

java - 找不到类 [org.apache.shiro.spring.LifecycleBeanPostProcessor]

java - 如何验证 RadioButton 是否被选中?

java - 无法为从logstash JDBC 输入插件创建的索引创建elasticsearch 的映射

java - 使用 objectify 如何获取对象的属性子集

java - 加载时显示 Java Swing 输入对话框

java - GUI...为什么我的按钮不响应第一个 if 并且总是前往下一个选项

java框架和标签

java - 使用 JEditorPane 获取所选文本的索引

java - gridbag约束不起作用

java - FHIR 资源上的 vhdir-organization 和 r4.organization 之间有什么区别?