我正在尝试从Agent,Obstacle和MovingEntity类调用draw()方法,但出现此错误:(我只有Obstacle类有错误)
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at java.awt.Graphics.drawPolygon(Graphics.java:732)
at Obstacle.draw(Obstacle.java:28)
at World.draw(World.java:121)
at MyWorldWindow.paint(MyWorldWindow.java:23)
at javax.swing.RepaintManager$3.run(RepaintManager.java:819)
at javax.swing.RepaintManager$3.run(RepaintManager.java:796)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:796)
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:769)
at javax.swing.RepaintManager.prePaintDirtyRegions(RepaintManager.java:718)
at javax.swing.RepaintManager.access$1100(RepaintManager.java:62)
at javax.swing.RepaintManager$ProcessingRunnable.run(RepaintManager.java:1677)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:733)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:694)
at java.awt.EventQueue$3.run(EventQueue.java:692)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:703)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)
world.java:
import java.util.*;
import java.awt.Color;
public class World {
int W,H;
int margin;
int N;
LinkedList entities;
public World() {
W = 800;
H = 600;
margin = 50;
entities = new LinkedList();
N = 10;
for(int i=0; i<N; i++) {
Agent e = new Agent ( randomPointInsideWorld(), randomPointInsideWorld(), this );
entities.add( e );
}
for(int i=0; i<4; i++) {
Obstacle o = new Obstacle ( randomPointInsideWorld(), this );
entities.add( o );
}
for(int i=0; i<3; i++) {
MovingEntity me = new MovingEntity ( randomPointInsideWorld(), this );
entities.add( me );
}
}
int getW() { return W; }
int getH() { return H; }
Vec2D randomPointInsideWorld() {
double x = margin + Math.random() * (W - 2*margin);
double y = margin + Math.random() * (H - 2*margin);
Vec2D p = new Vec2D(x,y);
return p;
}
void processCollisions() {
for(int i=0; i<N; i++) {
Entity ei = (Entity) entities.get(i);
ei.setCollides(false);
}
for(int i=0; i<N; i++) {
Entity ei = (Entity) entities.get(i);
ei.setCollides(false);
for(int j=0; j<N; j++) {
if(i == j) continue;
Entity ej = (Entity) entities.get(j);
if(ei.collides(ej)) {
ei.setCollides(true);
}
}
}
}
void removeOutsiders() {
for(int i=0; i<entities.size(); i++) {
Entity ei = (Entity) entities.get(i);
if((ei.getPos().getX() < 0) ||
(ei.getPos().getX() > W) ||
(ei.getPos().getY() < 0) ||
(ei.getPos().getY() > H))
{
entities.remove(ei);
}
}
N = entities.size();
}
void update() {
removeOutsiders();
processCollisions();
for(int i=0; i<N; i++) {
Entity ei = (Entity) entities.get(i);
ei.update();
if ( ei instanceof Agent ) {
Agent a = (Agent) ei;
if ( a.objReached() ) {
a.setObj(randomPointInsideWorld() );
}
}
}
}
public void run(int steps) {
for(int i = 0; i<steps; i++) update();
}
public void draw( java.awt.Graphics g ) {
N = entities.size();
for(int i=0; i<N; i++) {
Entity ei = (Entity) entities.get(i);
Vec2D p = ei.getPos();
double wa = 2;
g.setColor(Color.BLUE);
g.fillOval((int)(p.getX() - wa), (int)(p.getY() - wa), 2*(int)wa, 2*(int)wa);
if ( ei instanceof Agent ) {
Agent a = (Agent) ei;
a.draw(g);
}
if ( ei instanceof MovingEntity ) {
MovingEntity me = (MovingEntity) ei;
me.draw(g);
}
if ( ei instanceof Obstacle ) {
Obstacle obst = (Obstacle) ei;
obst.draw(g);
}
}
}
}
仅当我调用Obstacle类的draw方法时才会出现错误
障碍类别:
import java.awt.Graphics;
import java.awt.Color;
import java.awt.Polygon;
// Clase Obstacle Hereda atributos de la Clase Entity
public class Obstacle extends Entity {
private MyPolygon p;
// Método Constructor Obstacle
public Obstacle (Vec2D p, World wi) {
super (p, wi);
MyPolygon p1 = new MyPolygon();
p1.randomPolygon();
p1.center();
p1.translate((int)pos.getX(), (int)pos.getY());
}
// Getter Obstacle
public MyPolygon getObstacle() {
return p;
}
public void draw(Graphics g) {
if(bCollides)g.setColor(Color.RED);
else g.setColor(Color.ORANGE);
g.drawPolygon(p);
}
}
我是Java的新手,但是我觉得与Obstacle类中的某些值未正确初始化有关,这可能与事实有关吗?
谢谢你们。
最佳答案
更改为
private MyPolygon p = new MyPolygon();