-1

我在运行游戏时遇到了这个错误,由于某种原因没有调试它:/如果有人能帮助我解决这个错误,我将不胜感激,这是错误:(我是网站的新手)

Exception in thread "Display" java.lang.ArrayIndexOutOfBoundsException: 48600
at com.cmnatic.IndieFirstStudios.Game.render(Game.java:78)
at com.cmnatic.IndieFirstStudios.Game.run(Game.java:60)
at java.lang.Thread.run(Unknown Source)

这是我的代码:

package com.cmnatic.IndieFirstStudios;

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferInt;

import javax.swing.JFrame;

import com.cmnatic.IndieFirstStudios.graphics.Screen;

public class Game extends Canvas implements Runnable {
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    public static int width = 300;
    public static int height = width / 16 * 9;
    public static int scale = 3;

    private Thread thread;
    private JFrame frame;
    private boolean running = false;

    private Screen screen;

    private BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    private int[] pixels = ((DataBufferInt)image.getRaster().getDataBuffer()).getData();

    public Game() {
        Dimension size = new Dimension(width * scale, height * scale);
        setPreferredSize(size);

        screen = new Screen(width, height);

        frame = new JFrame();
   }

    public synchronized void start() {
        running = true;
        thread = new Thread(this, "Display");
        thread.start();
    }

    public synchronized void stop() {
        running = false;
        try {
            thread.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public void run() {
        while (running == true) {
            update();
            render();
        }
    }

    public void update() {

    }

    public void render() {
        BufferStrategy bs = getBufferStrategy();
        if (bs == null) {
            createBufferStrategy(3);
            return;
        }

        screen.render();

        for (int i = 0; 1 < pixels.length; i++) {
            pixels[i] = screen.pixels[i];
        }

        Graphics g = bs.getDrawGraphics();
        g.setColor(Color.BLACK);
        g.fillRect(0, 0, getWidth(), getHeight());
        g.drawImage(image, 0, 0, getWidth(), getHeight(), null);
        g.dispose();
        bs.show();
    }

    public static void main(String[] args) {
        Game game = new Game();
        game.frame.setResizable(false);
        game.frame.setTitle("Fallen Humanity");
        game.frame.add(game);
        game.frame.pack();
        game.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        game.frame.setLocationRelativeTo(null);
        game.frame.setVisible(true);

        game.start();
    }

}
4

2 回答 2

0

只需将循环更改为:

for (int i = 0; i < pixels.length; i++) {
    pixels[i] = screen.pixels[i];
}
于 2013-10-22T18:06:07.603 回答
0

您在第 78 行有一个无限循环。如果 1(one) < pixel.length,您的说法会贯穿 for 循环。所以最终我将 > pixel.length

于 2013-10-22T17:48:38.010 回答