简体   繁体   English

使用鼠标光标移动对象-Java

[英]Move object with mouse cursor - Java

I have to do the Agario game using object oriented programming with Java. 我必须使用面向对象的Java编程来做Agario游戏。 So far I've draw a circle and I was able to make it follow my cursor. 到目前为止,我已经画了一个圆,并且能够使它跟随我的光标。 However I have many bugs like the ball not following the cursor after the cursor touches the ball, sometimes, the ball moves to quickly, other times it moves to slowly. 但是,我有很多错误,例如在光标接触到球之后,球没有跟随光标移动,有时球会快速移动,而其他时候会缓慢移动。

Overall I think it's far from perfect and would like some suggestions. 总的来说,我认为这还远远不够完美,并希望提出一些建议。 I just have to make it follow and I'm struggling a bit. 我只需要使其遵循就行了,我有点挣扎。 Here's the code: 这是代码:

window class: 窗口类:

public class window extends JFrame implements Runnable {

  Ball b = new Ball();
  Thread t = new Thread(this);

  public window () {
    setSize(600, 600);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setResizable(true);
    setLocationRelativeTo(null);
    setVisible(true);
    t.start();

    addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
        public void mouseMoved(java.awt.event.MouseEvent evt) {
            formMouseMoved(evt);
        }

    });

  }

  private void formMouseMoved(MouseEvent evt) {
    b.setnewtarget(evt.getX(), evt.getY());
  }

  @Override
  public void paint(Graphics g) {
    super.paint(g); //To change body of generated methods, choose Tools | Templates.
    b.drawCenteredCircle(g);
  }

  public static void main(String[] args) {
    new window ();
  }

  @Override
  public void run() {
    while (true) {
        repaint();
    }
  }

} }

Here´s the Ball class: 这是Ball类:

public class Ball implements Runnable {

  Thread t = new Thread(this);
  int x, y, r;
  int targetx, targety;

  public Ball() {
    x = 300;
    y = 300;
    r = 35;
    t.start();
  }

  public void drawCenteredCircle(Graphics g) {
    g.fillOval(x, y, r, r);
    g.drawLine(x, y, targetx, targety);
  }

  @Override
  public void run() {
    while (true) 

         int dx = Math.abs(targetx-x);
         int dy = Math.abs(targety-x);

         if (dx > dy) {
            int oldx = x;
            if (x > targetx)
               x--;
            else
               x++;

            y =((targety-y)/(targetx-x))*(x-oldx)+y;
         } else {
            int oldy = y;
            if (y > targety)
               y--;
            else
               y++;

            x =((targetx-x)/(targety-y))*(y-oldy)+x;
         }

        try {
            t.sleep(10);
        } catch (InterruptedException ex) {
            Logger.getLogger(Bola.class.getName()).log(Level.SEVERE, null, ex);
        }

    }
  }

  void setnewtarget(int x, int y) {
    targetx = x;
    targety = y;
  }
}

Thanks :D 感谢:D

  1. There is no need for the Thread. 不需要线程。 You should NOT be using the infinite while loop. 您不应该使用无限的while循环。 Get rid of that code 摆脱那个代码

  2. In the formMouseMoved(...) moved method you invoked repaint() which will cause the component to paint itself. formMouseMoved(...)移动的方法中,您调用了repaint(),这将导致组件自行绘制。

  3. Also, get rid of the Thread and while loop in your Ball class. 另外,在Ball类中摆脱Thread和while循环。

The key point is that Swing is event driven. 关键是Swing是事件驱动的。 The mouseMoved(...) event is all you need for the animation. 动画只需要mouseMoved(...)事件。 Every time you drag the mouse an event will be generated which will cause you to repaint the Ball. 每次拖动鼠标时,都会生成一个事件,使您重新绘制Ball。

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

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