如何将椭圆移动到鼠标点击的坐标?

How to move an Oval to the coordinates of the mouseClick?

我正在尝试在 Java OOP 中构建一个小程序,它将显示一个椭圆,该椭圆将其位置更改为鼠标单击坐标。但是,由于我是初学者,我不知道如何完成它。

有人知道我的程序出了什么问题吗?

public class OvalWindow {

 JFrame window;
 int windowWidth = 500;
 int windowHeight = 500;

 OvalPanel panel;

   public void run() {
      //Set a window
      window = new JFrame("Oval moving");
      window.setVisible(true);
      window.setSize(windowWidth, windowHeight);
      window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);    

      //Create a panel and add it to the window
      panel = new OvalPanel();
      window.setContentPane(panel);

      //Change the location on mouse input
      panel.addMouseListener(new OvalMouseClick(panel));


   }

  public static void main(String[] args) {
    OvalWindow oWndow = new OvalWindow();
    oWndow.run();
  }

}

文件:Oval.java 创建椭圆 class

 public class Oval {

        private int diameter;
        private int x;
        private int y;
        private Color color;

        public Oval(int x, int y, int diameter, Color color) {
            this.x = x;
            this.y = y;
            this.diameter = diameter;
            this.color = color;
        }

        public int getX() {
            return x;
        }
        public int getY() {
            return y;
        }
        public int getDiameter() {
            return diameter;
        }
        public Color getColor() {
            return color;
        }

        /*
         * Draw the oval 
         */
        public void draw(Graphics g) {
            g.fillOval(x, y, diameter, diameter);
            g.setColor(color);

        }
}

文件:OvalPanel.java 这 class 将对象椭圆添加到屏幕上。 此外,设置椭圆的初始位置

    public class OvalPanel extends JPanel{
     Oval oval;

     public void addOval(Oval oval) {
     oval = new Oval(100, 100, 50, Color.BLACK);
     this.repaint();

    }
    public void paintComponent(Graphics g){
        oval.draw(g);
    }


  }

文件:OvalMouseClick.java 这 class 在屏幕上添加一个新的椭圆,在与鼠标点击坐标相同的位置。

    public class OvalMouseClick extends MouseAdapter{
        OvalPanel panel;

        public OvalMouseClick(OvalPanel panel) {
            this.panel = panel;
        }


        @Override
        public void mouseClicked(MouseEvent e) {
            panel.addOval(new Oval(e.getX(), e.getY(), 50, Color.BLUE));

        }

    }

编写自定义 paintComponent() 方法使您的 OvalPanel 实际显示某些内容。如果您向其中添加组件,它们会自动显示,但您的 Oval class 不是组件(在 UI 意义上),因此您需要明确地将代码写入显示它们。

@Override
public void paintComponent(Graphics g) {
    for(Oval o : ovals)
        o.draw(g);
}

我还建议阅读 https://docs.oracle.com/javase/tutorial/uiswing/ 上的 Swing 踪迹,因为您不能只 "guess" 事情是如何运作的。 Swing 很复杂,您需要了解它的作用。然后你会意识到 draw(Graphics g)paintComponent(Graphics g) 不仅仅是具有不同名称的类似方法。

万一有人在寻找答案。这是我的解决方案:

我修改了文件:OvalWindow.java

        public class OvalWindow {

            JFrame window;
            OvalPanel panel;

            int windowWidth = 500;
            int windowHeight = 500;

            /**
             * Run the program
             */
            public void run() {
                // Set a window
                window = new JFrame("Oval moving");
                window.setVisible(true);
                window.setSize(windowWidth, windowHeight);
                window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

                // Create a panel and add it to the window
                panel = new OvalPanel();
                window.add(panel, BorderLayout.CENTER);

                panel.addMouseListener(new MouseAdapter() {
                    public void mouseClicked(MouseEvent e) {
                        panel.moveToCoord(e);
                    }
                });

            }

            public static void main(String[] args) {
                OvalWindow oWndow = new OvalWindow();
                oWndow.run();
            }

        }

我也对文件进行了重大修改:OvalPanel.java

        public class OvalPanel extends JPanel {
            Oval oval;
            int x = 50;
            int y = 10;
            int diameter = 50;
            Color color = Color.BLUE;

            public void moveToCoord(MouseEvent e) {
                x = e.getX() - diameter / 2;
                y = e.getY() - diameter / 2;
                repaint();

            }

            @Override
            public void paintComponent(Graphics g) {
                super.paintComponent(g);
                oval = new Oval(x, y, diameter, color);
                oval.draw(g);

            }

        }

在文件上:Oval.java 只是一个小改动:

 public void draw(Graphics g) {
    g.setColor(color);
    g.fillOval(x, y, diameter, diameter);

}

文件:OvalMouseClick.java 已删除。