Java BounceBall鼠标事件 [英] Java BounceBall mouse event
问题描述
我正在使用下面的示例。
我有困难。
必要时让球逃脱鼠标指针。
找到指针时改变方向。
I am working with the example below. And I have a difficulty. Necessary to make the balls escape the mouse pointer. Change direction when they find the pointer.
我试图将鼠标位置作为参数传递给函数move,但得到一个错误:线程中的异常Thread-2java.lang。 NullPointerException
I tried to pass as a parameter to the mouse position to the function "move" but get an error: "Exception in thread" Thread-2 "java.lang.NullPointerException"
任何人都可以给我一个提示如何做到这一点?
我迷路了。
Can anyone give me a hint how to do this? I'm lost.
代码:
public class SimpleBalls {
private Point mousePoint;
public static void main(String[] args) {
new SimpleBalls();
}
public SimpleBalls() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager
.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException ex) {
} catch (InstantiationException ex) {
} catch (IllegalAccessException ex) {
} catch (UnsupportedLookAndFeelException ex) {
}
JFrame frame = new JFrame("Spot");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
Balls balls = new Balls();
frame.add(balls);
frame.setSize(400, 400);
frame.setVisible(true);
new Thread(new BounceEngine(balls)).start();
}
});
}
public static int random(int maxRange) {
return (int) Math.round((Math.random() * maxRange));
}
public class Balls extends JPanel {
private List<Ball> ballsUp;
public Balls() {
ballsUp = new ArrayList<Ball>(25);
MouseAdapter handler = new MouseAdapter() {
@Override
public void mouseMoved(MouseEvent e) {
mousePoint = e.getPoint();
// System.out.println(mousePoint);
}
@Override
public void mouseExited(MouseEvent e) {
mousePoint = e.getPoint();
}
};
addMouseListener(handler);
addMouseMotionListener(handler);
for (int index = 0; index < 10 + random(10); index++) {
ballsUp.add(new Ball(new Color(random(255), random(255),
random(255))));
}
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
for (Ball ball : ballsUp) {
ball.paint(g2d);
}
g2d.dispose();
}
public List<Ball> getBalls() {
return ballsUp;
}
}
public class BounceEngine implements Runnable {
private Balls parent;
public BounceEngine(Balls parent) {
this.parent = parent;
}
@Override
public void run() {
int width = getParent().getWidth();
int height = getParent().getHeight();
for (Ball ball : getParent().getBalls()) {
int x = random(width);
int y = random(height);
Dimension size = ball.getSize();
if (x + size.width > width) {
x = width - size.width;
}
if (y + size.height > height) {
y = height - size.height;
}
ball.setLocation(new Point(x, y));
}
while (getParent().isVisible()) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
getParent().repaint();
}
});
for (Ball ball : getParent().getBalls()) {
move(ball, mousePoint);
}
try {
Thread.sleep(100);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}
}
public Balls getParent() {
return parent;
}
public void move(Ball ball, Point mouse) {
try {
Point p = ball.getLocation();
Point speed = ball.getSpeed();
Dimension size = ball.getSize();
System.out.println(mouse.x);
int vx = speed.x;
int vy = speed.y;
int x = p.x;
int y = p.y;
if (x + vx < 0 || x + size.width + vx > getParent().getWidth()) {
vx *= -1;
}
if (y + vy < 0 || y + size.height + vy > getParent().getHeight()) {
vy *= -1;
}
x += vx;
y += vy;
ball.setSpeed(new Point(vx, vy));
ball.setLocation(new Point(x, y));
} catch (Exception e) {
e.printStackTrace();
}
}
}
public class Ball {
private Color color;
private Point location;
private Dimension size;
private Point speed;
private int dimeter;
public Ball(Color color) {
Random rnd = new Random();
dimeter = 10 + rnd.nextInt(35);
setColor(color);
speed = new Point(10 - random(20), 10 - random(20));
size = new Dimension(dimeter, dimeter);
}
public Dimension getSize() {
return size;
}
public void setColor(Color color) {
this.color = color;
}
public void setLocation(Point location) {
this.location = location;
}
public Color getColor() {
return color;
}
public Point getLocation() {
return location;
}
public Point getSpeed() {
return speed;
}
public void setSpeed(Point speed) {
this.speed = speed;
}
protected void paint(Graphics2D g2d) {
Point p = getLocation();
if (p != null) {
g2d.setColor(getColor());
Dimension size = getSize();
g2d.fillOval(p.x, p.y, size.width, size.height);
}
}
}
}
谢谢。
编辑:
错误..
Error..
java.lang.NullPointerException
at balls.SimpleBalls$BounceEngine.move(SimpleBalls.java:170)
at balls.SimpleBalls$BounceEngine.run(SimpleBalls.java:146)
at java.lang.Thread.run(Unknown Source)
推荐答案
问题是 mousepoint
可能无法在 move()
被调用时设置。只需检查 move()
方法中是否 null
。它看起来并不像你已经开始添加逃避鼠标指针的代码 - 我猜你可以自己处理这个部分。
The problem is mousepoint
may not be set by the time move()
is called. Just check to see if it is null
in your move()
method. It doesn't look like you've started to add the code to "escape the mouse pointer" -- I'm guessing you can handle figuring that part out on your own.
public void move(Ball ball, Point mouse) {
Point p = ball.getLocation();
Point speed = ball.getSpeed();
Dimension size = ball.getSize();
if (mouse != null) {
System.out.println(mouse.x); // here
}
int vx = speed.x;
int vy = speed.y;
// ...
这篇关于Java BounceBall鼠标事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!