-2

作为一个更大项目的一部分,我正在尝试创建一个基本 UI,允许用户单击 JButton 以选择所需的颜色,然后允许该用户单击另一个 JButton 以指定要显示的形状,根据填充之前选择的颜色。我明白我必须利用动作事件。

请注意我引用了这个类似的问题:

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

到目前为止,我的代码是:

import java.util.*;
import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;
import java.awt.event.*;

public class GUI extends JFrame implements ActionListener, WindowListener
{
private final JButton circleButton, rectangleButton, redButton;
private final JButton greenButton, blueButton, exitButton;
private final JTextArea textArea;
private final JLabel label1;
private final JPanel colorPane;

private String shapeColor = "black";
private String actualShape = "rectangle";

private static final int ROWS = 2, COLS = 3;

public GUI (String title)
{
    super(title);
    //setResizable(false);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    colorPane = new JPanel();

    label1 = new JLabel("current date here");
    label1.setVerticalAlignment(SwingConstants.BOTTOM);
    label1.setHorizontalAlignment(SwingConstants.LEFT);
    label1.setPreferredSize(new Dimension(200,0));
    getContentPane().add(label1, BorderLayout.WEST);

    colorPane.setLayout(new GridLayout(ROWS,COLS));
    getContentPane().add(colorPane, BorderLayout.CENTER);

    redButton = makeButton("Red");
    colorPane.add(redButton);
    greenButton = makeButton("Green");
    colorPane.add(greenButton);
    blueButton = makeButton("Blue");
    colorPane.add(blueButton);
    rectangleButton = makeButton("Rectangle");
    colorPane.add(rectangleButton);
    circleButton = makeButton("Circle");
    colorPane.add(circleButton);
    exitButton = makeButton("Exit");
    colorPane.add(exitButton);

    textArea = new JTextArea(0,20);
    getContentPane().add(textArea, BorderLayout.EAST);

    pack();
}


public void paint(Graphics g, String color)
{
    if (shapeColor.equalsIgnoreCase("blue") && actualShape.equalsIgnoreCase("rectangle"))
    {   
        g.setColor(Color.BLUE);
        g.fillRect(50, 90, 100, 50);
    }
    else if (shapeColor.equalsIgnoreCase("green") && actualShape.equalsIgnoreCase("circle"))
    {   
        g.setColor(Color.GREEN);
        g.fillOval(50, 180, 55, 55);
    }
    else if (shapeColor.equalsIgnoreCase("red") && actualShape.equalsIgnoreCase("rectangle"))
    {
        g.setColor(Color.RED);
        g.fillRect(50, 90, 100, 50);
    }
    else if (shapeColor.equalsIgnoreCase("green") && actualShape.equalsIgnoreCase("rectangle"))
    {
        g.setColor(Color.GREEN);
        g.fillRect(50,90,100,50);
    }
    else if (shapeColor.equalsIgnoreCase("blue") && actualShape.equalsIgnoreCase("circle"))
    {
        g.setColor(Color.BLUE);
        g.fillOval(50, 180, 55, 55);
    }
    else if (shapeColor.equalsIgnoreCase("red") && actualShape.equalsIgnoreCase("circle"))
    {
        g.setColor(Color.RED);
        g.fillOval(50, 180, 55, 55);
    }
}




//method designed to create new JButtons while avoiding code duplication
private JButton makeButton(String text)
{
    JButton b = new JButton(text);
    b.setHorizontalAlignment(SwingConstants.LEFT);
    b.addActionListener(this);
    b.setPreferredSize(new Dimension(125,55));

    return b;
}

@Override
public void windowOpened(WindowEvent e) {
    // TODO Auto-generated method stub

}

@Override
public void windowClosing(WindowEvent e) 
{
    System.exit(0);

}

@Override
public void windowClosed(WindowEvent e) {
    // TODO Auto-generated method stub

}

@Override
public void windowIconified(WindowEvent e) {
    // TODO Auto-generated method stub

}

@Override
public void windowDeiconified(WindowEvent e) {
    // TODO Auto-generated method stub

}

@Override
public void windowActivated(WindowEvent e) {
    // TODO Auto-generated method stub

}

@Override
public void windowDeactivated(WindowEvent e) {
    // TODO Auto-generated method stub

}

@Override
public void actionPerformed(ActionEvent e) 
{
    System.out.println( ( (JButton)e.getSource() ).getText() + " button pressed ");

    if ( ( ((JButton) e.getSource()).getText().equalsIgnoreCase("Red")) )
    {   
        setShapeColor("Red");
        System.out.println("selected color is: " + shapeColor + " selected shape is: " + actualShape);
        //paint(this.getGraphics());
    }   
    else if ( ( ((JButton) e.getSource()).getText().equalsIgnoreCase("Blue")) )
    {   
        setShapeColor("Blue");
        System.out.println("selected color is: " + shapeColor + " selected shape is: " + actualShape);
        //paint(this.getGraphics());
    }

    else if ( ( ((JButton) e.getSource()).getText().equalsIgnoreCase("Green")) )
    {   
        setShapeColor("Green");
        System.out.println("selected color is: " + shapeColor + " selected shape is: " + actualShape);
        //paint(this.getGraphics());
    }   


    if ( ( ((JButton) e.getSource()).getText().equalsIgnoreCase("Rectangle")) )
    {
        setActualShape("rectangle");
        System.out.println("selected shape is: " + actualShape + " selected color is: " + shapeColor);
        paint(this.getGraphics(), shapeColor);


    }

    else if ( ( ((JButton) e.getSource()).getText().equalsIgnoreCase("Circle")) )
    {
        setActualShape("circle");
        System.out.println("selected shape is: " + actualShape + " selected color is: " + shapeColor);
        paint(this.getGraphics(), shapeColor);
    }

}

public String getShapeColor() {
    return shapeColor;
}

public void setShapeColor(String shapeColor) {
    this.shapeColor = shapeColor;
}

public String getActualShape() {
    return actualShape;
}

public void setActualShape(String actualShape) {
    this.actualShape = actualShape;
}

public static void main(String[] args)
{
    new GUI("My Gui").setVisible(true);
}

}

到目前为止,我已经实现了一个输出,它显示了所选颜色以及要以所选颜色呈现的所选形状。

此外,我已经成功地输出了一个形状,其位置是硬编码的,但其类型(圆形或正方形)和颜色(红色、蓝色或绿色)在用户点击后正确输出。

我正在努力的最后一个阶段是形状输出的实现,以便用户的点击序列确定形状输出到屏幕的位置和尺寸。

目标是实现此处演示的功能:

https://metrostate.learn.minnstate.edu/content/2020/4560296-20201000539/ICS372%​​20-%20Assignment%202%20Video.mp4?d2lSessionVal=ARifwbCHriCBrkgxBWpL9g8fL&ou=4560296

我比较确定正确的解决方案类似于以下代码:

import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

/**
 *  Note: Normally the ButtonPanel and DrawingArea would not be static 
classes.
 *  This was done for the convenience of posting the code in one class and 
to
 *  highlight the differences between the two approaches. All the 
differences
 *  are found in the DrawingArea class.
 */
public class DrawOnComponent
{
public static void main(String[] args)
{
    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            createAndShowGUI();
        }
    });
}

private static void createAndShowGUI()
{
    DrawingArea drawingArea = new DrawingArea();
    ButtonPanel buttonPanel = new ButtonPanel( drawingArea );

    JFrame.setDefaultLookAndFeelDecorated(true);
    JFrame frame = new JFrame("Draw On Component");
    frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
    frame.getContentPane().add(drawingArea);
    frame.getContentPane().add(buttonPanel, BorderLayout.SOUTH);
    frame.setSize(400, 400);
    frame.setLocationRelativeTo( null );
    frame.setVisible(true);
}

static class ButtonPanel extends JPanel implements ActionListener
{
    private DrawingArea drawingArea;

    public ButtonPanel(DrawingArea drawingArea)
    {
        this.drawingArea = drawingArea;

        add( createButton(" ", Color.BLACK) );
        add( createButton(" ", Color.RED) );
        add( createButton(" ", Color.GREEN) );
        add( createButton(" ", Color.BLUE) );
        add( createButton(" ", Color.ORANGE) );
        add( createButton(" ", Color.YELLOW) );
        add( createButton("Clear Drawing", null) );
    }

    private JButton createButton(String text, Color background)
    {
        JButton button = new JButton( text );
        button.setBackground( background );
        button.addActionListener( this );

        return button;
    }

    public void actionPerformed(ActionEvent e)
    {
        JButton button = (JButton)e.getSource();

        if ("Clear Drawing".equals(e.getActionCommand()))
            drawingArea.clear();
        else
            drawingArea.setForeground( button.getBackground() );
    }
}

static class DrawingArea extends JPanel
{
    private final static int AREA_SIZE = 400;
    private ArrayList<ColoredRectangle> coloredRectangles = new ArrayList<ColoredRectangle>();
    private Rectangle shape;

    public DrawingArea()
    {
        setBackground(Color.WHITE);

        MyMouseListener ml = new MyMouseListener();
        addMouseListener(ml);
        addMouseMotionListener(ml);
    }

    @Override
    public Dimension getPreferredSize()
    {
        return isPreferredSizeSet() ?
            super.getPreferredSize() : new Dimension(AREA_SIZE, AREA_SIZE);
    }

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

        //  Custom code to paint all the Rectangles from the List

        Color foreground = g.getColor();

        g.setColor( Color.BLACK );
        g.drawString("Add a rectangle by doing mouse press, drag and release!", 40, 15);

        for (DrawingArea.ColoredRectangle cr : coloredRectangles)
        {
            g.setColor( cr.getForeground() );
            Rectangle r = cr.getRectangle();
            g.drawRect(r.x, r.y, r.width, r.height);
        }

        //  Paint the Rectangle as the mouse is being dragged

        if (shape != null)
        {
            Graphics2D g2d = (Graphics2D)g;
            g2d.setColor( foreground );
            g2d.draw( shape );
        }
    }

    public void addRectangle(Rectangle rectangle, Color color)
    {
        //  Add the Rectangle to the List so it can be repainted

        ColoredRectangle cr = new ColoredRectangle(color, rectangle);
        coloredRectangles.add( cr );
        repaint();
    }

    public void clear()
    {
        coloredRectangles.clear();
        repaint();
    }

    class MyMouseListener extends MouseInputAdapter
    {
        private Point startPoint;

        public void mousePressed(MouseEvent e)
        {
            startPoint = e.getPoint();
            shape = new Rectangle();
        }

        public void mouseDragged(MouseEvent e)
        {
            int x = Math.min(startPoint.x, e.getX());
            int y = Math.min(startPoint.y, e.getY());
            int width = Math.abs(startPoint.x - e.getX());
            int height = Math.abs(startPoint.y - e.getY());

            shape.setBounds(x, y, width, height);
            repaint();
        }

        public void mouseReleased(MouseEvent e)
        {
            if (shape.width != 0 || shape.height != 0)
            {
                addRectangle(shape, e.getComponent().getForeground());
            }

            shape = null;
        }
    }

    class ColoredRectangle
    {
        private Color foreground;
        private Rectangle rectangle;

        public ColoredRectangle(Color foreground, Rectangle rectangle)
        {
            this.foreground = foreground;
            this.rectangle = rectangle;
        }

        public Color getForeground()
        {
            return foreground;
        }

        public void setForeground(Color foreground)
        {
            this.foreground = foreground;
        }

        public Rectangle getRectangle()
        {
            return rectangle;
        }
    }
}
}

我知道我必须重写“paint”方法,并且作为硬编码练习,我在代码中包含了以下内容:

public void paint(Graphics g, String color)
{
    if (shapeColor.equalsIgnoreCase("blue") && actualShape.equalsIgnoreCase("rectangle"))
    {   
        g.setColor(Color.BLUE);
        g.fillRect(50, 90, 100, 50);
    }
    else if (shapeColor.equalsIgnoreCase("green") && actualShape.equalsIgnoreCase("circle"))
    {   
        g.setColor(Color.GREEN);
        g.fillOval(50, 180, 55, 55);
    }
    else if (shapeColor.equalsIgnoreCase("red") && actualShape.equalsIgnoreCase("rectangle"))
    {
        g.setColor(Color.RED);
        g.fillRect(50, 90, 100, 50);
    }
    else if (shapeColor.equalsIgnoreCase("green") && actualShape.equalsIgnoreCase("rectangle"))
    {
        g.setColor(Color.GREEN);
        g.fillRect(50,90,100,50);
    }
    else if (shapeColor.equalsIgnoreCase("blue") && actualShape.equalsIgnoreCase("circle"))
    {
        g.setColor(Color.BLUE);
        g.fillOval(50, 180, 55, 55);
    }
    else if (shapeColor.equalsIgnoreCase("red") && actualShape.equalsIgnoreCase("circle"))
    {
        g.setColor(Color.RED);
        g.fillOval(50, 180, 55, 55);
    }
}
}

我不确定如何记录用户按钮单击的坐标,然后将这些坐标传递给所需形状的构造函数。

4

2 回答 2

3

您的代码中有几个错误需要更改:

  1. 不要扩展JFrame,请参阅扩展 JFrame 与在程序中创建它,而是在您的类中创建它的实例。如果您需要从 a 扩展,JComponent让它变得更灵活,例如JPanel.

  2. 不要覆盖paint(...)方法,您需要覆盖JPanelpaintComponent(...)方法,并且不要忘记在其中调用super.paintComponent(g)作为第一行,否则您可能会破坏绘制链并产生有趣/奇怪的行为。既不传递getGraphics()对象,请参阅自定义绘画教程

  3. 使用ShapeAPI 而不是直接在其上绘图,JPanel因为它提供了更多功能。看这个帖子:在jframe中创建java的正方形、矩形、三角形

  4. 不要调用setPreferredSize,覆盖getPreferredSize,请参阅:我应该避免在 Java Swing 中使用 set(Preferred|Maximum|Minimum)Size 方法吗?

  5. 将您的程序放在 EDT 上,请参阅同一答案中第 3 点的链接帖子中的第 7 点。

因此,这是一个遵循上述建议的示例:

import java.awt.*;
import javax.swing.*;
import java.awt.geom.*;
import java.awt.event.*;

public class PaintExample {
    private JFrame frame;
    private JPanel pane;
    private JPanel buttonsPane;
    private CustomShape customShape;
    private JButton squareButton;
    private JButton circleButton;
    private JButton purpleButton;
    private JButton blueButton;

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> new PaintExample().createAndShowGUI());
    }

    private void createAndShowGUI() {
        frame = new JFrame(getClass().getSimpleName()); //Create a new JFrame with a title = this class name
        pane = new JPanel();
        buttonsPane = new JPanel();

        buttonsPane.setLayout(new GridLayout(2, 2, 5, 5)); // We generate a grid layout of 2 x 2 for our JButtons

        squareButton = new JButton("Square");
        circleButton = new JButton("Circle");
        purpleButton = new JButton("Purple");
        blueButton = new JButton("Blue");

        squareButton.addActionListener(listener);
        circleButton.addActionListener(listener);
        purpleButton.addActionListener(listener);
        blueButton.addActionListener(listener);

        buttonsPane.add(squareButton);
        buttonsPane.add(circleButton);
        buttonsPane.add(purpleButton);
        buttonsPane.add(blueButton);

        customShape = new CustomShape(); // We create an instance of our custom JPanel class

        pane.setLayout(new BorderLayout());
        pane.add(customShape);
        pane.add(buttonsPane, BorderLayout.SOUTH);

        frame.add(pane);
        frame.pack();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }

    ActionListener listener = e -> { //Java 8+, for Java 7- add the actionPerformed method instead of the lambda expression
        // We check which button was clicked and set the shape / color for our custom class
        if (e.getSource().equals(squareButton)) {
            customShape.setShape(ShapeToDraw.SQUARE);
        } else if (e.getSource().equals(circleButton)) {
            customShape.setShape(ShapeToDraw.CIRCLE);
        } else if (e.getSource().equals(purpleButton)) {
            customShape.setColor(Color.MAGENTA);
        } else if (e.getSource().equals(blueButton)) {
            customShape.setColor(Color.BLUE);
        } 
    };

    enum ShapeToDraw {
        SQUARE, CIRCLE // You can define here other properties for each enum option
    }

    class CustomShape extends JPanel {
        private Color color;
        private ShapeToDraw shape;

        public CustomShape() {

        }

        public Color getColor() {
            return color;
        }

        public void setColor(Color color) {
            this.color = color;
            this.repaint(); // Everytime we set the color we ask the component to repaint itself
        }

        public ShapeToDraw getShape() {
            return shape;
        }

        public void setShape(ShapeToDraw shape) {
            this.shape = shape;
            this.repaint(); // Everytime we set the shape we ask the component to repaint itself
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(200, 200); // We define the panel's size
        }

        @Override
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g;
            g2d.setColor(color != null ? color : Color.BLACK); //If we haven't set the Color yet, we default it to black, otherwise we set the color to the one chosen by the user.
            if (shape == ShapeToDraw.SQUARE) { //If the shape is a square, we draw a square
                g2d.draw(new Rectangle2D.Double(50, 50, 100, 100)); // Change the coordinates that you get by user click using the MouseListener
            } else if (shape == ShapeToDraw.CIRCLE) { // Or we draw a circle
                g2d.draw(new Ellipse2D.Double(50, 50, 100, 100));
            } 
        }
    }
}

这是程序的样子:

在此处输入图像描述 在此处输入图像描述

我不确定如何记录用户按钮单击的坐标,然后将这些坐标传递给所需形状的构造函数。

要获取相对于您的窗口的坐标,请参阅:如何获取鼠标点击相对于摆动窗口的位置

于 2019-05-30T21:20:10.620 回答
2

以下是一个建议的实现,还包含了您从 Frakcool 获得的良好指导

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.Point;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Rectangle2D;
import java.util.HashMap;
import java.util.Map;
import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JToggleButton;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;

public class GUI
{
    private final ButtonGroup colorGroup; //group  buttons
    private final ButtonGroup shapeGroup; //so only one can be selected at any given time
    private final Map<String, Color> colors; //map colors to it names.
    private Color color; // color for painting
    private Shape shape; //shape to paint
    private JFrame frame;
    private JPanel buttonsPane;
    private JTextArea textArea;

    private static final int ROWS = 2, COLS = 3;
    private static final String[] BUTTONS_LABELS = {"Rectangle", "Circle", "Exit"};

    public GUI()
    {
        shapeGroup = new ButtonGroup(); colorGroup = new ButtonGroup();
        colors = new HashMap<>();
        colors.put("Red", Color.RED); colors.put("Green", Color.GREEN); colors.put("Blue", Color.BLUE);
    }

    private void createAndShowGUI(String title) {
        frame = new JFrame(title);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //use a GridLayout for the buttons pane
        buttonsPane = new JPanel();
        buttonsPane.setLayout(new GridLayout(ROWS, COLS));
        frame.getContentPane().add(buttonsPane, BorderLayout.CENTER);//each BorderLayout position can hold ONE component

        for(String colorName : colors.keySet()){
            JToggleButton button = makeButton(colorName);
            buttonsPane.add(button);
            colorGroup.add(button);
            button.addActionListener(changeColorAction());
        }

        for(String text : BUTTONS_LABELS){
            JToggleButton button = makeButton(text);
            buttonsPane.add(button);
            shapeGroup.add(button);
            button.addActionListener(changeShapeAction());
        }

        setDefaults();

        frame.getContentPane().add(new Draw(), BorderLayout.WEST);
        textArea = new JTextArea(0,20);
        frame.getContentPane().add(textArea, BorderLayout.EAST);

        frame.pack();
        frame.setVisible(true);
    }

    private JToggleButton makeButton(String text) {
        JToggleButton b = new JToggleButton(text); //use toggle buttons
        b.setHorizontalAlignment(SwingConstants.LEFT);
        b.setPreferredSize(new Dimension(100, 80)); //set preferred and let Layout manager do its work
        return b;
    }

    private ActionListener changeColorAction() {
        return e->{
            color = colors.get(((JToggleButton)e.getSource()).getText());
            frame.repaint();
        };
    }

    private ActionListener changeShapeAction() {
        return e->{
            switch (((JToggleButton)e.getSource()).getText()){

                case "Circle":
                    shape = Shape.CIRCLE;
                    break;
                case "Rectangle":
                    shape = Shape.RECTANGLE;
                    break;
                default: System.exit(0);
            }

            frame.repaint();
        };
    }

    private void setDefaults() {
        colorGroup.getElements().nextElement().setSelected(true);
        color = colors.get(colorGroup.getElements().nextElement().getText());
        shapeGroup.getElements().nextElement().setSelected(true);
        shape = Shape.RECTANGLE;
    }

      public static void main(String[] args) {
            SwingUtilities.invokeLater(() -> new GUI().createAndShowGUI("My Gui"));
        }

    class Draw extends JPanel{

        private final Point[] points; // an array to hold clicked points
        private int mouseClicks = 0;
        private static final int POINT_SIZE = 2;

        Draw(){
            setLayout(new BorderLayout());
            setBackground(Color.WHITE);
            setPreferredSize(new Dimension(200, 200));

            JLabel help = new JLabel("Click 2 points to draw");
            help.setHorizontalAlignment(SwingConstants.CENTER);
            add(help, BorderLayout.PAGE_START);

            JLabel timeLabel = new JLabel("current time here");
            timeLabel.setHorizontalAlignment(SwingConstants.LEFT);
            add(timeLabel, BorderLayout.PAGE_END);

            points = new Point[2];
            addMouseListener(new MouseAdapter(){
                @Override
                public void mouseClicked(MouseEvent e) {
                    addPoint(e.getX(), +e.getY() );
                }
            });
        }

        @Override
        public void paintComponent(Graphics g){
            super.paintComponent(g);
            g.setColor(color);
            for(Point point : points)
                if(point != null){
                    g.drawOval(point.x, point.y, POINT_SIZE, POINT_SIZE);
                }

            drawShape((Graphics2D)g);
        }

        private void addPoint(int x, int y) {
            if(mouseClicks ==2){
                mouseClicks = 0;
                points[1] = null;
            }

            points[mouseClicks++] = new Point(x, y);
            repaint();
        }

        private void drawShape(Graphics2D g2d) {

            if(points[0] == null ||  points[1] == null) return;
            if(shape == Shape.RECTANGLE) {
                drawRectangle(g2d);
            }else{
                drawCircle(g2d);
            }
        }

        private void drawRectangle(Graphics2D g2D) {

            int minX = Math.min(points[0].x, points[1].x);
            int minY = Math.min(points[0].y, points[1].y);
            int maxX = Math.max(points[0].x, points[1].x);
            int maxY = Math.max(points[0].y, points[1].y);
            int width  = maxX - minX;
            int height = maxY - minY;
            Rectangle2D.Double rectangle = new Rectangle2D.Double(minX, minY, width, height);
            g2D.draw(rectangle);
        }

        private void drawCircle(Graphics2D g2D) {

            int minX = Math.min(points[0].x, points[1].x);
            int minY = Math.min(points[0].y, points[1].y);
            int maxX = Math.max(points[0].x, points[1].x);
            int maxY = Math.max(points[0].y, points[1].y);
            double dx  = maxX - minX;
            double dy = maxY - minY;
            double radius =  Math.sqrt(dx*dx + dy*dy)/2;
            double centerX = minX + dx/2;
            double centerY = minY + dy/2;

            g2D.draw(new Ellipse2D.Double(centerX - radius , centerY - radius, 2* radius, 2* radius));
        }
    }

    enum Shape {
        RECTANGLE, CIRCLE
    }
}

在此处输入图像描述

于 2019-05-31T05:55:31.773 回答