绘制像素时数组索引超出界限

时间:2013-09-30 02:37:05

标签: java swing graphics awt render

我目前正试图在我的窗口中绘制像素,而且我似乎遇到了程序启动时索引越界错误。老实说,我不知道如何解决它。谁能告诉我问题出在哪里?我已经检查了很多次,但似乎对我来说应该没问题。我已经为下面的项目提供了所有代码,但错误是在for循环所在的render方法(Game类)中的行。

编辑:

错误发生在Game类的for循环中。它说

  

java.lang.ArrayIndexOutOfBoundsException:48601"在线   " pixles [i] = screen.pixels [i]"

屏幕类

public class Screen {
    private int width,height;
    public int[] pixels;

    public Screen(int width,int height){
        this.width = width;
        this.height = height;
        pixels = new int[width * height]; 

    }

    public void render(){
        for (int y = 0; y < height; y++){
            for (int x = 0; x < width; x++){
                pixels[x + y * width] = 0xff00ff; 
            }
        }
    }

}


Game Class
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;


public class Game extends Canvas implements Runnable{

    private int width = 300;
    private int height = width / 16 * 9;
    private int scale = 3;
    private Thread thread;
    private boolean running = false;
    private JFrame frame;
    private BufferedImage image = new BufferedImage(width * scale, height * scale,BufferedImage.TYPE_INT_RGB);
    private int[] pixels = ((DataBufferInt)image.getRaster().getDataBuffer()).getData();
   private Screen screen;
    public Game(){

        screen = new Screen(width,height);
        frame = new JFrame("Relm Of The Mad God Clone");
        this.setPreferredSize(new Dimension(width * scale, height * scale));
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setResizable(false);
        frame.add(this);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);



        start();

    }

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

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

    public static void main(String[] args) {
        Game g = new Game();
    }

    private void update(){

    }

    private void render(){
        BufferStrategy buffer = getBufferStrategy();

        if (buffer == null){
            createBufferStrategy(3);
            return;
        }  

        screen.render();

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

        Graphics g = buffer.getDrawGraphics();

        g.drawImage(image, 0, 0, getWidth(), getHeight(),this);

        g.dispose();
        buffer.show();
    }

    @Override
    public void run() {
        while (running){
            update();
            render();

        }
    }
}

1 个答案:

答案 0 :(得分:1)

screen变量已使用new Screen(width, height)初始化,因此可能有width*height像素。另一方面,pixels变量是从image初始化的,其width*scale * height*scale像素。

scale=3开始,这意味着pixels数组的长度是screen数组的3倍。这就是你获得例外的原因。