Java:平行移动球

时间:2014-10-10 11:22:54

标签: java applet

我在java中创建了一个程序,它应该创建10个balles并且随机移动,ball-Threads应该并行运行,不幸的是程序无法编译。有谁知道为什么? 我为每个线程收到以下错误:

package movement;

import java.applet.Applet;
import java.awt.*;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Random;



public class Ball {
    int x;
    int y;
    int radius = 10;
    public Graphics g;
    int dx, dy, n = 0;

    Ball(int x, int y) {
        this.x = x;
        this.y = y;
    }

    public void move() {
        Random randomX = new Random();
        Random randomY = new Random();
        Random randomN = new Random();
        paint();

        try {
            Thread.sleep(10);
        } catch (InterruptedException e) {
            //TODO Auto-generated catch block
            e.printStackTrace();
        }

        x = x + dx;
        y = y + dy;
        dx = (int) (Math.pow(-1, n) * randomX.nextInt(5));
        dy = (int) (Math.pow(-1, n) * randomY.nextInt(5));
        n = randomN.nextInt(10) + 1;
    }

    public void paint() {
        g.setColor(Color.black);
        g.fillOval(x + 100, y + 100, radius, radius);
    }

}

package movement;


class BallMovement implements Runnable {
    private final Ball ball;

    BallMovement(Ball ball) {
        this.ball = ball;
    }

    @Override
    public void run() {
        while (true) {
            ball.move();
            try {
                Thread.sleep(20);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

}

package movement;

        import java.applet.Applet;
        import java.awt.Color;
        import java.awt.Graphics;
        import java.util.Iterator;
        import java.util.List;
        import java.util.ArrayList;
        import java.util.Random;

public class StartingPoint extends Applet {

    public int startX, startY;
    private static final int nbBalles = 10;
    private static final long serialVersionUID = 1L;
    ArrayList<Ball> BallList = new ArrayList<Ball>();

    @Override
    public void init() {
        for (int i = 0; i < nbBalles; i++) {
            Random randX = new Random();
            Random randY = new Random();
            startX = randX.nextInt(500) + 1;
            startY = randY.nextInt(500) + 1;
            BallList.add(new Ball(startX, startY));
        }
    }

    @Override
    public void start() {
        Iterator<Ball> it = BallList.iterator();
        while (it.hasNext()) {
            Ball ball = it.next();
            Thread thread = new Thread(new BallMovement(ball));
            thread.start();
        }
    }

    @Override
    public void stop() {
        // TODO Auto-generated method stub
    }
}

例外:

Exception in thread "Thread-5" 
java.lang.NullPointerException at movement.Ball.paint(Ball.java:40) 
at movement.Ball.move(Ball.java:23) 
at movement.BallMovement.run(BallMovement.java:13) 
at java.lang.Thread.run(Thread.java:695) 

2 个答案:

答案 0 :(得分:1)

您应该从Graphics的{​​{1}}方法中获取void paint(Graphics g)对象。

Applet

并在Ball中,将public class StartingPoint extends Applet { public int startX, startY; private static final int nbBalles = 10; private static final long serialVersionUID = 1L; ArrayList<Ball> BallList = new ArrayList<Ball>(); ... @Override public void paint(Graphics g) { for(Ball ball : BallList) { ball.paint(g); } } } 方法更改为paint()

并且不要从paint(Graphics g)致电paint(),并且move()课程中没有Graphics变量。

答案 1 :(得分:0)

我认为问题出在这个领域:

public Graphics g;

在此方法中使用。

public void paint() {
    g.setColor(Color.black);
    g.fillOval(x + 100, y + 100, radius, radius);
}

您正在引用的g字段未初始化。

您可以通过将球构造函数更改为

来修复它
Ball(int x, int y, Graphics g) {
    this.x = x;
    this.y = y;
    this.g = g;
}

然后在初始化球对象时,从applet传递图形对象:

BallList.add(new Ball(startX, startY, getGraphics()));
相关问题