Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Error while moving the tiles in frame

  1. #1
    Junior Member
    Join Date
    Jan 2014
    My Mood
    Thanked 0 Times in 0 Posts

    Exclamation Error while moving the tiles in frame

    Hello! I was following a tutorial on how to make a game when i suddenly got an error concerning moving tiles. i get this error when i tried to use a and d to move:

    Exception in thread "Display" java.lang.ArrayIndexOutOfBoundsException: 48600
    at com.cherno.graphics.Screen.render(Screen.java:44)
    at com.cherno.Game.render(Game.java:124)
    at com.cherno.Game.run(Game.java:87)
    at java.lang.Thread.run(Unknown Source)

    I do not get the error while using w or s but the tiles are still not moving when pressing them.

    I have three classes at work here.
    The main, Game.java;

    package com.cherno;
    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.cherno.Game;
    import com.cherno.graphics.Screen;
    import com.cherno.input.Keyboard;
    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 Keyboard key;
    	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);
    		screen = new Screen(width, height);
    		frame = new JFrame();
    		key = new Keyboard();
    	public synchronized void start(){
    		running = true;
    		thread = new Thread(this, "Display");
    	public synchronized void stop(){
    		running = false;
    		}catch(InterruptedException e){
    	public void run(){
    		long lastTime = System.nanoTime();
    		long timer = System.currentTimeMillis();
    		final double ns= 1000000000.0 / 60.0;
    		double delta = 0;
    		int frames = 0;
    		int updates = 0;
    		while (running){
    			long now = System.nanoTime();
    			lastTime = now;
    			while (delta >= 1){
    			if (System.currentTimeMillis() - timer > 1000){
    				timer += 1000;
    				//System.out.println(updates + "ups, " + frames + " fps");
    				frame.setTitle("false rabbit " + "  |   "+updates + "ups, " + frames + " fps");
    	public int x=0, y=0;
    public void update(){
    	public void render(){
    		BufferStrategy bs = getBufferStrategy();
    		if (bs==null){
    		screen.render(x, 0);
    		for (int i = 0; i < pixels.length; i++){
    			pixels[i] = screen.pixels[i];
    		Graphics g = bs.getDrawGraphics();
    		g.drawImage(image, 0, 0, getWidth(), getHeight(), null);
    	public static void main(String[] args){
    		Game game = new Game();
    		//game.frame.setTitle("rabbit false");

    The class that mainly structures the "screen", Screen;
    package com.cherno.graphics;
    import java.util.Random;
    public class Screen {
    	private int width, height;
    	public int[] pixels;
    	public final int MAP_SIZE = 64;
    	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];
    		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++){
    		pixels[i] = 0;
    	public void render(int xoffset, int yoffset){
            for(int y = 0; y < height; y++){
            	int yy = y + yoffset;
            	//if (yy < 0 || yy >= height )break;
    			for(int x = 0; x < width; x++){
    			    int xx = x + xoffset;
    				//if (xx < 0 || xx >= width )break;
    			    int tileIndex = ((xx >> 4) & MAP_SIZE_MASK ) + ((yy >> 4) & MAP_SIZE_MASK) * MAP_SIZE;
    			    pixels[xx + yy * width] = tiles[tileIndex];
    			    //error uccurs when using a and d not w or s

    And at last The Key class that listens to my keystrokes, i present..
    package com.cherno.input;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
    public class Keyboard implements KeyListener{
    	private boolean[] keys = new boolean[120];
    	public boolean up, down, left, right;
    	public void update(){
    		up = keys[KeyEvent.VK_UP] || keys[KeyEvent.VK_W];
    		down = keys[KeyEvent.VK_DOWN] || keys[KeyEvent.VK_S];
    		left = keys[KeyEvent.VK_LEFT] || keys[KeyEvent.VK_A];
    		right = keys[KeyEvent.VK_RIGHT] || keys[KeyEvent.VK_D];
    	public void keyPressed(KeyEvent e) {
    		keys[e.getKeyCode()] = true;
    	public void keyReleased(KeyEvent e) {
    		keys[e.getKeyCode()] = false;
    	public void keyTyped(KeyEvent e) {

    I am SO happy for answers!

  2. #2
    Super Moderator
    Join Date
    Jun 2013
    So. Maryland, USA
    My Mood
    Thanked 698 Times in 680 Posts

    Default Re: Error while moving the tiles in frame

    The integer array 'pixels[]' in Screen.render() is being given an index value greater than the array has been initialized to hold. The offending index value is 48,600 which must be larger than the product width * height. Work backwards, find out what the product width * height is and determine how to either limit the element index values to one less than that value OR increase width * height to accommodate larger values, if required. Or use an ArrayList of Integers for pixel[] if the size of the pixel[] array is uncertain (I like this last suggestion the least, because it indicates the programmer doesn't know what's going on, and that's not good when working with graphics/pixels on a display surface.)

  3. The Following User Says Thank You to GregBrannon For This Useful Post:

    weirddan (January 17th, 2014)

  4. #3
    Junior Member
    Join Date
    Jan 2014
    My Mood
    Thanked 0 Times in 0 Posts

    Smile Re: Error while moving the tiles in frame

    So the array "pixels" is been given a value of ((300/16*9)*300)= 50625 and that is certainly greater than 48,600

    Isnt it the line;

    pixels = new int[width * height];

    that set the max value the array "pixel" can hold?

    --- Update ---

    In the main class i changed the line;
    screen.render(x, 0);
    screen.render(x, y);
    this makes it crash with the same error when i press any fps move button(a,w,s,d) or any arrow button.
    in the tutorial i am following that is the correct line

    --- Update ---

    FIXED IT!. the error i got were because i (from main class Game) sent x and y to Screen but Screen did not actually do anything with it. by changing xx to x and yy to y in Following line i have successfully done what i intended.;

    pixels[xx + yy * width] = tiles[tileIndex];

    created the error, butt changing to;

    pixels[x + y * width] = tiles[tileIndex];

    fixed it!

    I removed this error by referring x and y in that line to those who Game is sending to Screen.

    Thanks for the help!!

Similar Threads

  1. Drawing many Tiles on screen
    By Gravity Games in forum Object Oriented Programming
    Replies: 12
    Last Post: November 20th, 2012, 08:49 PM
  2. Remder multiple tiles with arraylist
    By opiop65 in forum What's Wrong With My Code?
    Replies: 0
    Last Post: November 10th, 2012, 08:25 AM
  3. Replies: 1
    Last Post: January 19th, 2012, 03:44 PM
  4. using css file with tiles
    By the light in forum Web Frameworks
    Replies: 0
    Last Post: October 5th, 2011, 01:22 AM
  5. Struts 1.3.8 and tiles
    By jsnx7 in forum Web Frameworks
    Replies: 3
    Last Post: July 21st, 2009, 03:29 AM