Java 3D引擎像素绘制问题:多类联合运行触发out of bounds error求助
Hey there! Let's break down how to track down that out-of-bounds error you're hitting when running your three Java classes together—even without a working debugger.
Since your debugger won't fire up, we'll lean into good old-fashioned print statements to catch the bad index. Focus on places where your graphics classes interact with arrays (like pixel buffers, which are the #1 culprit for out-of-bounds in graphics code):
- In
Screen.java, wrap every pixel array access with a check that logs when you're hitting invalid indices. For example:
// Replace direct array writes with this int index = y * width + x; if (index < 0 || index >= pixels.length) { System.err.println("[Screen] Out of bounds! Index: " + index + ", pixels length: " + pixels.length + ", x: " + x + ", y: " + y); } else { pixels[index] = color; }
- In
Render.java, do the same for any methods that pass coordinates toScreenor manipulate its own buffers. Log the x/y values being passed before they're used. - In
Display.java, log the window width/height when it initializes or resizes—this will tell you if the display dimensions don't match whatScreen/Renderare expecting.
Since each class works alone but breaks together, the issue is almost definitely a conflict in shared state or mismatched parameters:
- Mismatched dimensions:
Displaymight be using a window size (e.g., 800x600) butScreeninitialized its pixel array with a smaller size (e.g., 799x599). WhenRendertries to draw to the 800th pixel, it blows up. - Shared resource race conditions: If
Displayis running on the UI thread andRender/Screenon a separate render thread, they might be modifying the same pixel array at the same time. This can lead to invalid indices being calculated when the array size is mid-update. - Unsynced state changes: If
Displayresizes the window afterScreen/Renderinitialize, those classes won't know to update their buffer sizes—so they'll keep writing to an array that's too small.
Given your package structure (Display at the root, Render/Screen in graphics), here's how to align everything:
- Use shared constants for dimensions: Define your window width/height as public static constants in
Display.java:
package com.mime.Game; public class Display { public static final int WIDTH = 800; public static final int HEIGHT = 600; // Rest of your code... }
Then have Screen and Render reference these constants instead of hardcoding their own:
package com.mime.Game.graphics; import com.mime.Game.Display; public class Screen { private int[] pixels = new int[Display.WIDTH * Display.HEIGHT]; // Rest of your code... }
This ensures everyone's using the same size values.
- Add boundary checks everywhere: Even if you fix the dimensions, add defensive checks to all coordinate-based methods. For example, in
Render.java's render method:
public void render(int x, int y, int color) { if (x < 0 || x >= Display.WIDTH || y < 0 || y >= Display.HEIGHT) { System.err.println("[Render] Invalid coordinates: x=" + x + ", y=" + y); return; } // Proceed with rendering... }
- Fix thread safety if needed: If you're using multiple threads, wrap access to shared pixel arrays with
synchronizedblocks. For example, inScreen.java's method that updates pixels:
public synchronized void setPixel(int x, int y, int color) { // Pixel array access here }
And call this method from Render instead of directly writing to the array.
Run all three classes together after adding the logs and checks. The error messages will point you directly to which class/method is generating the invalid index—then you can tweak that code to align with the shared dimensions or add proper synchronization.
内容的提问来源于stack exchange,提问作者DaHomieAdam and

