Once again, my friend Kody and I have come to a dilema. We made a Hopper class which makes 2 feet hop forward by our int stepLength. We placed a stop method to stop the feet at the end of the screen, but our instructor wants the feet directly in place whenever we press the "stop" button. Which right now, starts the feet at the beginning again. Here is the source we have been working the most with, and a parallel source used to gain predetermined methods.
// This class represents a walker with two feet.
import java.awt.Image;
import java.awt.Graphics;
public class Hopper
{
public static final int PIXELS_PER_INCH = 6;
private Foot leftFoot, rightFoot;
private int stepLength;
private int stepsCount;
private int stepLength2;
// Constructor
public Hopper(int x, int y, Image leftPic, Image rightPic)
{
leftFoot = new Foot(x, y - PIXELS_PER_INCH * 4, leftPic);
rightFoot = new Foot(x, y + PIXELS_PER_INCH * 4, rightPic);
stepLength = PIXELS_PER_INCH * 12;
stepLength2 = PIXELS_PER_INCH * 6;
}
// Returns the left foot
public Foot getLeftFoot()
{
return leftFoot;
}
// Returns the right foot
public Foot getRightFoot()
{
return rightFoot;
}
// Makes first step, starting with the left foot
public void firstStep()
{
leftFoot.moveForward(stepLength);
rightFoot.moveForward(stepLength);
stepsCount = 1;
}
// Makes next step
public void nextStep()
{
leftFoot.moveForward(stepLength);
rightFoot.moveForward(stepLength);
stepsCount++; // increment by 1
}
// Stops this walker (brings its feet together)
public void stop()
{
stepsCount++; // increment by 1
}
// Returns the distance walked
public int distanceTraveled()
{
return 0;
}
// Draws this walker
public void draw(Graphics g)
{
rightFoot.draw(g);
leftFoot.draw(g);
}
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class HoppingGroup implements StudentGroup
{
private Walker amy;
private Walker ben;
private Hopper cat;
private Image leftWomansShoe, rightWomansShoe;
private Image leftMansShoe, rightMansShoe;
private Image leftCatsPaw, rightCatsPaw;
private DanceFloor danceFloor;
private enum State {READY, MOVING, STOPPED}
private State currentState;
private int stepsCount;
// Constructor
public HoppingGroup(DanceFloor df)
{
danceFloor = df;
leftWomansShoe = (new ImageIcon("leftsandal.gif")).getImage();
rightWomansShoe = (new ImageIcon("rightsandal.gif")).getImage();
leftMansShoe = (new ImageIcon("leftshoe.gif")).getImage();
rightMansShoe = (new ImageIcon("rightshoe.gif")).getImage();
leftCatsPaw = (new ImageIcon("leftpaw.gif")).getImage();
rightCatsPaw = (new ImageIcon("rightpaw.gif")).getImage();
}
// Sets up this group of participants
public void setup(int floorDir, Dance steps1, Dance steps2)
{
int width = danceFloor.getWidth();
int height = danceFloor.getHeight();
int x = width / 10;
int y = height / 2;
if (floorDir == 0)
{
amy = new Walker(x, y - height / 5, leftWomansShoe, rightWomansShoe);
ben = new Walker(x, y + height / 5, leftMansShoe, rightMansShoe);
cat = new Hopper(x, y, leftCatsPaw, rightCatsPaw);
}
else
{
amy = new Walker(x, y + height / 5, leftWomansShoe, rightWomansShoe);
ben = new Walker(x, y - height / 5, leftMansShoe, rightMansShoe);
cat = new Hopper(x, y, leftCatsPaw, rightCatsPaw);
}
currentState = State.READY;
danceFloor.update(this);
}
// Moves the group by one step
public void makeNextStep()
{
if (currentState == State.READY)
{
amy.firstStep();
ben.firstStep();
cat.firstStep();
currentState = State.MOVING;
stepsCount = 0;
}
else if (currentState == State.MOVING)
{
if (amy.distanceTraveled() <= danceFloor.getWidth() * 3 / 4)
{
amy.nextStep();
ben.nextStep();
cat.nextStep();
stepsCount++;
}
else
{
amy.stop();
ben.stop();
cat.stop();
currentState = State.STOPPED;
}
}
danceFloor.update(this);
}
// Draws this group
public void draw(Graphics g)
{
amy.draw(g);
ben.draw(g);
cat.draw(g);
}
}
If you need other source files from our project please let us know. -From our computer to yours- zach&kody