我想在一段時間延遲後在生成的座標上繪製圖像,但我得到了nullpointerexception。我試圖讓我的形象從一個點移動到另一個點,並且有明顯的時間延遲。帶時間延遲的繪圖圖像
public class PaintStations extends JPanel implements Runnable {
private static final long serialVersionUID = 6734649580151111907L;
private Thread thread;
public PaintStations() {
setSize(new Dimension(MainWindow.WIDTH, MainWindow.HEIGHT));
setOpaque(false);
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
}
private void plot(Graphics g, int x, int y) {
Graphics2D g2d = (Graphics2D) g;
Image img = Toolkit.getDefaultToolkit().getImage("cpn.png");
g2d.drawImage(img, x, y, this);
}
private void drawLine(Graphics g, int x1, int y1, int x2, int y2) {
int d = 0;
int dy = Math.abs(y2 - y1);
int dx = Math.abs(x2 - x1);
int dy2 = (dy << 1);
int dx2 = (dx << 1);
int ix = x1 < x2 ? 1 : -1;
int iy = y1 < y2 ? 1 : -1;
if (dy <= dx) {
for (;;) {
plot(g, x1, y1);
if (x1 == x2)
break;
x1 += ix;
d += dy2;
if (d > dx) {
y1 += iy;
d -= dx2;
}
}
} else {
for (;;) {
plot(g, x1, y1);
if (y1 == y2)
break;
y1 += iy;
d += dx2;
if (d > dy) {
x1 += ix;
d -= dy2;
}
}
}
}
@Override
public void run() {
drawLine(getGraphics(), 0, 0, 10, 10);
}
public void start() {
thread = new Thread(this);
thread.start();
}
}
這裏是錯誤,我得到:
Exception in thread "Thread-1" java.lang.NullPointerException
at PaintStations.plot(PaintStations.java:27)
at PaintStations.drawLine(PaintStations.java:44)
at PaintStations.run(PaintStations.java:71)
at java.lang.Thread.run(Unknown Source)
我測試將圖像隨機COORDS但repaint();
無法正常工作。在generateFuelStations()
結束後,似乎paintComponent()
方法被觸發,並且在屏幕上只在最後生成的座標上出現一個圖像。
public class PaintStations extends JPanel implements Runnable {
private static final long serialVersionUID = 6734649580151111907L;
private ArrayList<Point> stationsLocation;
private Shape region;
private int x;
private int y;
private int stationsNumber;
public PaintStations(Shape region, int stationsNumber) {
setSize(new Dimension(MainWindow.WIDTH, MainWindow.HEIGHT));
setOpaque(false);
this.region = region;
this.stationsNumber = stationsNumber;
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
System.out.println("Repaint!");
Graphics2D g2d = (Graphics2D) g;
Image img = Toolkit.getDefaultToolkit().getImage("cpn.png");
g2d.drawImage(img, x, y, this);
}
private void generateFuelStations(int stationsNumber) {
Rectangle r = region.getBounds();
this.stationsLocation = new ArrayList<>();
for(int i=0; i<stationsNumber; i++) {
Random rand = new Random();
do {
x = (int) (r.getX() + rand.nextInt((int) r.getWidth()));
y = (int) (r.getY() + rand.nextInt((int) r.getHeight()));
} while(!region.contains(x,y));
System.out.println("X: " + x + " Y: " + y);
stationsLocation.add(new Point(x,y));
repaint();
}
}
@Override
public void run() {
generateFuelStations(stationsNumber);
}
}
@Tavo,謝謝... – camickr