-1

我正在用 Java 开发一个应用程序,并且我已经完成了三个主要类文件的编写——但是我遇到了一个令人困惑的错误。我目前只是“尝试”在屏幕上绘制像素,但我的像素数组不起作用。

即使我现在在代码中收到警告或错误,我也会在我的Render.java中收到IndexOutOfBoundsException ?

显示.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);
    }
}

渲染.java

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];
          }
        }
      }
    }

屏幕.java

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

错误

Exception in thread "Thread-2" java.lang.ArrayIndexOutOfBoundsException: 57000
at com.willajhughes.java.graphics.Render.draw(Render.java:23)
at com.willajhughes.java.graphics.Screen.render(Screen.java:21)
at com.willajhughes.java.Display.render(Display.java:76)
at com.willajhughes.java.Display.run(Display.java:94)
at java.lang.Thread.run(Unknown Source)
4

3 回答 3

3

你的pixels数组是 size width * height。您正在尝试访问width + height * width. 正如您可能想象的那样,Java 先生的情况不会很好。

于 2013-08-16T15:30:02.773 回答
0

在我看来,您正在尝试叠加render高度和宽度

256, 256 (65536 pixels)

在一个高度和宽度的屏幕上

300, 190; (57000 pixels)

并且您对它们都使用相同的索引。


也许你想让你的渲染匹配你的屏幕

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

而不是像你一样硬编码为 256x256

于 2013-08-16T15:35:38.083 回答
0

创建的Screen对象的高度为 190 像素,但Render在构造函数中创建的对象Screen的高度为256像素。您需要确保不会覆盖Screen.

请注意WIDTH * HEIGHT = 300 * 190 = 57000. (来自类中定义的WIDTH和。)HEIGHTDisplay

于 2013-08-16T15:37:20.043 回答