GridWorld GUI

GIF animation class; Help me, anyone, for the love of all that's n

Page 1 of 1

3 Replies - 6159 Views - Last Post: 27 May 2009 - 08:31 PM Rate Topic: -----

#1 wolfwithinme  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 18-April 09

GridWorld GUI

Posted 14 May 2009 - 07:18 PM

Hi, I'm having some major problems figuring out this how to make the GUI for GridWorld accept animated gif images within the GridPanel of my home-brew Critter class, LaughingMan, and I was wondering if anyone could help me before i go insane. I'm including my source code of LaughingMan.java & Hacked.java (you need both for a successful Runner class to compile) for the convenience of anyone who can help me with this, as well as their images; If anyone wants to use my source code for teaching purposes, that's fine by me; just make sure you save the 2 files in the 'frameworks' folder of GridWorld, otherwise they won't compile correctly. As for the GUI issue, I tried using code examples from something called FileChooserDemo2.java and it's other subclassed files, so here is a link to them:

Koders Code Search: FileChooserDemo2

& here is the code and images I have so far:

LaughingMan.gif
Attached Image
LaughingMan.java
/**
 * @LaughingMan.java
 * @Lee Savide
 * @version 1.00 2009/4/17
 */

import info.gridworld.actor.*;
import info.gridworld.grid.*;
import java.util.ArrayList;
import java.awt.Color;

/**
 * A 'LaughingMan' is an actor that doesn't move through its world, but processes
 * other actors by deleting actors who "eat" 'Bug' actors when they pass the 'LaughingMan' actor. <br />
 * The implementation of this class isn't testable on the AP CS A and AB exams.
 */

public class LaughingMan extends Critter
{
	private LaughingMan warai;
	private Hacked hack;
	private Grid<Actor> grid;
	private ArrayList<Actor> actors;
	private ArrayList<Location> location;
	private ArrayList<Location> hackLocs;
	private Location loc;
	private Location next;
	private Location other;
	private static final int direction = Location.NORTH;

	/**
	 * Default 'LaughingMan' facing north in the grid.
	 */
	public LaughingMan()
	{
	Color c = null;
	setColor(c);
	getGrid();
	getLocation();
	location = new ArrayList<Location>();
	location.add(getLocation());
	setDirection(direction);
	}

	/**
	 * 'LaughingMan' processes actors in all 8 directions around itself, finds specific actors to process, and acts on them.
	 * Preconditions:
	 * 1.) 'LaughingMan' doesn't not move.
	 * 2.) 'LaughingMan' replaces 'CrabCritter' and 'Jumper' actors with new 'LaughingMan' actors.
	 * 3.) 'LaughingMan' replaces 'Critter' and 'ChameleonCritter' actors with 'Hacked' actors.
	 */
	protected void hackActors(ArrayList<Actor> actors)
	{
	hack = new Hacked();
	grid = getGrid();
	actors = getActors();
		for(Actor a : actors)
		{
			if(!(a instanceof LaughingMan))
			{
				if((a instanceof Jumper) || (a instanceof CrabCritter))
				{
				warai = new LaughingMan();
				next = a.getLocation();
				a.removeSelfFromGrid();
				warai.putSelfInGrid(grid, next);
				}

				else if((a instanceof ChameleonCritter) || (a instanceof Critter))
				{
				hack = new Hacked();
				next = a.getLocation();
				a.removeSelfFromGrid();
				hack.putSelfInGrid(grid, next);
				}
			}

			/**
			 * These actors are specified because
			 * since 'Jumper' extends the 'Actor'
			 * class, searching for the instances
			 * of 'Actor' would make 'LaughingMan'
			 * ignore every 'Jumper', and wouldn't
			 * run as intended.
			 */
			if((a instanceof Flower) || (a instanceof Hacked) || (a instanceof Bug) || (a instanceof Rock))
			{
			continue;
			}

			else
			{
			//This applies to the any other actors.
			continue;
			}
		}
	}

	/**
	 * Returns the location for 'LaughingMan' to put a 'Hacked' actor in.
	 * @return next the location to put the 'Hacked' actor.
	 */
	public Location selectMoveLocation(ArrayList<Location> hackLocs)
	{
	actors = getActors();
		for(Actor a : actors)
		{
			if(!(a instanceof LaughingMan) || (a instanceof Critter) || (a instanceof Jumper))
			{
			next = a.getLocation();
			hackLocs.add(next);
			}

			if(a instanceof Actor)
			{
			continue;
			}

			else //This applies to the standard 'Actor' actor.
			{
			break;
			}
		}
	return next;
	}

	/**
	 * Identifies the locations that a 'LaughingMan' can access.
	 * @return the other actors within the 8 locations
	 * surrounding the 'LaughingMan'.
	 */
	protected ArrayList<Location> getMoveLocations(Location loc)
	{
	return getGrid().getOccupiedAdjacentLocations(getLocation());
	}

	/**
	 * Identifies the other actors in the grid.
	 * @return the other actors in the grid
	 */
	public ArrayList<Actor> getActors()
	{
	return getGrid().getNeighbors(getLocation());
	}

	/**
	 * When 'LaughingMan' acts, it will identify the other actors & their locations,
	 * then select one randomly and either destroy that actor with a 'Hacked' actor, or leave it alone.
	 */
	public void act()
	{
		if(getGrid() == null)
		{
		return;
		}
	hackLocs = getMoveLocations(loc);
	other = selectMoveLocation(hackLocs);
	hackActors(actors);
	}

	/**
	 * Prints info about a 'LaughingMan' to the screen.
	 * @return the class name and motto of 'LaughingMan'.
	 */
	public String toString()
	{
	return getClass().getName() + " I Thought What I'd Do Was, I'd Pretend I Was One Of Those Deaf-Mutes";
	}
}


Hacked.gif
Attached Image
Hacked.java
/**
 * @Hacked.java
 * @Lee Savide
 * @version 1.00 2009/4/17
 */

import info.gridworld.actor.*;
import info.gridworld.grid.*;
import java.util.ArrayList;
import java.awt.Color;


public class Hacked extends Actor
{
	private Hacked hack;
	private Grid<Actor> grid;
	private ArrayList<Actor> actors;
	private ArrayList<Location> location;
	private ArrayList<Location> hackLocs;
	private Location loc;
	private Location next;
	private int numSteps;
	private int sideLength;
	private int length;
	private Color c1;
	private static final int direction = Location.NORTH;

	/**
	 * Default 'Hacked', facing north in the grid.
	 */
	public Hacked()
	{
	c1 = null;

	getGrid();
	getLocation();
	location = new ArrayList<Location>();
	location.add(getLocation());
	setDirection(direction);
	}

	/**
	 * 'Hacked' processes what grid it's in, what location it's at, and what actors there are.
	 * Postcondition:
	 * 'Hacked' actors delete themselves upon taking one step after being brought into the grid.
	 */
	protected void process(ArrayList<Actor> actors)
	{
	grid = getGrid();
	loc = getLocation();
	actors = getActors();
	removeSelf();
	}

	/**
	 * A 'Hacked' actor acts by deleting
	 * itself after taking a single step.
	 */
	public void act()
	{
		if(getGrid() == null)
		{
		return;
		}
	actors = getActors();
	process(actors);
	}

	/**
	 * Indicates when 'Hacked' needs to remove itself.
	 */
	protected void removeSelf()
	{
	length = 1;
	sideLength = length;
		for(numSteps = 0; numSteps < sideLength; numSteps++)
		{
		removeSelfFromGrid();
		}
	}

	/**
	 * Identifies the other actors in the grid.
	 * @return the other actors and their locations.
	 */
	protected ArrayList<Actor> getActors()
	{
	return getGrid().getNeighbors(getLocation());
	}

	protected ArrayList<Location> getMoveLocations()
	{
	hackLocs = getGrid().getOccupiedAdjacentLocations(getLocation());
	return hackLocs;
	}
}


AnimationDriver.java
/**
 * @AnimationDriver.java
 * @Lee Savide
 * @version 1.00 2009/5/14
 */

package info.gridworld.animate;

import info.gridworld.animate.ImageFileView;
import info.gridworld.animate.ImageFilter;
import info.gridworld.animate.ImagePreview;
import info.gridworld.animate.Utils;
import info.gridworld.animate.AnimatedGifEncoder;
import info.gridworld.animate.GifDecoder;
import info.gridworld.animate.LZWEncoder;
import info.gridworld.animate.NeuQuant;

import info.gridworld.actor.LaughingMan;
import info.gridworld.gui.GridPanel;
import info.gridworld.world.World;

import java.io.*;

import java.awt.*;
import java.awt.image.*;

import java.net.*;
import java.util.*;
import java.beans.*;

import javax.swing.*;
import javax.swing.filechooser.*;
import javax.imageio.*;

public class AnimationDriver implements ImageProducer, ImageConsumer
{
	private String extRef = "gif";
	private String waraiPath = "/Documents and Settings/Lee Savide/My Documents/AP Computer Science/ap06_compsci_GridWorld_Code/GridWorldCode/framework/info/gridworld/actor/LaughingMan.gif";
	private char sep = '/';
	private char ext = '.';
	private Image warai = new Image();
	private static final int HEIGHT = 2;
	private static final int WIDTH = 1;
	private static final int SCALE_SMOOTH = 4;
	private static final int X = /*?*/;
	private static final int Y = /*?*/;

	ImageObserver obs.imageUpdate(warai, int infoflags,  /*?*/,  /*?*/, WIDTH, HEIGHT);
	public abstract boolean drawImage(warai, int x, int y, obs);

	public AnimationDriver()
	{
	AnimatedGifEncoder e = new AnimatedGifEncoder();
	GifDecoder d = new GifDecoder();
		if(ImageFileView.getTypeDescription(warai) != null)
		{
			if(ImageFileView.getTypeDescription(warai) == Utils.gif)
			{
			ImageDisplay(LaughingMan);
			}
		}
	}

	public Image laugh()
	{
	warai.getGraphics();
	warai.getSource();
	warai.getProperty(waraiPath, obs);
	warai.getScaledInstance(getWidth(), getHeight(), SCALE_SMOOTH);
	}
}


If anyone could help, I'd probably might consider making you my second god. Just kidding! But I'd be exceedingly grateful; I'm just a high school student, so i have absolutely no clue how to work out how GUIs work. Thanks!

Is This A Good Question/Topic? 0
  • +

Replies To: GridWorld GUI

#2 wolfwithinme  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 18-April 09

Re: GridWorld GUI

Posted 14 May 2009 - 08:14 PM

I'm not asking for the whole answer; just that the gif file is properly referenced within the code. I'll come up with the rest on my own.

This post has been edited by wolfwithinme: 14 May 2009 - 08:18 PM

Was This Post Helpful? 0
  • +
  • -

#3 wolfwithinme  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 18-April 09

Re: GridWorld GUI

Posted 14 May 2009 - 08:27 PM

anyone?
Was This Post Helpful? 0
  • +
  • -

#4 TedSSBM  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 36
  • Joined: 19-March 09

Re: GridWorld GUI

Posted 27 May 2009 - 08:31 PM

View Postwolfwithinme, on 14 May, 2009 - 07:14 PM, said:

I'm not asking for the whole answer; just that the gif file is properly referenced within the code. I'll come up with the rest on my own.


I feel your pain man, I am in the AP Computer Science class at my school as well. We tried to get an animated gif to show properly in the grid, but all 4 of us together couldn't do it. Im a senior, so Im going to graduate soon, but im keeping gridworld because is fun to tinker with.

Oh and if you told me how to change gridworld's default grid color from white to some other color i WOULD make you my second god.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1