繁体   English   中英

Java Swing 绘制小程序

[英]Java Swing Draw Applet

我正在做一项任务,我撞到了一堵砖墙,真的可以使用一些指导。 该程序启动一个 Java GUI 小程序(我有这个工作),在该小程序中,您可以在几个选项之间进行选择,例如形状类型(椭圆形或矩形)、填充类型(实心或空心)和颜色(一些颜色选项)。

应用程序中有一个 Draw 按钮,这个 draw 按钮应该根据上面所做的选择绘制一个图像。

对于形状类型,我正在查看带有“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

此外,您在此处看到的类和方法是我们被建议使用的类和方法,

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

在我看来,您有 3 个属性来控制绘画:

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

所以你的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);
}

现在您需要为您的 Rectangle 和 Oval 对象实现绘画代码。 就像是:

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

上述结构将允许您在绘图面板上仅绘制一个形状。 如果您需要多个形状,那么您需要修改您的绘图类。 我在上面提供的关于“自定义绘画方法”的链接显示了如何做到这一点。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM