以下程序很简单,应该画一个落到重力影响的球然后反弹回来。该程序似乎编译但我不断收到异常错误。
在类球中,功能移动包含用于计算速度和位移的物理公式。
包装重力;
import java.awt.Color;
import java.awt.Graphics;
public class Ball extends StartingPoint{
/**
*
*/
private static final long serialVersionUID = 1L;
public int x;
public int y;
private static final double dt = .2;
private static final double g = 9.8;
public double vy = 0;
Ball(int x, int y){
this.x = x;
this.y = y;
}
public void move(){
vy += g*dt;
y += (1/2)*g * dt*dt + vy * dt;
if (y >= (this.getHeight() - 20 -1)){
y = this.getHeight() - 20 -1;
vy = -vy;}
}
public void paintBALL(Graphics g){
g.setColor(Color.LIGHT_GRAY);
g.fillOval(x, y, 20, 20);
}
}
package gravity;
import java.applet.Applet;
import java.awt.Graphics;
public class StartingPoint extends Applet {
/**
*
*/
private static final long serialVersionUID = 1L;
public int x = 400, y = 25;
Ball ball;
//private static final int nbBalls = 1;
@Override
public void init() {
setSize(800, 600);
}
@Override
public void start() {
Ball ball = new Ball(x,y);
Thread thread = new Thread(new RunBall(ball));
thread.start();
}
public class RunBall implements Runnable{
private Ball ball;
RunBall(Ball ball){
this.ball = ball;
}
@Override
public void run() {
while(true){
ball.move();
try {
Thread.sleep(17);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
repaint();
}
}
}
@Override
public void stop() {
}
@Override
public void paint(Graphics g) {
ball.paintBALL(g);
}
}
编译器不断添加以下exeption:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at gravity.StartingPoint.paint(StartingPoint.java:61)
at java.awt.Container.update(Container.java:1835)
at sun.awt.RepaintArea.updateComponent(RepaintArea.java:267)
at sun.awt.RepaintArea.paint(RepaintArea.java:233)
at apple.awt.ComponentModel.handleEvent(ComponentModel.java:263)
at java.awt.Component.dispatchEventImpl(Component.java:4852)
at java.awt.Container.dispatchEventImpl(Container.java:2142)
at java.awt.Component.dispatchEvent(Component.java:4604)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:717)
at java.awt.EventQueue.access$400(EventQueue.java:82)
at java.awt.EventQueue$2.run(EventQueue.java:676)
at java.awt.EventQueue$2.run(EventQueue.java:674)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:86)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:690)
at java.awt.EventQueue$3.run(EventQueue.java:688)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:86)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:687)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:296)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:211)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:196)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:188)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
答案 0 :(得分:0)
在start
方法中,您已宣布Ball
...
Ball ball = new Ball(x,y);
这意味着在调用ball
时,实例字段null
仍为paint
。
相反,在start
方法中,初始化实例字段...
ball = new Ball(x,y);
Applet
已经过时了,因为小程序重新绘制,你应该为很多闪烁做好准备。
如果可以,我建议使用JApplet
,但使用JPanel
作为主要油漆表面。通过覆盖JPanel
paintComponent
方法,您可以默认获得双缓冲支持,这样可以在不进行任何其他工作的情况下停止闪烁,只是不要忘记拨打super.paintComponent
在做任何自定义绘画之前...
答案 1 :(得分:0)
paint
,只需更改颜色:
@Override
public void paint(Graphics g) {
if(ball != null)
ball.paintBALL(g);
}