Java-IndexOutOfBoundsException

Java-IndexOutOfBoundsException,java,awt,indexoutofboundsexception,Java,Awt,Indexoutofboundsexception,我正在用Java开发一个应用程序,我已经完成了三个主要类文件的编写——但是我遇到了一个令人困惑的错误。我现在只是“尝试”在屏幕上画像素,但我的像素阵列不工作 我在我的Render.java中获得IndexOutOfBoundsException,即使我现在在代码中得到警告或错误 Display.java public class Display extends Canvas implements Runnable { private static final long serialVersi

我正在用Java开发一个应用程序,我已经完成了三个主要类文件的编写——但是我遇到了一个令人困惑的错误。我现在只是“尝试”在屏幕上画像素,但我的像素阵列不工作

我在我的Render.java中获得IndexOutOfBoundsException,即使我现在在代码中得到警告或错误


Display.java

public class Display extends Canvas implements Runnable {

private static final long serialVersionUID = 1L;

public static final int WIDTH = 300;
public static final int HEIGHT = 190;
public static final int SCALE = 3;

public static Dimension DIMENSION = new Dimension(WIDTH * SCALE, HEIGHT * SCALE);

public static final String TITLE = "Untitled Project";

public int[] pixels;

public boolean isRunning = false;

public Thread thread;
public Screen screen;
public Render render;

public BufferedImage img;

public Display() {
    screen = new Screen(WIDTH, HEIGHT);
    img = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
    pixels = ((DataBufferInt) img.getRaster().getDataBuffer()).getData();
}

public void start(boolean isDebug) {
    if (isRunning)
        return;

    isRunning = true;

    thread = new Thread(this);
    thread.start();
}

public void stop() {
    if (!isRunning)
        return;

    isRunning = false;

    try {
        thread.join();
    } catch (Exception e) {
        e.printStackTrace();
        System.exit(0);
    }
}

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();
}

private void tick() {

}

public void run() {
    while (isRunning) {
        render();
        tick();
    }
}

public static void main(String[] args) {
    Display display = new Display();
        display.setMinimumSize(DIMENSION);
        display.setMaximumSize(DIMENSION);
        display.setPreferredSize(DIMENSION);

    JFrame frame = new JFrame();
        frame.setTitle(TITLE);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLayout(new BorderLayout());
        frame.add(display, BorderLayout.CENTER);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setResizable(false);
        frame.setVisible(true);

    display.start(true);
}
}
public class Render {

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

public Render(int width, int height) {
    this.width = width;
    this.height = height;
    pixels = new int[width * height];
}

public void draw(Render render, int xOffset, int yOffset) {
    for (int y = 0; y < render.height; y++) {

        int yPix = y + yOffset;

        for (int x = 0; x < render.width; x++) {
            int xPix = x + xOffset;

    ERROR -->   pixels[xPix + yPix * width] = render.pixels[x + y * render.width];
      }
    }
  }
}
public class Screen extends Render {

public Render render;
public Random random;

public Screen(int width, int height) {
    super(width, height);
    render = new Render(256, 256);
    random = new Random();

    for (int i = 0; i < 256 * 256; i++) {
        render.pixels[i] = random.nextInt();
    }
}

public void render() {
    draw(render, 0, 0);
}
}
提前感谢

--


您的
像素
数组的大小为
宽度*高度
。您正试图访问
width+height*width
。正如你所想象的那样,这对Java先生来说不太合适。

你的
像素
数组的大小是
宽度*高度
。您正试图访问
width+height*width
。正如你所想象的那样,这对Java先生来说不太合适。

在我看来,你似乎试图在高度和宽度上施加一个超级的
渲染

256, 256 (65536 pixels)
300, 190; (57000 pixels)
在一个高度和宽度的屏幕上

256, 256 (65536 pixels)
300, 190; (57000 pixels)
你对它们都使用相同的索引


也许您希望使渲染与屏幕匹配

render = new Render(this.width, this.height);

不像你那样硬编码到256x256

在我看来,你是在试图超级强加高度和宽度的
渲染

256, 256 (65536 pixels)
300, 190; (57000 pixels)
在一个高度和宽度的屏幕上

256, 256 (65536 pixels)
300, 190; (57000 pixels)
你对它们都使用相同的索引


也许您希望使渲染与屏幕匹配

render = new Render(this.width, this.height);

创建的
屏幕
对象的高度为190像素,而在
屏幕
构造函数中创建的
渲染
对象的高度为
256
像素,而不是像您那样硬编码到256x256。您需要确保不覆盖
屏幕的边界


请注意,
WIDTH*HEIGHT=300*190=57000
。(从
显示
类中定义的
宽度
高度

创建的
屏幕
对象的高度为190像素,但在
屏幕
构造函数中创建的
渲染
对象的高度为
256
像素。您需要确保不覆盖
屏幕的边界


请注意,
WIDTH*HEIGHT=300*190=57000
。(从
Display
类中定义的
WIDTH
HEIGHT

xPix+yPix*WIDTH
x+y*渲染。WIDTH
大于或等于
像素。length
@luigimendoza您不用说……作为引号。如果您对索引的计算不太确定,请确保大小始终与您的宽度和高度一致,即像素[(xPix+yPix*WIDTH)%WIDTH]。
xPix+yPix*WIDTH
x+y*render。WIDTH
大于或等于
像素。length
@luigimendoza您不说…作为引语。如果您对索引的计算不太确定,请确保大小始终与您的宽度和高度一致,即像素[(xPix+yPix*宽度)%WIDTH]。在什么情况下
xPix
不小于
宽度
?而且
yPix
不小于
height
这不是问题所在,是吗?在某一点上,它们比
宽度
高度
小1。然后OP尝试引用
xPix+yPix*width
。我真的很抱歉。它是
2
小于
width+height*width
。在什么情况下
xPix
不小于
width
?而且
yPix
不小于
height
这不是问题所在,是吗?在某一点上,它们比
宽度
高度
小1。然后OP尝试引用
xPix+yPix*width
。我真的很抱歉。它的
2
小于
width+height*width