2014-02-21 25 views
0

我有一个问题,我用Java编程,当我去运行它时,它列出了大约6个错误。这些不能找到任何可能的错误,但Java说不同

Exception in thread "Display" java.lang.ArrayIndexOutOfBoundsException: 64 
    at com.cmnatic.mld.graphics.Screen.clear(Screen.java:27) 
    at com.cmnatic.mld.Game.render(Game.java:107) 
    at com.cmnatic.mld.Game.run(Game.java:77) 
    at java.lang.Thread.run(Unknown Source) 

如果有帮助,这是我的代码(OFC它)

Game.java:

package com.cmnatic.mld; 

import java.awt.Canvas; 
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.mld.graphics.Screen; 

public class Game extends Canvas implements Runnable { 
    private static final long serialVersionUID = 1L; 

    public static int width = 300; // 300 * 3 = 900 
    public static int height = width/16 * 9; //168.75 * 3 = 506.25 
    public static int scale = 3; 
    public static String title = "CMNatic's MLD Entry #49"; 


    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); 
     setPrefferedSize(size); 

     screen = new Screen(width, height); 

     frame = new JFrame(); 
     this.setSize(900,506); 
    } 

    private void setPrefferedSize(Dimension size) { 

    } 

    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() { 
     long lastTime = System.nanoTime(); 
     long timer = System.currentTimeMillis(); 
     final double ns = 100000000.0/60.0; // nano-seconds = 1000000000 (9 0'S)/60.0 
     double delta = 0; 
     int frames = 0; 
     int updates = 0; 
     while (running) { 
      long now = System.nanoTime(); 
      delta += (now-lastTime)/ns; //nano-seconds (ns) 
      lastTime = now; 
      while (delta >= 1) { 
       update(); 
       updates++; 
       delta--; 
      } 
      render(); 
      frames++; 

      if (System.currentTimeMillis() - timer > 1000) { 
       timer += 1000; 
       System.out.println(updates + " ups, " + frames + " fps"); 
       frame.setTitle(title + " | " + updates + "ups, " + frames); 
       updates = 0; 
       frames = 0; 
      } 
     } 
     stop(); 
    } 

    int x = 0, y = 0; 

    public void update() { 
     y++; 
     if (y % 10 == 0) x++; 
     x++; 
     //y++; 
    } 

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

Screen.Java

package com.cmnatic.mld.graphics; 

import java.util.Random; 

public class Screen { 

    private int width, height; 
    public int[] pixels; 
    public final int MAP_SIZE = 8; 
    public final int MAP_SIZE_MASK = MAP_SIZE - 1; 
    public int[] tiles = new int[MAP_SIZE * MAP_SIZE]; 

    private Random random = new Random(); 

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

     for (int i = 0; i < MAP_SIZE * MAP_SIZE; i++) { 
      tiles[i] = random.nextInt(0xffffff); 
     } 
    } 

    public void clear() { 
     for (int i = 0; i < pixels.length; i++) { 
      tiles[i] = random.nextInt(0xffffff); 
      tiles[0] = 0; 
     } 
    } 

    public void render(int xOffset, int yOffset) { 
     for (int y = 0; y < height; y++) { 
      int yy = y + yOffset; 
      //if (yy < 0 || y >= height) break; 
      for (int x = 0; x < width; x++) { 
       int xx = x + xOffset; 
       //if (xx < 0 || x >= width) break; 
       int tileIndex = ((xx >> 4) + xOffset& MAP_SIZE_MASK) + ((yy >> 4)& MAP_SIZE_MASK) * MAP_SIZE; 
       pixels[x + y * width] = tiles[tileIndex]; 


      } 
     } 
    } 

} 

如果有人可以帮助,我会永远感激!

+2

设置您的堆栈跟踪的格式(适用于包含它)并指定它所引用的行。 – chrylis

+0

Screen.java ---第27行 – kosa

+2

如果您在发布的源文件中注释堆栈跟踪行,这将有所帮助,因此我们不必计算行数。 –

回答

2

Screen.clear()您有:

for (int i = 0; i < pixels.length; i++) { 
     tiles[i] = random.nextInt(0xffffff); 
     tiles[0] = 0; 
    } 

但基于您的意见,pixels显然比tiles大。你的意思可能是tiles.length这个for循环(我假设clear应该和你在Screen构造函数末尾的循环中做同样的事情)。

一般来说,当您看到一个ArrayIndexOutOfBoundsException时,它恰恰意味着数组索引超出范围。当你遇到这种情况时,仔细看看你的代码,并试图找到发生的机会。在这种情况下,在索引循环中使用不同数组的length是一个大红旗。

此外,顺便说一句,该循环中的tiles[0] = 0看起来好像不应该在那里。

+0

谢谢老兄!工作就像一个魅力,这是一个愚蠢的错误:P – user3335689

+0

不愚蠢。让其他人看看你一直盯着太久的代码是很好的。 –

2

您的问题是您在clear方法中互换使用pixelstiles。逻辑“板”大小为8x8,但您的pixels阵列根据传入的参数进行大小设置。然后尝试迭代8x8板上的50k左右像素,并立即运行结束。

此外,这两个阵列被很明显地表示的二维概念(一个板和一个屏幕),并且它使你的代码更清楚使用二维数组:

int pixels[][] = new int[width][height]; 
+0

谢谢你的回复,幸运的是Jason的回答为我工作。但我正在扫描我的代码,这样的小东西:) 再次感谢 – user3335689