Problem with a java array

Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

yazool

Thread Starter
Joined
Sep 24, 2008
Messages
2
Hello, I've been trying to write an applet which draws a simple grid and has interactive squares within the lines.
The squares are basically the Rectangle class but with a few more methods which are based on a boolean occupation
The applet should draw a blue rectangle if the square isn't occupied, and should draw a red rectangle if it is occupied.

Now the error is when I go to draw the colored rectanglesr:

Code:
Exception in thread "AWT-EventQueue-1" java.lang.ArrayIndexOutOfBoundsException:
 8
        at GridTest3.drawGridSquares(GridTest3.java:88)
        at GridTest3.paint(GridTest3.java:56)
        at sun.awt.RepaintArea.paintComponent(RepaintArea.java:248)
        at sun.awt.RepaintArea.paint(RepaintArea.java:224)
        at sun.awt.windows.WComponentPeer.handleEvent(WComponentPeer.java:254)
        at java.awt.Component.dispatchEventImpl(Component.java:4060)
        at java.awt.Container.dispatchEventImpl(Container.java:2024)
        at java.awt.Component.dispatchEvent(Component.java:3819)
        at java.awt.EventQueue.dispatchEvent(EventQueue.java:463)
        at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchTh
read.java:242)
        at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThre
ad.java:163)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:157)

        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:149)

        at java.awt.EventDispatchThread.run(EventDispatchThread.java:110)
I know the code to iterate through the array is correct, as I used it to construct each element in the array before hand.

Here's the GridSquare class:
Code:
//Basically a Rectangle with a field referring to whether something is in it or not
import java.awt.Rectangle;
import java.awt.*;

public class GridSquare extends Rectangle
{
	public GridSquare()
	{
		setLocation(0, 0);
		setSize(0, 0);
		setOccupied(false);
	}	
	public void setOccupied(boolean change)
	{
		occupationStatus = change;
	}
	
	public void changeOccupied()
	{
		if (occupationStatus)
			occupationStatus = false;
		else
			occupationStatus = true;
	}
	public boolean isOccupied()
	{
		return occupationStatus;
	}
	
	private boolean occupationStatus;	
}
And the applet, I've marked the line generating the first error:
Code:
//GridTest3 
//draws a grid, clicking squares changes their color from red to blue and vice versa

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.util.*;
import java.awt.geom.*;

public class GridTest3 extends Applet implements MouseListener
{
	int arraySizeX = 8, arraySizeY = 8;
	int width = getSize().width;
	int height = getSize().height;
	
	GridSquare[][] testGrid = new GridSquare[arraySizeX][arraySizeY];
	
	public void init()
	{
		addMouseListener(this); //event watcher for the mouse
		int locX, locY;		
     
	 
		for (int x = 0; x < arraySizeX; x++) //iterates through columns
		{
			for (int y = 0; y < arraySizeY; y++)// iterates through rows
			{
				testGrid[x][y] = new GridSquare(); //makes sure I don't get a NullPointerException by creating the object GridSquare in each array slot 
				locX = ((x + 1) * width); //multiplies the number of the square by the width of what each square should be
				locY = ((y + 1) * height);
				testGrid[x][y].setRect(locX, locY, width, height); //actually defines each GridSquare's Rectangle
				testGrid[x][y].setOccupied(false);
			}
		}
		
	}
	
	//applet paint event
	public void paint(Graphics g)
	{
		//create instance of graphics2D from graphics
		Graphics2D g2d = (Graphics2D)g;
		
		//save window height and width 
		int width = getSize().width;
		int height = getSize().height;
		
		//draw the background
		g2d.setPaint(Color.BLACK); 
		g2d.fillRect(0, 0, width, height);
		
		//draws the grid (the white lines)
		drawGrid(g2d);
		
		//draw the grid squares (the red/blue boxes)
		drawGridSquares(g2d);
	}
	
	public void drawGrid(Graphics2D g2d)
	{		
		g2d.setColor(Color.WHITE);
		
		for (int x = 0; x < getSize().width; x += getSize().width / arraySizeX) //draws vertical "lines" (moar liek rectangles with small width kekekekekeke)
		{
			g2d.fillRect(x, 1, 1, getSize().height);
		}
		
		for (int y = 0; y < getSize().height; y += getSize().height / arraySizeY) //draws horizontal "lines"
		{
			g2d.fillRect(1, y, getSize().width, 1);
		}		
	}
	
	public void drawGridSquares(Graphics2D g2d)
	{	 
		for (int x = 0; x < arraySizeX; x++) //iterates through entire array of GridSquares
		{
			for (int y = 0; y < arraySizeY; y++)
			{
                                [COLOR="Red"]if (testGrid[x][y].isOccupied())[/COLOR] //if current GridSquare is already Occupied a red rectangle is drawn, if it is not then a blue rectangle is drawn 
				{
					g2d.setColor(Color.red);
					g2d.fillRect((int) testGrid[x][y].getX() + 1, (int) testGrid[x][y].getY() + 1, (int) testGrid[x][y].getWidth() - 1, (int) testGrid[x][y].getHeight() - 1); 
				}
				else
				{
					g2d.setColor(Color.blue);
					g2d.fillRect((int) testGrid[x][y].getX() + 1, (int) testGrid[x][y].getY() + 1, (int) testGrid[x][y].getWidth() - 1, (int) testGrid[x][y].getHeight() - 1); 
				}
			}
		}
	}		
	
	//handle input events from the mouse
	public void mouseClicked(MouseEvent e)
	{
		switch(e.getButton())
		{
			case MouseEvent.BUTTON1:
				Point clickedLoc = new Point(e.getPoint());
				changeGridOccupation(clickedLoc);
				repaint();
				break;
		}
	}
	
	public void mouseEntered(MouseEvent e)
	{
	}
	
	public void mouseExited(MouseEvent e)
	{
	}
	
	public void mousePressed(MouseEvent e)
	{
	}
	
	public void mouseReleased(MouseEvent e)
	{
	}
	
	public void mouseDragged(MouseEvent e)
	{
	}
	
	public void mouseMoved(MouseEvent e)
	{
	}
	
	public void changeGridOccupation(Point location)
	{
		for (int x = 0; x < arraySizeX; x++) //iterates through columns
		{
			for (int y = 0; y < arraySizeY; y++)// iterates through rows
			{
				if (testGrid[x][y].contains(location))
					testGrid[x][y].changeOccupied();
			}
		}	
	}
}
I realize a lot of the code is quite inefficient (like using fillRect to draw lines when there's a drawLine method), but I'm still a beginner programmer and want to keep it simple even if it is inefficient.

Thanks
 
Joined
Jan 20, 2002
Messages
433
There's no obvious problem that jumps out at me. In a situation like this, I would suggest that you double check to make sure the source code really matches the running code (for example, change the fill color).
 
Joined
Jul 29, 2004
Messages
6,650
Hi yazool,

Welcome to TSG ! :)

Your GridSquare instances are not properly initialized. Indeed, the values of testGrid[x][y].getX(), testGrid[x][y].getY(), testGrid[x][y].getWidth() and testGrid[x][y].getHeight() are equal to zero for each item.
Therefore, the test (testGrid[x][y].contains(location)) in the changeGridOccupation method won't never work.
 
Joined
Jul 29, 2004
Messages
6,650
Hi yazool,

Welcome to TSG ! :)

Your GridSquare instances are not properly initialized. Indeed, the values of testGrid[x][y].getX(), testGrid[x][y].getY(), testGrid[x][y].getWidth() and testGrid[x][y].getHeight() are equal to zero for each item.
Therefore, the test (testGrid[x][y].contains(location)) in the changeGridOccupation method won't never work.
 

yazool

Thread Starter
Joined
Sep 24, 2008
Messages
2
Thank you!
I went through the code and outputted the values to the console to figure out where I went wrong and I finally singled it out.
 
Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 807,865 other people just like you!

Latest posts

Members online

Top