You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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.

Step 1: Add Manual Debug Logging (Since Debugger Isn't Working)

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 to Screen or 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 what Screen/Render are expecting.
Step 2: Common Culprits to Check

Since each class works alone but breaks together, the issue is almost definitely a conflict in shared state or mismatched parameters:

  • Mismatched dimensions: Display might be using a window size (e.g., 800x600) but Screen initialized its pixel array with a smaller size (e.g., 799x599). When Render tries to draw to the 800th pixel, it blows up.
  • Shared resource race conditions: If Display is running on the UI thread and Render/Screen on 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 Display resizes the window after Screen/Render initialize, those classes won't know to update their buffer sizes—so they'll keep writing to an array that's too small.
Step 3: Targeted Fixes for Your Class Setup

Given your package structure (Display at the root, Render/Screen in graphics), here's how to align everything:

  1. 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.

  1. 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...
}
  1. Fix thread safety if needed: If you're using multiple threads, wrap access to shared pixel arrays with synchronized blocks. For example, in Screen.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.

Step 4: Verify the Fix

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 08:17:34