Koders Code Search: FileChooserDemo2
& here is the code and images I have so far:
LaughingMan.gif

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

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!

New Topic/Question
Reply



MultiQuote





|