基本上,这样做的目的是使蠕虫在单击“添加蠕虫”按钮时扩展。我试图使一种蠕虫扩展,并最终使用多线程技术允许多个蠕虫在整个屏幕上扩展。目前,我只允许一只蠕虫膨胀就麻烦了。这是以下类(Main,ThreadFrame和DrawThread)的代码:

主要:

public class Main {
public static void main(String[] args) {
    ThreadFrame myFrame = new ThreadFrame();
    myFrame.setSize(new Dimension(640, 480));
    myFrame.setLocationRelativeTo(null);
    myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    myFrame.setTitle("Worms! - Jonathan Perron");
    myFrame.setVisible(true);
    myFrame.setResizable(false);
}
}


ThreadFrame类:

public class ThreadFrame extends JFrame implements ActionListener {

JButton btnNewWorm, btnKillWorm;
JPanel myPanel2 = new JPanel();
ArrayList<DrawThread> worms = new ArrayList<DrawThread>();

public JPanel getMyPanel2(){
    return this.myPanel2;
}

public ThreadFrame() {
    JPanel myPanel = new JPanel();
    btnNewWorm = new JButton("New Worm");
    btnKillWorm = new JButton("Kill Worm");
    myPanel.setBounds(0, 400, 640, 80);
    myPanel.setLayout(new FlowLayout());
    myPanel2.setSize(new Dimension(640, 400));
    myPanel2.setLayout(null);
    myPanel2.setBackground(Color.WHITE);
    btnNewWorm.setBounds(100, 410, 200, 30);
    btnKillWorm.setBounds(340, 410, 200, 30);
    add(btnNewWorm);
    add(btnKillWorm);
    add(myPanel2);
    add(myPanel);
    btnNewWorm.addActionListener(this);
    btnKillWorm.addActionListener(this);
}

public void actionPerformed(ActionEvent AE) {
    if(AE.getSource() == btnNewWorm){
        DrawThread dw = new DrawThread(myPanel2);
        worms.add(dw);
        System.out.println("New worm!");
    }
    if(AE.getSource() == btnKillWorm){
        System.out.println("Kill worm!");
    }
}
}


DrawThread类:

public class DrawThread extends Thread implements Runnable{

Graphics2D g, graph;
private int sleepTime, wormDiameter, hue, saturation, brightness, randomWidth, randomHeight;

public DrawThread(int sleepTime, int wormDiamter, int hue, int saturation, int brightness, int randomWidth, int randomHeight) {
    this.sleepTime = sleepTime;
    this.brightness = brightness;
    this.hue = hue;
    this.saturation = saturation;
    this.randomWidth = randomWidth;
    this.randomHeight = randomHeight;
}

public int getSleepTime(){
    return sleepTime;
}

public void setSleepTime(int sleepTime){
    this.sleepTime = sleepTime;
}
public DrawThread(JPanel myPanel2){

    Random rand = new Random();

    //get panel dimensions
    int panelWidth = myPanel2.getWidth();
    int panelHeight = myPanel2.getHeight();

    //worm location
    randomWidth = rand.nextInt(panelWidth);
    randomHeight = rand.nextInt(panelHeight);

    //worm size
    wormDiameter = rand.nextInt(7)+3;

    //worm color
    hue = rand.nextInt(255);
    saturation = rand.nextInt(255);
    brightness = rand.nextInt(255);
    Color color = new Color(hue, saturation, brightness);

    //sleep
    sleepTime = rand.nextInt(80) + 20;

    //Graphics
    System.out.println(myPanel2);
    g = (Graphics2D) myPanel2.getGraphics();
    g.setColor(color);
    Ellipse2D.Double ellipse2D = new Ellipse2D.Double(randomWidth, randomHeight, wormDiameter, wormDiameter);
    g.fill(ellipse2D);
    Thread thread1 = new Thread(new DrawThread(sleepTime, wormDiameter, hue, saturation, brightness, randomWidth, randomHeight));
    thread1.start();
}

public void run(){
    try {
        while(true) {
            sleep(sleepTime);
            Ellipse2D.Double ellipse2D = new Ellipse2D.Double(randomWidth + 10, randomHeight + 10, wormDiameter, wormDiameter);
            g.fill(ellipse2D); //This is where I receive my error
            System.out.println("ran");
        }
    }
    catch (InterruptedException e) {
        System.out.println("ERROR!");
    }
}

public String toString() {
    String result = "SleepTime: " + sleepTime + "\nWorm Diameter: " + wormDiameter
            + "\nHue: " + hue + "\nSaturation: " + saturation + "\nBrightness: "
            + brightness + "\nWidth: " + randomWidth + "\nHeight: " + randomHeight;
    return result;
}
}


尝试填充图形时,我在DrawThread类中收到错误,给我一个空指针异常。这是确切的错误:

Exception in thread "Thread-4" java.lang.NullPointerException
at Main.DrawThread.run(DrawThread.java:78)
at java.lang.Thread.run(Unknown Source)


如何修复我的代码,以免出现此错误?

最佳答案

实例变量g为空。应该在适当的构造函数中为其赋予一个值。

您已在DrawThread(JPanel)构造函数中进行了设置,但未在其他方法中进行设置。

您可能要考虑这是否应该是实例变量。如果是这样,请在其他构造函数中进行适当设置。

[编辑]您可以将JPanel传递给另一个构造函数,并从中设置g。

public DrawThread(int sleepTime, int wormDiamter, int hue, int saturation, int brightness, int randomWidth, int randomHeight, JPanel panel) {
    //...
    g = (Graphics2D) panel.getGraphics();
    //...
}

10-08 09:18