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