Welcome to Dream.In.Code
Getting Java Help is Easy!

Join 132,161 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,736 people online right now. Registration is fast and FREE... Join Now!




Feet

 
Reply to this topicStart new topic

Feet, Zigzag correction

gormanator92
post 10 Oct, 2008 - 08:39 AM
Post #1


New D.I.C Head

*
Joined: 9 Oct, 2008
Posts: 16

All right, I am sorry for my last zigzag as it was unclear. The project was to make the feet walk in a diamond or up and down the screen while moving forwards(i chose diamond). It does not matter if they go off the screen, just so long as it looks like they are actually walking and the distance between them stays the same. My problem is that at each turn except the first the feet move too far away from each other. I would appreciate any help. I will post 3 codes for reference. The .jar file only does what I want it to if it is with all the files from the project, so I will not be attatching it. I will post other files for reference at request. Any help is much appreciated.
( I dont know if it has an impact, but I use JCreator to run my Java programs.)

My first file, is ZigZag.
CODE
// Question 3-8

// A subclass of Walker that adds the turnAround method.

import java.awt.Image;

public class ZigZag extends Pacer
{
  // Constructor
  public ZigZag(int x, int y, Image leftPic, Image rightPic)
  {
    super(x, y, leftPic, rightPic);
  }
  
  public void firstTurn()
  {
      Foot lf = getLeftFoot();
      Foot rf = getRightFoot();
      lf.turn(45);
      rf.turn(45);
    lf.moveSideways(-PIXELS_PER_INCH * 8);
    lf.moveForward(PIXELS_PER_INCH * 8);
  }

      
  

Next is Pacer
CODE
// Question 3-8

// A subclass of Walker that adds the turnAround method.

import java.awt.Image;

public class Pacer extends Walker
{
  // Constructor
  public Pacer(int x, int y, Image leftPic, Image rightPic)
  {
    super(x, y, leftPic, rightPic);
  }

  // Turns this Pacer 180 degrees
  // Precondition: the left and right feet are side by side
  public void turnAround()
  {
    Foot lf = getLeftFoot();
    Foot rf = getRightFoot();
    lf.turn(180);
    rf.turn(180);
    lf.moveSideways(-PIXELS_PER_INCH * 8);
    rf.moveSideways(PIXELS_PER_INCH * 8);
  }

  // Turns this Pacer left 90 degrees
  // Precondition: the left and right feet are side by side
  public void turnLeft()
  {
    Foot lf = getLeftFoot();
    Foot rf = getRightFoot();
    lf.turn(-90);
    rf.turn(-90);
    rf.moveSideways(PIXELS_PER_INCH * 8);
    rf.moveForward(PIXELS_PER_INCH * 8);
  }

  // Turns this Pacer right 90 degrees
  // Precondition: the left and right feet are side by side
  public void turnRight()
  {
    Foot lf = getLeftFoot();
    Foot rf = getRightFoot();
    lf.turn(90);
    rf.turn(90);
    lf.moveSideways(-PIXELS_PER_INCH * 8);
    lf.moveForward(PIXELS_PER_INCH * 8);
  }
}


My last code is PacingGroup which organizes the methodss into what they do and when they should do it.

CODE

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class PacingGroup implements StudentGroup
{
  private ZigZag ben;
  private Image leftWomansShoe, rightWomansShoe;
  private Image leftMansShoe, rightMansShoe;

  private DanceFloor danceFloor;

  private enum State {READY, MOVING, STOPPED, PREPARED}
  private State currentState;
  private int stepsCount;

  // Constructor
  public PacingGroup(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();
  }

  // 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)
    {
      ben = new ZigZag(x, height / 2, leftMansShoe, rightMansShoe);
    }
    else
    {
      ben = new ZigZag(width, height / 2,
                                          leftMansShoe, rightMansShoe);
    }
    currentState = State.READY;
    danceFloor.update(this);
  }

  // Moves the group by one step
  public void makeNextStep()
  {
    if (currentState == State.READY)
    {
      ben.firstTurn();
      currentState = State.PREPARED;
      stepsCount = 0;
    }
    else if (currentState == State.PREPARED)
    {
        ben.firstStep();
        currentState = State.MOVING;
        stepsCount = 0;
    }
    else if (currentState == State.MOVING)
    {
      if (ben.distanceTraveled() <= danceFloor.getHeight() * 1 / 4)
      {
        ben.nextStep();
        stepsCount++;
      }
      else
      {
          ben.stop();
        currentState = State.STOPPED;
      }
    }
    else if (currentState == State.STOPPED)
    {
      ben.turnLeft();
      currentState = State.PREPARED;
      stepsCount = 0;
    }

    danceFloor.update(this);
  }

  // Draws this group
  public void draw(Graphics g)
  {
    ben.draw(g);
  }
}


This post has been edited by gormanator92: 10 Oct, 2008 - 08:40 AM
User is offlineProfile CardPM

Go to the top of the page

gormanator92
post 10 Oct, 2008 - 02:28 PM
Post #2


New D.I.C Head

*
Joined: 9 Oct, 2008
Posts: 16

The other files you may need are walker, coordinate Systems, and foot.
Again i appreciate anyone's time to even read this.
User is offlineProfile CardPM

Go to the top of the page

Gloin
post 11 Oct, 2008 - 07:26 AM
Post #3


On MeD.i.Cation

Group Icon
Joined: 4 Aug, 2008
Posts: 717



Thanked 46 times
My Contributions


Alrighty.. I assume that the problem is that when the feet turn 90 degrees you don't get the result you want. When turning 180 degrees it should be fine though, am I right?

Right now you get this result

|| becomes - - while actually what you want is -> = (sorry for the poor graphics)

am I right?

If not, can you try to describe (give the postcondition) what result you want?
User is offlineProfile CardPM

Go to the top of the page

gormanator92
post 11 Oct, 2008 - 10:20 AM
Post #4


New D.I.C Head

*
Joined: 9 Oct, 2008
Posts: 16

i get what ur trying to show me, you are sort of right. The feet just spread really far apart and it is = tilted 45 degrees because of the firststep in ZigZag. Then when it reaches the bottom of the screen it just turns but the feet are not a good distance from each other.
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 11/21/08 02:13PM

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month