Java RunTime Error

Help with sloving my java runtime error please.

  • (4 Pages)
  • +
  • 1
  • 2
  • 3
  • Last »

46 Replies - 2559 Views - Last Post: 21 April 2008 - 09:25 PM Rate Topic: -----

#1 Jet_Man  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 07-April 08

Java RunTime Error

Posted 20 April 2008 - 02:31 PM

I am Having a problem with running a game in creating on java, it is a run time error which says "indexOutofBounsException:
Index; 0, Size: 0 (in java.util.Arraylist)"


i have made bold the line of code java highlights with this error:

 import java.util.ArrayList;
import java.io.*;
/**
 * Prototype "maze game" class. To play, create a new Game
 * object in BlueJ and invoke the goUp, goRight, etc commands
 * to move around the maze.
 */
public class Game
{
	/** The player. */
	private Player player;
	
	/**The List of Mazes created. */
	private ArrayList <Maze> listofMaze;
	
	/** A panel in which the game state is displayed. */
	private GameWindow view;
	
	/**
	 * current level in game
	 */
	 private int currentLevel;
	 
	/**
	 * Initialise a new Game.
	 */
	
	/**
	 *  the maze
	 */
	
   private Maze maze;
	
	public Game() throws IOException
	{
	   nextgame();
	   view = new GameWindow(this);
	   view.addKeyListener(new KeyControl());
		
	   currentLevel = 0;
	   listofMaze = new ArrayList<Maze>();
	   createMazes();
	   
	 maze = listofMaze.get(0);

	   
	   player = new Player(getMaze().getStartX(), getMaze().getStartY());
	   
		if (!isValidPosition(player.getX(), player.getY())) {
			throw new Error("Player has started in the wrong position: (" + player.getX() + "," + player.getY() + ")");
		}
		
	}
	
   public void nextgame()
	{	   
			try {
				currentLevel = 0;
				listofMaze = new ArrayList<Maze>();
				createMazes();
				
			}catch(java.io.IOException e){}
		player = new Player(getMaze().getStartX(), getMaze().getStartY());
		if (!isValidPosition(player.getX(), player.getY())) {
			throw new Error("Player has started in the wrong position: (" + player.getX() + "," + player.getY() + ")");	 
		}
	}

	
	
	/**
	 * returns the maze object
	 */
	
	public Maze getMaze()
	{
		[b]return listofMaze.get(currentLevel);[/b]
	}

	
	/**
	 * creates different mazes. Add items in two cells.  
	 * Add the mazes to the list of mazes.
	 */
	
   
   private void createMazes() throws IOException
   {
		
		listofMaze.add(new Maze ("Maze1-filetxt.txt"));
		listofMaze.get(0).setStart(7, 4);
		listofMaze.get(0).setFinish(5, 9);
		listofMaze.get(0).getCell (10, 6).setitem(new Item("health"));
		listofMaze.get(0).getCell (11, 8).setitem(new Item("door"));
	   
		listofMaze.add(new Maze ("Maze2-filetxt.txt"));
		listofMaze.get(1).setStart(6, 3);
		listofMaze.get(1).setFinish(8, 10);
		listofMaze.get(1).getCell (7, 2).setitem(new Item("health"));
		listofMaze.get(1).getCell (8, 10).setitem(new Item("door"));
		listofMaze.get(1).getCell (3, 2).setitem(new Item ("shield"));
		 
	 listofMaze.add(new Maze ("Maze3-filetxt.txt"));
		listofMaze.get(2).setStart(4, 2);
		listofMaze.get(2).setFinish(8, 10);
		listofMaze.get(2).getCell (7, 2).setitem(new Item("health"));
		listofMaze.get(2).getCell (8, 10).setitem(new Item ("button"));
		listofMaze.get(2).getCell (3, 2).setitem(new Item ("shield"));
		  
		
	}
	   
	
	  /**
	 * Move player into a certain position.
	 * Do nothing if the player's current position is not
	 * connected to a neighbour in that direction.
	 * 
	 * @param d The direction in which to move.
	 */
	
	public void go(String d)
	{
		int x = player.getX();
		int y = player.getY();
		Cell currentCell = getMaze().getCell(x, y);
		if (currentCell.isOpen(d)) {
			if (d.equals("up")) {
				player.setPosition(x, y-1);
			} else if (d.equals("right")) {
				player.setPosition(x+1, y);
			} else if (d.equals("down")) {
				player.setPosition(x, y+1);
			} else if (d.equals("left")) {
				player.setPosition(x-1, y);
			} else {
				throw new Error("Wrong direction: " + d);
			}
			if (!isValidPosition(player.getX(), player.getY())) {
				throw new Error("Invaild Position: (" + player.getX() + "," + player.getY() + ")");
			}
			
			if (getMaze().getFinishX() == player.getX() && getMaze().getFinishY() == player.getY())
			{
				if (currentLevel < listofMaze.size()-1)
				{
					currentLevel++;
					player.setPosition(getMaze().getStartX(), getMaze().getStartY());
				}
				else
				{
					System.exit(0);
				}
			} 
			Cell c = getMaze().getCell(player.getX(), player.getY());
				
				// Points are increased by 20 when the player enters this cell
				if (c.getitem()!= null && c.getitem().getitemname() == "shield") 
				{
				 player.setscore(20);
				 
				 c.setitem(null);
			}
			
			//One life is added for the player when this cell is entered
			 if (c.getitem()!= null && c.getitem().getitemname() == "Health") 
			   {
				player.sethealth(1);
				 
			   c.setitem(null);
				 
			view.repaint();
		}
	}

}
 
	
	/**
	 * Attempt to move the player up.
	 * Do nothing if the player's current position is not
	 * connected to a neighbour in that direction.
	 * 
	 */
	public void goUp()
	{
		go("up");
	}
	
	
	/**
	 * Attempt to move the player right.
	 * Do nothing if the player's current position is not
	 * connected to a neighbour in that direction.
	 * 
	 */
	public void goRight()
	{
		go("right");
	}
	
	
	/**
	 * Attempt to move the player down.
	 * Do nothing if the player's current position is not
	 * connected to a neighbour in that direction.
	 * 
	 */
	public void goDown()
	{
		go("down");
	}
	
	
	/**
	 * Attempt to move the player left.
	 * Do nothing if the player's current position is not
	 * connected to a neighbour in that direction.
	 * 
	 */
	public void goLeft()
	{
		go("left");
}
	/**
	 * 
	 *Returns player
	 */
	
  public Player getPlayer()
	{
	   return player;
	}
		

	
	/**
	 * Is a given position a valid place for the player to be?
	 * 
	 * @param x The x-position to test.
	 * @param y The y-position to test.
	 */
	
	private boolean isValidPosition(int x, int y)
	{
		if (x >= 0 && x < getMaze().getWidth() && y >= 0 && y < getMaze().getHeight()) {
			return getMaze().getCell(x, y) !=null;
		} else {
			return false;
		}
	}
 
	
	private class KeyControl extends java.awt.event.KeyAdapter
	{
		public void keyPressed(java.awt.event.KeyEvent e)
		{
			int code = e.getKeyCode();
			if (code == java.awt.event.KeyEvent.VK_UP) {
				goUp();
			} else if (code == java.awt.event.KeyEvent.VK_RIGHT) {
				goRight();
			} else if (code == java.awt.event.KeyEvent.VK_DOWN) {
				goDown();
			} else if (code == java.awt.event.KeyEvent.VK_LEFT) {
				goLeft();
			}
		}
	}
}





I will be very greatfull on any help solving this error, please ask if you need more information from me to slove the problem.

Thank you

This post has been edited by Jet_Man: 20 April 2008 - 02:33 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Java RunTime Error

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8332
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Java RunTime Error

Posted 20 April 2008 - 03:30 PM

If you want us to help you post also the code of your classes:

- Maze
- Player
- GameWindow
Was This Post Helpful? 0
  • +
  • -

#3 m2s87  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 21
  • View blog
  • Posts: 390
  • Joined: 28-November 06

Re: Java RunTime Error

Posted 20 April 2008 - 03:31 PM

It is not bad all bad :^: . Anyhow you have quite a few mistakes, something like:
	 maze = listofMaze.get(0);
	   player = new Player(getMaze().getStartX(), getMaze().getStartY());



Well in short you are not actually using getMaze() method anyhow, so why not just write
	 maze = listofMaze.get(currentLevel);
	   player = new Player(maze.getStartX(), maze.getStartY());



Looking at duplicate code:
       nextgame();
       ...
       listofMaze = new ArrayList<Maze>();
       createMazes();

This might seem ok, but if you take a look at nextgame method, you might find there
       ...
       listofMaze = new ArrayList<Maze>();
       createMazes();

Was This Post Helpful? 0
  • +
  • -

#4 Jet_Man  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 07-April 08

Re: Java RunTime Error

Posted 20 April 2008 - 04:34 PM

post below

This post has been edited by Jet_Man: 20 April 2008 - 04:53 PM

Was This Post Helpful? 0
  • +
  • -

#5 Jet_Man  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 07-April 08

Re: Java RunTime Error

Posted 20 April 2008 - 04:45 PM

Ok here are my Classes ...



Maze

 import java.io.*;

/**
 * This maze class will contain all the information needed
 * 
 * @author 
 * 
 */
public class Maze
{
	/** Declaration of the cells field. */
   private Cell[][] cells;
   
	/** The height of the created maze. */
	private int height;
  
   /** The width of the created maze. */
	private int width;
	
	
	/**
	 * the starting position.
	 * the ending position.
	 */
	private int x_Start, y_Start, x_Finish, y_Finish;
  
	/**
	 * Constructor for objects of class Maze
	 */
 
	
	
	public Maze()
	{
		// Creates an empty cell.
		width = 20;
		height = 15;
		cells = new Cell[width][height];
	}
   
		public Maze(String fileName) throws IOException
	{
		FileReader fr = null;
		BufferedReader br = null;
		try {
			fr = new FileReader(fileName);
			br = new BufferedReader(fr);
			String line = br.readLine();
			int y = 0;
			while (line != null) {
			  
			 for(int x = 0; x < line.length();x++)
	 {
		char c = line.charAt(x);
		if(c ==' '){
		cells[x][y] = new Cell();
	}
}
	line =br.readLine();
	y++;

			}
			
		} finally {
			if (br != null) br.close();
			if (fr != null) fr.close();
		}
	}


  
	/**
	 * Connect the cell at (x,y) to its neighbouring cell
	 * in the given direction. The position (x,y) and the
	 * neighbouring position must be non-null in the cells
	 * array.
	 * 
	 * @param d The direction of the neighbour.
	 * @param x The x-position of the cell.
	 * @param y The y-position of the cell.
	 */
	 public void connect(String d, int x, int y)
	{
		if (d.equals("up")) {
			cells[x][y].openWall("up");
			cells[x][y-1].openWall("down");
		} else if (d.equals("right")) {
			cells[x][y].openWall("right");
			cells[x+1][y].openWall("left");
		} else if (d.equals("down")) {
			cells[x][y].openWall("down");
			cells[x][y+1].openWall("up");
		} else if (d.equals("left")) {
			cells[x][y].openWall("left");
			cells[x-1][y].openWall("right");
		} else {
			throw new Error("Wrong direction: " + d);
		}
	}

	
	public void setStart(int x, int y)
	{
		x_Start = x;
		y_Start = y;
	}
	
	public void setFinish(int x, int y)
	{
		x_Finish = x;
		y_Finish = y;
	}
	
	/**
	 * returns the x to the bigining. 
	 */
	
	public int getStartX()
	{
		return x_Start;
	}
	
	/**
	 * returns the y to the bigining. 
	 */
	 public int getStartY()
	{
		return y_Start;
	}
	
	/**
	 * returns the x to the end. 
	 */
	
	public int getFinishX()
	{
		return x_Finish;
	}
	
	/**
	 * returns the y to the end. 
	 */
	
	public int getFinishY()
	{
		return y_Finish;
	}
		
   
	 /**
	 * The maze cell at a given position.
	 * 
	 * @param x The x-coordinate of the position.
	 * @param y The y-coordinate of the position.
	 */
	
	public Cell getCell(int x, int y)
	{
		return cells[x][y];
	}
	
	
	public void addCell(int x, int y)
	{
		
		cells[x][y] = new Cell();
	}
	
   /**
	 * The width of maze.
	 */
	
	 public int getWidth()
	{
		return width;
	}
	
	/**
	 * The height of maze.
	 */
	public int getHeight()
	{
		return height;
	}
	
   
   
} 



Player


/**
 * A Player in the maze game.
 */
public class Player
{
	/** Current x-position of the player in the maze. */
	private int xPosition;
	
	/** Current y-position of the player in the maze. */
	private int yPosition;
	
	/**health of player. */
	private int lives;
	
	 /**points player achived. */
	private int score;
	
	/**
	 * Initialise a new Player. The coordinate specifies a cell
	 * in the maze grid (not a pixel position on the screen).
	 * 
	 * @param x Initial x-position.
	 * @param y Initial y-position.
	 */
	public Player(int x, int y)
	{
		this.setPosition(x, y);
		score = 0;
		lives = 4;
	}
	
	/**
	 * The x-position of the player's current cell in the maze grid.
	 */
	public int getX()
	{
		return xPosition;
	}
	
	/**
	 * The y-position of the player's current cell in the maze grid.
	 */
	public int getY()
	{
		return yPosition;
	}

	/**
	 * Set the player's position. The coordinate specifies a cell
	 * in the maze grid (not a pixel position on the screen).
	 * 
	 * @param x The new x-position.
	 * @param y The new y-position.
	 */
	public void setPosition(int x, int y)
	{
		xPosition = x;
		yPosition = y;
	}
	
	/**Set the health of the player.
	 * 
	 * @parm scorenumber how high the players score is.
	 */
	 public void setscore (int scorenumber)
	 {
		score = scorenumber + score;
	 }

	 /**
	  * @parm healthbar how high the players health is.
	  */
	  public void setlife (int add)
	  {
		 lives = lives + add;
	   }
	   
	   /**The Players Score.*/
	   public int getscore()
	   
	   {
		   return score;
		   
		}
		
		   /**The amount of health the player has.*/
	   public int getlife()
	   
	   {
		   return lives;
		   
		}
		
}
  


Game Window

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Color;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JMenuBar;
import javax.swing.JLabel;
import javax.swing.JFrame;
import javax.swing.SwingConstants;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.BorderLayout;
import java.awt.Font;

/**
 * Panel for displaying the maze game state.
 */
public class GameWindow extends JFrame
{
	/** Size in pixels to draw the cells of the maze. */
	private static final int CELL_SIZE = 40;
	
	private StatusView status;
	private Game game;
	private MazeView view;
	private JLabel label;
	private JMenuBar menuBar;
	private JMenu fileMenu;
	private JMenu nextgameItem;
	private JMenu closegameItem;
   
  
	public GameWindow(Game game)
	{
		super("game");
		this.game = game;
		setPreferredSize(new Dimension(CELL_SIZE * game.getMaze().getWidth(), CELL_SIZE * game.getMaze().getHeight()));
		setFocusable(true);
		MazeView view = new MazeView (game);
		status = new StatusView (game);
		this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		this.add(view);
		this.pack();
		this.setVisible(true);
		this.add(status, BorderLayout.PAGE_END);
		menuBar = new JMenuBar();
		fileMenu = new JMenu("file");
		nextgameItem = new JMenu("Next Game");
		closegameItem = new JMenu("Close Game");
		menuBar.add(fileMenu);
		this.setJMenuBar(menuBar);
		
	  
		ActionListener closegame = new closegameListener();
		closegameItem.addActionListener(closegame);
		
		ActionListener nextgame = new nextgameListener();
		nextgameItem.addActionListener(nextgame);
	}  
	  private class nextgameListener implements ActionListener
   {
		public void actionPerformed(ActionEvent e)
		{
		   game.nextgame();
		}

  
	}
	private class closegameListener implements ActionListener
	{
		public void actionPerformed(ActionEvent e)
		{
		   System.exit(0);
		}

	/**
	 * Draw a red dot showing where the player is.
	 */
	private void drawPlayer(Graphics g, Player player)
	{
		int x = player.getX() * CELL_SIZE;
		int y = player.getY() * CELL_SIZE;
		int dotSize = 10;
		int offset = (CELL_SIZE - dotSize)/2;
		g.setColor(Color.RED);
		g.fillOval(x + offset, y + offset, dotSize, dotSize);
	}
	
	/**
	 * Draw the cell which is at position (x,y) in the game grid.
	 */
	private void drawCell(Graphics g, int x, int y) 
	{
		/**this code gets the cell from the game class
		 * 
		 */
		Cell cell = game.getMaze().getCell(x,y);
		
		/**this code checks if created items exist
		 * 
		 */
		if  (cell.getitem() !=null)
	   {
		g.setColor(Color.RED);
		String s = cell.getitem().getitemname();
		int offset = CELL_SIZE/8;
		g.drawString(s, x * CELL_SIZE + offset, y * CELL_SIZE + (3 * offset));
		
	}

		g.setColor(Color.BLACK);
		g.drawRect(x * CELL_SIZE, y * CELL_SIZE, CELL_SIZE, CELL_SIZE);
		
	}
}
}




Thank you,


I have fixed the other mistakes 'm2s87' thank you,

the run time error is still present tho...

This post has been edited by Jet_Man: 20 April 2008 - 04:54 PM

Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8332
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Java RunTime Error

Posted 20 April 2008 - 05:50 PM

And now we have Cell missing in the chain
Was This Post Helpful? 0
  • +
  • -

#7 Jet_Man  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 07-April 08

Re: Java RunTime Error

Posted 20 April 2008 - 06:26 PM

heres cell


import java.util.Set;

/**
 * Cells in a maze. Each cell may be open to neighbours in
 * any (or none) of the directions up, right, down, left.
 */
public class Cell
{
	/** The directions in which this cell is open.**/
	private Set <String> openWalls;
   
	 /** The Items within the.**/
	private Item item;
  
	/**
	 * Initialise a new Cell. All walls are initially closed.
	 */
	public Cell()
	{
	   openWalls = new java.util.HashSet<String>();
	}
	
	/**
	 * Open the wall in the given direction.
	 * 
	 * @param d The direction.
	 */
	public void openWall(String d)
	{
		openWalls.add(d);
	}
	
	/**
	 * Close a wall.
	 * Has no effect if the cell is not open in that direction.
	 * 
	 * @param d The direction.
	 */
	public void closeWall(String d)
	{
		openWalls.remove(d);
	}
	
	/**
	 * Is this cell open in the given direction?
	 * 
	 * @param d The direction.
	 */
	public boolean isOpen(String d)
	{
		return openWalls.contains(d);
	}


 /**
	 * set the item
	 * 
	 * @param name
	 * 
	 */
	public void setitem(Item name)
	{
		item = name;
	}
	
	/**
	 * 
	 * 
	 * returns item
	 * 
	 */
	public Item getitem()
	
	{
		return item;
	}
	
}  
	

Was This Post Helpful? 0
  • +
  • -

#8 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8332
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Java RunTime Error

Posted 20 April 2008 - 06:36 PM

View PostJet_Man, on 20 Apr, 2008 - 06:26 PM, said:

heres cell


Thanks... what about Item, MazeView, StatusView ?

Not easy to find an error if we cannot run your code

This post has been edited by pbl: 20 April 2008 - 06:40 PM

Was This Post Helpful? 0
  • +
  • -

#9 Jet_Man  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 07-April 08

Re: Java RunTime Error

Posted 20 April 2008 - 07:39 PM

View Postpbl, on 20 Apr, 2008 - 06:36 PM, said:

View PostJet_Man, on 20 Apr, 2008 - 06:26 PM, said:

heres cell


Thanks... what about Item, MazeView, StatusView ?

Not easy to find an error if we cannot run your code




Item

 


/**
 * Write a description of class Item here.
 * 
 * @author () 
 * @version (a version number or a date)
 */
public class Item
{
	// instance variables - replace the example below with your own
	private String item;

	/**
	 * Constructor for objects of class Item
	 */
	public Item(String name)
	{
			// initialise instance variables
			item = name;
	}

	/**
	 * Return the Item
	 * 
	 */
	public String getitemname()
	{
			
			return item;
	}
	/**
	 * get the item
	 * 
	 */
	public void setItem(String name)
	{
	
		item = name;
	
	}
}




Status View


import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Color;
import javax.swing.JPanel;
import javax.swing.JFrame;

/**
 * Panel for displaying the maze game state.
 */
public class StatusView extends JPanel
{
	/** Size in pixels to draw the cells of the maze. */
	private static final int CELL_SIZE = 40;

	private Game game;
	
	public StatusView(Game game)
	{
		this.game = game;
		setPreferredSize(new Dimension(CELL_SIZE * game.getMaze().getWidth(), CELL_SIZE * game.getMaze().getHeight()));
		setFocusable(true);   
	}
	
	/**
	 * Draw the game state.
	 */
   public void paintComponent(Graphics g)
	{
		super.paintComponent(g);
		drawHealth(g);
	}

	   public void drawHealth(Graphics g)
	{
		
		String l = "Lives: "+ game.getPlayer().getlife();
		g.setColor(Color.blue);
		g.drawString(l, 10, 15);
		
		String p = "Score: "+ game.getPlayer().getscore();
	   g.setColor(Color.blue);
	   g.drawString(p, 10, 35);
	}
	
	
}

   


Maze View


import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Color;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.ImageIcon;

/**
 * Panel for displaying the maze game state.
 */
public class MazeView extends JPanel
{
	/** Size in pixels to draw the cells of the maze. */
	private static final int CELL_SIZE = 40;

	private Game game;
	private Maze maze;
	
	public MazeView(Game game)
	{
		this.game = game;
		setPreferredSize(new Dimension(CELL_SIZE * game.getMaze().getWidth(), CELL_SIZE * game.getMaze().getHeight()));
		setFocusable(true);   
	}
	
	/**
	 * Draw the game state.
	 */
   public void paintComponent(Graphics g)
	{
		super.paintComponent(g);
		for (int x = 0; x < game.getMaze().getWidth(); x++) {
			for (int y = 0; y < game.getMaze().getHeight(); y++) {
				if (game.getMaze().getCell(x, y) != null) {
					drawCell(g, x, y);
				}
			}
		}
		drawPlayer(g, game.getPlayer());
	}
	
	/**
	 * Draw a red dot showing where the player is.
	 */
	private void drawPlayer(Graphics g, Player player)
	{
		int x = player.getX() * CELL_SIZE;
		int y = player.getY() * CELL_SIZE;
		int dotSize = 10;
		int offset = (CELL_SIZE - dotSize)/2;
		g.setColor(Color.RED);
		g.fillOval(x + offset, y + offset, dotSize, dotSize);
	}
	
	/**
	 * Draw the cell which is at position (x,y) in the game grid.
	 */
	private void drawCell(Graphics g, int x, int y) 
	{
		/**this code gets the cell from the game class
		 * 
		 */
		Cell cell = game.getMaze().getCell(x,y);
		
		/**this code checks if created items exist
		 * 
		 */
		if  (cell.getitem() !=null)
	   {
		g.setColor(Color.RED);
		String s = cell.getitem().getitemname();
		int offset = CELL_SIZE/8;
		g.drawString(s, x * CELL_SIZE + offset, y * CELL_SIZE + (3 * offset));
		
	}

		g.setColor(Color.BLACK);
		g.drawRect(x * CELL_SIZE, y * CELL_SIZE, CELL_SIZE, CELL_SIZE);
		
	}
	
	public void drawItem (Graphics g, int x, int y, Cell cell)
  {
		int a = x * CELL_SIZE;
		  int b = y * CELL_SIZE;
		  Item item = cell.getitem();
		  if(item != null){
					if (item.getitemname().equals("health")) {
					ImageIcon icon = new ImageIcon("heart.gif");
					icon.paintIcon(this, g, a + -3 , b + 2);
					
				}if (item.getitemname().equals("shield")) {
					ImageIcon icon = new ImageIcon("shield.gif");
					icon.paintIcon(this, g, a + -3 , b + 2);
					
				}if (item.getitemname().equals("door")) {
				   ImageIcon icon = new ImageIcon("Passage.gif");
					icon.paintIcon(this, g, a + -3 , b + 2);
				}
			}
	  }
	  
}





Game


import java.util.ArrayList;
import java.io.*;
/**
 * Prototype "maze game" class. To play, create a new Game
 * object in BlueJ and invoke the goUp, goRight, etc commands
 * to move around the maze.
 */
public class Game
{
	/** The player. */
	private Player player;
	
	/**The List of Mazes created. */
	private ArrayList <Maze> listofMaze;
	
	/** A panel in which the game state is displayed. */
	private GameWindow view;
	
	/**
	 * current level in game
	 */
	 private int currentLevel;
	 
	/**
	 * Initialise a new Game.
	 */
	
	/**
	 *  the maze
	 */
	
   private Maze maze;
	
	public Game() throws IOException
	{
	   nextgame();
	   view = new GameWindow(this);
	   view.addKeyListener(new KeyControl());
	   
	 maze = listofMaze.get(currentLevel);

	   
	   player = new Player(getMaze().getStartX(), getMaze().getStartY());
	   
		if (!isValidPosition(player.getX(), player.getY())) {
			throw new Error("Player has started in the wrong position: (" + player.getX() + "," + player.getY() + ")");
		}
		
	}
	
   public void nextgame()
	{	   
			try {
				currentLevel = 0;
				listofMaze = new ArrayList<Maze>();
				createMazes();
				
			}catch(java.io.IOException e){}
		player = new Player(getMaze().getStartX(), getMaze().getStartY());
		if (!isValidPosition(player.getX(), player.getY())) {
			throw new Error("Player has started in the wrong position: (" + player.getX() + "," + player.getY() + ")");	 
		}
	}

	
	
	/**
	 * returns the maze object
	 */
	
	public Maze getMaze()
	{
		return listofMaze.get(currentLevel);
	}

	
	/**
	 * creates different mazes. Add items in two cells.  
	 * Add the mazes to the list of mazes.
	 */
	
   
   private void createMazes() throws IOException
   {
		
		listofMaze.add(new Maze ("Maze1-filetxt.txt"));
		listofMaze.get(0).setStart(7, 4);
		listofMaze.get(0).setFinish(5, 9);
		listofMaze.get(0).getCell (10, 6).setitem(new Item("life"));
		listofMaze.get(0).getCell (11, 8).setitem(new Item("door"));
	   
		listofMaze.add(new Maze ("Maze2-filetxt.txt"));
		listofMaze.get(1).setStart(6, 3);
		listofMaze.get(1).setFinish(8, 10);
		listofMaze.get(1).getCell (7, 2).setitem(new Item("life"));
		listofMaze.get(1).getCell (8, 10).setitem(new Item("door"));
		listofMaze.get(1).getCell (3, 2).setitem(new Item ("shield"));
		 
	 listofMaze.add(new Maze ("Maze3-filetxt.txt"));
		listofMaze.get(2).setStart(4, 2);
		listofMaze.get(2).setFinish(8, 10);
		listofMaze.get(2).getCell (7, 2).setitem(new Item("life"));
		listofMaze.get(2).getCell (8, 10).setitem(new Item ("door"));
		listofMaze.get(2).getCell (3, 2).setitem(new Item ("shield"));
		  
		
	}
	   
	
	  /**
	 * Move player into a certain position.
	 * Do nothing if the player's current position is not
	 * connected to a neighbour in that direction.
	 * 
	 * @param d The direction in which to move.
	 */
	
	public void go(String d)
	{
		int x = player.getX();
		int y = player.getY();
		Cell currentCell = getMaze().getCell(x, y);
		if (currentCell.isOpen(d)) {
			if (d.equals("up")) {
				player.setPosition(x, y-1);
			} else if (d.equals("right")) {
				player.setPosition(x+1, y);
			} else if (d.equals("down")) {
				player.setPosition(x, y+1);
			} else if (d.equals("left")) {
				player.setPosition(x-1, y);
			} else {
				throw new Error("Wrong direction: " + d);
			}
			if (!isValidPosition(player.getX(), player.getY())) {
				throw new Error("Wrong Move: (" + player.getX() + "," + player.getY() + ")");
			}
			
			if (getMaze().getFinishX() == player.getX() && getMaze().getFinishY() == player.getY())
			{
				if (currentLevel < listofMaze.size()-1)
				{
					currentLevel++;
					player.setPosition(getMaze().getStartX(), getMaze().getStartY());
				}
				else
				{
					System.exit(0);
				}
			} 
			Cell c = getMaze().getCell(player.getX(), player.getY());
				
				// Points are increased by 20 when the player enters this cell
				if (c.getitem()!= null && c.getitem().getitemname() == "shield") 
				{
				 player.setscore(20);
				 
				 c.setitem(null);
			}
			
			//One life is added for the player when this cell is entered
			 if (c.getitem()!= null && c.getitem().getitemname() == "Life") 
			   {
				player.setlife(1);
				 
			   c.setitem(null);
				 
			view.repaint();
		}
	}

}
 
	
	/**
	 * Attempt to move the player up.
	 * Do nothing if the player's current position is not
	 * connected to a neighbour in that direction.
	 * 
	 */
	public void goUp()
	{
		go("up");
	}
	
	
	/**
	 * Attempt to move the player right.
	 * Do nothing if the player's current position is not
	 * connected to a neighbour in that direction.
	 * 
	 */
	public void goRight()
	{
		go("right");
	}
	
	
	/**
	 * Attempt to move the player down.
	 * Do nothing if the player's current position is not
	 * connected to a neighbour in that direction.
	 * 
	 */
	public void goDown()
	{
		go("down");
	}
	
	
	/**
	 * Attempt to move the player left.
	 * Do nothing if the player's current position is not
	 * connected to a neighbour in that direction.
	 * 
	 */
	public void goLeft()
	{
		go("left");
}
	/**
	 * 
	 *Returns player
	 */
	
  public Player getPlayer()
	{
	   return player;
	}
		

	
	/**
	 * Is a given position a valid place for the player to be?
	 * 
	 * @param x The x-position to test.
	 * @param y The y-position to test.
	 */
	
	private boolean isValidPosition(int x, int y)
	{
		if (x >= 0 && x < getMaze().getWidth() && y >= 0 && y < getMaze().getHeight()) {
			return getMaze().getCell(x, y) !=null;
		} else {
			return false;
		}
	}
 
	
	private class KeyControl extends java.awt.event.KeyAdapter
	{
		public void keyPressed(java.awt.event.KeyEvent e)
		{
			int code = e.getKeyCode();
			if (code == java.awt.event.KeyEvent.VK_UP) {
				goUp();
			} else if (code == java.awt.event.KeyEvent.VK_RIGHT) {
				goRight();
			} else if (code == java.awt.event.KeyEvent.VK_DOWN) {
				goDown();
			} else if (code == java.awt.event.KeyEvent.VK_LEFT) {
				goLeft();
			}
		}
	}
}



hope this is evrything
Was This Post Helpful? 0
  • +
  • -

#10 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8332
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Java RunTime Error

Posted 20 April 2008 - 07:55 PM

Still Sub missing....
Hope doesn't call a few other classes
Better to isolate your probel son, won't be easy to scan a douzain of classes

This post has been edited by pbl: 20 April 2008 - 07:58 PM

Was This Post Helpful? 0
  • +
  • -

#11 Jet_Man  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 07-April 08

Re: Java RunTime Error

Posted 20 April 2008 - 08:13 PM

ermmm, sorry i dont know what you mean but i have the error message java gives me in red, maybe this can help solve the problem,

this is the error message:


java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
at java.util.ArrayList.RangeCheck(ArrayList.java:546)
at java.util.ArrayList.get(ArrayList.java:321)
at Game.getMaze(Game.java:73)
at Game.nextgame(Game.java:59)
at Game.<init>(Game.java:36)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:494)
at bluej.runtime.ExecServer$3.run(ExecServer.java:874)


Thanx,

Ps. all the classes from by project have been sent already.

This post has been edited by Jet_Man: 20 April 2008 - 08:15 PM

Was This Post Helpful? 0
  • +
  • -

#12 Jet_Man  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 07-April 08

Re: Java RunTime Error

Posted 21 April 2008 - 07:51 AM

hello pbl, just wanted to ask if you were having any luck with the runtime error, i hope the error message i sent you above is of any help im stuck on a dead end here....

sorry for the trouble.
Was This Post Helpful? 0
  • +
  • -

#13 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8332
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Java RunTime Error

Posted 21 April 2008 - 08:05 AM

View PostJet_Man, on 21 Apr, 2008 - 07:51 AM, said:

hello pbl, just wanted to ask if you were having any luck with the runtime error, i hope the error message i sent you above is of any help im stuck on a dead end here....

sorry for the trouble.


Don't understand how you can have a run time error your code does not even compile.

In Game:

	if (c.getitem()!= null && c.getitem().getitemname() == "Health") 
	{
		player.sethealth(1);

		c.setitem(null);

		view.repaint();
	}



Player.sethealth() undefined method

I will try it with setlife()

This post has been edited by pbl: 21 April 2008 - 08:06 AM

Was This Post Helpful? 0
  • +
  • -

#14 Jet_Man  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 07-April 08

Re: Java RunTime Error

Posted 21 April 2008 - 08:10 AM

Sorry, i did correct that before i ran it and it did complie i will send my new game class below,

yes setlife is the right one,

Game Class

import java.util.ArrayList;
import java.io.*;
/**
 * Prototype "maze game" class. To play, create a new Game
 * object in BlueJ and invoke the goUp, goRight, etc commands
 * to move around the maze.
 */
public class Game
{
	/** The player. */
	private Player player;
	
	/**The List of Mazes created. */
	private ArrayList <Maze> listofMaze;
	
	/** A panel in which the game state is displayed. */
	private GameWindow view;
	
	/**
	 * current level in game
	 */
	 private int currentLevel;
	 
	/**
	 * Initialise a new Game.
	 */
	
	/**
	 *  the maze
	 */
	
   private Maze maze;
	
	public Game() throws IOException
	{
	   nextgame();
	   view = new GameWindow(this);
	   view.addKeyListener(new KeyControl());
	   
	 maze = listofMaze.get(currentLevel);

	   
	   player = new Player(maze.getStartX(),maze.getStartY());
	   
		if (!isValidPosition(player.getX(), player.getY())) {
			throw new Error("Player has started in the wrong position: (" + player.getX() + "," + player.getY() + ")");
		}
		
	}
	
   public void nextgame()
	{	   
			try {
				currentLevel = 0;
				listofMaze = new ArrayList<Maze>();
				createMazes();
				
			}catch(java.io.IOException e){}
		player = new Player(getMaze().getStartX(), getMaze().getStartY());
		if (!isValidPosition(player.getX(), player.getY())) {
			throw new Error("Player has started in the wrong position: (" + player.getX() + "," + player.getY() + ")");	 
		}
	}

	
	
	/**
	 * returns the maze object
	 */
	
	public Maze getMaze()
	{
		return listofMaze.get(currentLevel);
	}

	
	/**
	 * creates different mazes. Add items in two cells.  
	 * Add the mazes to the list of mazes.
	 */
	
   
   private void createMazes() throws IOException
   {
		
		listofMaze.add(new Maze ("Maze1-filetxt.txt"));
		listofMaze.get(0).setStart(7, 4);
		listofMaze.get(0).setFinish(5, 9);
		listofMaze.get(0).getCell (10, 6).setitem(new Item("life"));
		listofMaze.get(0).getCell (11, 8).setitem(new Item("door"));
	   
		listofMaze.add(new Maze ("Maze2-filetxt.txt"));
		listofMaze.get(1).setStart(6, 3);
		listofMaze.get(1).setFinish(8, 10);
		listofMaze.get(1).getCell (7, 2).setitem(new Item("life"));
		listofMaze.get(1).getCell (8, 10).setitem(new Item("door"));
		listofMaze.get(1).getCell (3, 2).setitem(new Item ("shield"));
		 
	 listofMaze.add(new Maze ("Maze3-filetxt.txt"));
		listofMaze.get(2).setStart(4, 2);
		listofMaze.get(2).setFinish(8, 10);
		listofMaze.get(2).getCell (7, 2).setitem(new Item("life"));
		listofMaze.get(2).getCell (8, 10).setitem(new Item ("door"));
		listofMaze.get(2).getCell (3, 2).setitem(new Item ("shield"));
		  
		
	}
	   
	
	  /**
	 * Move player into a certain position.
	 * Do nothing if the player's current position is not
	 * connected to a neighbour in that direction.
	 * 
	 * @param d The direction in which to move.
	 */
	
	public void go(String d)
	{
		int x = player.getX();
		int y = player.getY();
		Cell currentCell = getMaze().getCell(x, y);
		if (currentCell.isOpen(d)) {
			if (d.equals("up")) {
				player.setPosition(x, y-1);
			} else if (d.equals("right")) {
				player.setPosition(x+1, y);
			} else if (d.equals("down")) {
				player.setPosition(x, y+1);
			} else if (d.equals("left")) {
				player.setPosition(x-1, y);
			} else {
				throw new Error("Wrong direction: " + d);
			}
			if (!isValidPosition(player.getX(), player.getY())) {
				throw new Error("Wrong Move: (" + player.getX() + "," + player.getY() + ")");
			}
			
			if (getMaze().getFinishX() == player.getX() && getMaze().getFinishY() == player.getY())
			{
				if (currentLevel < listofMaze.size()-1)
				{
					currentLevel++;
					player.setPosition(getMaze().getStartX(), getMaze().getStartY());
				}
				else
				{
					System.exit(0);
				}
			} 
			Cell c = getMaze().getCell(player.getX(), player.getY());
				
				// Points are increased by 20 when the player enters this cell
				if (c.getitem()!= null && c.getitem().getitemname() == "shield") 
				{
				 player.setscore(20);
				 
				 c.setitem(null);
			}
			
			//One life is added for the player when this cell is entered
			 if (c.getitem()!= null && c.getitem().getitemname() == "Life") 
			   {
				player.setlife(1);
				 
			   c.setitem(null);
				 
			view.repaint();
		}
	}

}
 
	
	/**
	 * Attempt to move the player up.
	 * Do nothing if the player's current position is not
	 * connected to a neighbour in that direction.
	 * 
	 */
	public void goUp()
	{
		go("up");
	}
	
	
	/**
	 * Attempt to move the player right.
	 * Do nothing if the player's current position is not
	 * connected to a neighbour in that direction.
	 * 
	 */
	public void goRight()
	{
		go("right");
	}
	
	
	/**
	 * Attempt to move the player down.
	 * Do nothing if the player's current position is not
	 * connected to a neighbour in that direction.
	 * 
	 */
	public void goDown()
	{
		go("down");
	}
	
	
	/**
	 * Attempt to move the player left.
	 * Do nothing if the player's current position is not
	 * connected to a neighbour in that direction.
	 * 
	 */
	public void goLeft()
	{
		go("left");
}
	/**
	 * 
	 *Returns player
	 */
	
  public Player getPlayer()
	{
	   return player;
	}
		

	
	/**
	 * Is a given position a valid place for the player to be?
	 * 
	 * @param x The x-position to test.
	 * @param y The y-position to test.
	 */
	
	private boolean isValidPosition(int x, int y)
	{
		if (x >= 0 && x < getMaze().getWidth() && y >= 0 && y < getMaze().getHeight()) {
			return getMaze().getCell(x, y) !=null;
		} else {
			return false;
		}
	}
 
	
	private class KeyControl extends java.awt.event.KeyAdapter
	{
		public void keyPressed(java.awt.event.KeyEvent e)
		{
			int code = e.getKeyCode();
			if (code == java.awt.event.KeyEvent.VK_UP) {
				goUp();
			} else if (code == java.awt.event.KeyEvent.VK_RIGHT) {
				goRight();
			} else if (code == java.awt.event.KeyEvent.VK_DOWN) {
				goDown();
			} else if (code == java.awt.event.KeyEvent.VK_LEFT) {
				goLeft();
			}
		}
	}
}





i think i accidently sent you and older game class before,

sorry
Was This Post Helpful? 0
  • +
  • -

#15 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8332
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Java RunTime Error

Posted 21 April 2008 - 08:53 AM

And where is your main() method ?
Didn't found one on all the classes you posted.
Was This Post Helpful? 0
  • +
  • -

  • (4 Pages)
  • +
  • 1
  • 2
  • 3
  • Last »