大多数情况下,这段代码一切都很完美。
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.*;
public class Main {
public static void main(String[] args) {
new Main();
}
private Main() {
JFrame f = new JFrame("Test");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(new Game());
f.setResizable(false);
f.pack();
f.setVisible(true);
}
private class Game extends JPanel implements MouseListener {
private static final long serialVersionUID = -7048656881407382561L;
int x = 300, y = 300;
Image thing;
ImageIcon ico = new ImageIcon("smiley.png");
private Game() {
setPreferredSize(new Dimension(600, 600));
thing = ico.getImage();
addMouseListener(this);
}
private void goTo(int stX, int stY, int endX, int endY) {
int dx = Math.abs(endX - stX);
int dy = Math.abs(endY - stY);
int sx = endX > stX ? 1 : -1;
int sy = endY > stY ? 1 : -1;
int err = dx - dy;
while (true) {
x = stX;
y = stY;
repaint();
validate();
if (stX == endX && stY == endY) break;
if ((err*2) > -dy) {
err -= dy;
stX += sx;
}
if ((err*2) < dx) {
err += dx;
stY += sy;
}
try {
Thread.sleep(10);
} catch (InterruptedException e) {}
}
}
private void goTo(final int endX, final int endY) {
new Thread(new Runnable() {
@Override
public void run() {
goTo(x, y, endX, endY);
}
}).start();
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
((Graphics2D)g).drawImage(thing, x, y, null);
}
@Override
public void mousePressed(MouseEvent e) {
goTo(e.getX(), e.getY());
}
public void mouseReleased(MouseEvent e) {} //These
public void mouseEntered(MouseEvent e) {} //are
public void mouseExited(MouseEvent e) {} //useless
public void mouseClicked(MouseEvent e) {} //methods
}
}
只有一个问题:有时,看似随意,图像只是继续前进,而不是在鼠标点击的地方停止。通常它可以工作,但有时它会搞砸并继续前进。
这是与它有关的图像。
我注意到,如果你在他的眼睛之间点击,它总是会飞走。