If you are familiar with the GridWorld case study, chapter 5 requires you to implement an unBoundedGrid using an array that doubles if necessary. (This is exercise 3)
I believe I have successfully implemented all the methods but when I convert my grid from a BoundedGrid to an UnBoundedGrid2, this occurs:
Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: info.gridworld.actor.Flower
at info.gridworld.gui.GridPanel.drawOccupants(GridPanel.java:191)
at info.gridworld.gui.GridPanel.paintComponent(GridPanel.java:110)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JViewport.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JLayeredPane.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JComponent.paintWithOffscreenBuffer(Unknown Source)
at javax.swing.JComponent.paintDoubleBuffered(Unknown Source)
at javax.swing.JComponent._paintImmediately(Unknown Source)
at javax.swing.JComponent.paintImmediately(Unknown Source)
at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
at javax.swing.SystemEventQueueUtilities$ComponentWorkRequest.run(Unknown Source)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
Here's my class:
import info.gridworld.grid.AbstractGrid;
import info.gridworld.grid.Location;
import java.util.ArrayList;
public class UnBoundedGrid2 extends AbstractGrid {
private Object[][] grid;
public UnBoundedGrid2(){
grid = new Object[16][16];
}
public Object get(Location loc) {
if(loc.getRow() >= this.getNumRows() || loc.getCol() >= this.getNumCols())
return null;
return grid[loc.getRow()][loc.getCol()];
}
public int getNumCols() {
return grid[0].length;
}
public int getNumRows() {
return grid.length;
}
public ArrayList getOccupiedLocations() {
ArrayList toReturn = new ArrayList();
for(int i = 0; i < getNumRows(); i++)
for(int j = 0; j < getNumCols(); j++)
if(grid[i][j] != null)
toReturn.add(grid[i][j]);
return toReturn;
}
public boolean isValid(Location loc) {
if(loc.getCol() < 0 || loc.getRow() < 0)
return false;
return true;
}
public Object put(Location loc, Object obj) {
if (!isValid(loc))
throw new IllegalArgumentException("Location " + loc
+ " is not valid");
if (obj == null)
throw new NullPointerException("obj == null");
while(loc.getRow() >= getNumRows() || loc.getCol() >= getNumCols())
resize();
Object old = get(loc);
grid[loc.getRow()][loc.getCol()] = obj;
return old;
}
public Object remove(Location loc) {
if (!isValid(loc))
throw new IllegalArgumentException("Location " + loc
+ " is not valid");
Object obj = grid[loc.getRow()][loc.getCol()];
grid[loc.getRow()][loc.getCol()] = null;
return obj;
}
public void resize(){
Object[][] temp = new Object[getNumRows() * 2][getNumCols() * 2];
for(int i = 0; i < getNumRows(); i++)
for(int j = 0; j < getNumCols(); j++)
temp[i][j] = grid[i][j];
grid = temp;
}
}
Can someone suggest a solution to this exception? I have already added this class to the World in the constructor.