为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

12345678_0001 2021-08-04 09:57:22
我正在尝试在 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 创建一个 Oval 类 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 此类向屏幕添加对象椭圆。另外,设置椭圆的初始位置    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);    }  }
查看完整描述

2 回答

  • 2 回答
  • 0 关注
  • 191 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信