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

Recommended Answers

All 7 Replies

I am assuming that walk left foot and right foot both calls 1 step. I am not sure that is the right way to count as step... If it is, then please read the comment below.

1)Why do you have firstStep() method? nextStep() does the same job. You could initial the 'stepsCount=0;' inside your constructor. Also, you could implement 'resetStepCount()' and reset the stepsCount to 0 instead.
2)In 1st portion of code line 60, why distanceTraveled() returns 0? Why does not it return stepsCount?
3)In 2nd portion of code line 65, your variables have moved 1 step already, but your assign 0 to stepsCount?
4.Why do you check only amy in the 2nd portion? amy is Walker class but you only gave Hopper class. Or Walker extends Hopper class?

I have a feeling that your instructor wants you to break the 'step' into left and right, not both left+right equal to a step...

commented: Thanks! +0

In 1, we will combine the methods into one called makeNsteps(). We understand that in your list of events, 2 and 3 we messed up. We originally had them what you said, but while trying to fix our problem we changed them and forgot to change them back. Thanks for that. As for number 4, Walker does not extend Hopper. They are separate classes. We did not show Walker because if we can fix the problem in Hopper, it should be the same in Walker. If you would like to see Walker, we can post it. This source along with others in a project, when running with a .JAR file, shows three pairs of feet. ben, amy, and cat. cat is the hopper, ben and amy are walkers. There is a button on the side that at first says "go", when you click that the feet start to move and the button changes to "stop". What our instructor wants is when we click the "stop" button, the feet should stop where they are. When we click the button now, they reset and go back to the beginning.

OK. I understand a lot better. Now what does the 'stop' button call on your display class? Does your display class work similarly to a JFrame? The display should not be reset unless your 'stop' does something like initiate those variables again.

Also, in order to stop where it is, you may need to break your 'step' out in to 'leftStep' and 'rightStep', but increment the stepsCount only when 'rightStep' is completed. That way, you should be able to stop it right where it should be.

OK. I understand a lot better. Now what does the 'stop' button call on your display class? Does your display class work similarly to a JFrame? The display should not be reset unless your 'stop' does something like initiate those variables again.

Also, in order to stop where it is, you may need to break your 'step' out in to 'leftStep' and 'rightStep', but increment the stepsCount only when 'rightStep' is completed. That way, you should be able to stop it right where it should be.

Well we are not really sure where exactly the feet need to stop, he may tell us to stop the feet after the first step, or he may tell us to stop the fight right before the reach the opposite end of the screen. Would separating the 'step' into 'leftStep' and 'rightStep' be worth doing if we are not sure how long they will be walking. The steps completion needs to be whenever we click the stop button on the screen. Also, I'm not really sure what you are asking when you say "what does the 'stop' button call on your display class?" What are you refering to as our display class? Thank you very much though for the imformation you have provided us with. We are finally on the right track and I believe we are getting very close to what we need to do. -From our computer to yours- zach&kody

I guess you have to ask your instructor about what he/she really wants about the stop on display.

There is a button on the side that at first says "go," when you click that the feet start to move and the button changes to "stop."

The quote above is what I want to know. What the method that the button "stop" calls? If you have a button "go" to start, would it call setup() and then go()? If so, what does the button "stop" call? There must be a method that the stop needs to call and need to call a different one too.

I guess you have to ask your instructor about what he/she really wants about the stop on display.

The quote above is what I want to know. What the method that the button "stop" calls? If you have a button "go" to start, would it call setup() and then go()? If so, what does the button "stop" call? There must be a method that the stop needs to call and need to call a different one too.

Oh, I understand the question now, but this is something that neither of us are sure about. We are wondering this ourselves. Would you like to see some of the other programs we have in our project? -From our computer to yours- zach&kody

Hmm... I just want to see how you create & call your class in main(). That may give me an idea about how the display is created.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.