为什么我收到此错误消息?

时间:2014-08-31 01:16:06

标签: java

Exception in thread "main" java.lang.IllegalArgumentException: n must be positive
at java.util.Random.nextInt(Unknown Source)
at net.survival_instincts.src.main.screenClass.<init>(screenClass.java:21)
at net.survival_instincts.src.main.windowClass.<init>(windowClass.java:28)
at net.survival_instincts.src.main.windowClass.main(windowClass.java:104)

CODE:

windowClass:

package net.survival_instincts.src.main;

import java.awt.Canvas;
import java.awt.Graphics; 
import java.awt.image.BufferedImage;

import javax.swing.JFrame;

import net.survival_instincts.src.graphics.renderClass;

import java.awt.image.*;

public class windowClass extends Canvas implements Runnable {

public static int WIDTH = 854;
public static int HEIGHT = 480;

private Thread thread;
private screenClass screen;
private BufferedImage img;
private boolean running = false;
private renderClass render;
private int[] pixels;

public windowClass() {

    screen = new screenClass(WIDTH, HEIGHT);
    img = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
    pixels = ((DataBufferInt) img.getRaster().getDataBuffer()).getData();
}

private void start() {

    if (running)
        return;
    running = true;
    thread = new Thread(this);
    thread.start();

    System.out.println("Starting...");

}

@SuppressWarnings("unused")
private void stop() {

    if (!running)
        return;
    running = false;
    try {
        thread.join();
    } catch (Exception e) {

        e.printStackTrace();
        System.exit(0);

    }

}

public void run() {

    while (running) {
        tick();
        render();

    }

}

private void tick() {

}

private void render() {

    BufferStrategy bs = this.getBufferStrategy();
    if (bs == null) {

        createBufferStrategy(3);
        return;

    }

    screen.render();

    for (int i = 0; i < WIDTH * HEIGHT; i++) {

        pixels[i] = screen.pixels[i];

    }

    Graphics g = bs.getDrawGraphics();
    g.drawImage(img, 0 , 0, WIDTH , HEIGHT, null);
    g.dispose();
    bs.show();

}

// Generates The Main Window
public static void main(String[] args) {

    windowClass game = new windowClass();

    JFrame frame = new JFrame();

    frame.pack();
    frame.setTitle("Survival Instincts v.0.01");
    frame.setSize(WIDTH, HEIGHT);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.add(game);
    frame.setLocationRelativeTo(null);
    frame.setResizable(true);
    frame.setVisible(true);

    System.out.println("Initializing Startup...");

    game.start();

}

public renderClass getRender() {
    return render;
}

public void setRender(renderClass render) {
    this.render = render;
}

}

renderClass

package net.survival_instincts.src.graphics;

public class renderClass {


public static int width;
public static int height;
public final int[] pixels;

public renderClass(int width, int height) {

    renderClass.width = width;
    renderClass.height = height;
    pixels = new int[width * height];

}

public void draw(renderClass render, int xOffset, int yOffset) {

    for (int y = 0; y < renderClass.height; y++) {

        int yPix = y + yOffset;


        for (int x = 0; x < renderClass.width; x++) {

            int xPix = x + xOffset;

            pixels[xPix + yPix * width] = render.pixels[x + y * renderClass.width];
        }

    }

}
}

screenClass:

  package net.survival_instincts.src.main;

   import java.util.Random;

   import net.survival_instincts.src.graphics.renderClass;

   public class screenClass extends renderClass {

    public renderClass test;

    public screenClass(int width, int height){

        super(width, height);

        Random random = new Random();

        test = new renderClass(256, 256);

        for (int i = 0; i <65536; i++){

            test.pixels[i] = random.nextInt(i);

        }

    }

    public void render(){

        draw(test, width, height);

    }

    }

2 个答案:

答案 0 :(得分:4)

您收到此错误是因为Random.nextInt(0)会引发异常。传递给nextInt()的值必须为正数。

要解决此问题,您需要在i = 1开始循环。

答案 1 :(得分:0)

Random.nextInt()需要一个积极的数字,在这个for loop中你用0调用它:

for (int i = 0; i <65536; i++){
     test.pixels[i] = random.nextInt(i); // it will call random.nextInt(0)
}