caterpillar application help!

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • NewJack
    New Member
    • Oct 2006
    • 6

    #1

    caterpillar application help!

    Hi,

    I am currently taking a Java Programming course and the assignment is to create an application (or applet) in which a caterpillar or any other type of bug goes up a set of steps. If anyone can help me out with some code, advice, etc, that would be very VERY helpful.

    Thank you,
    NewJack
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by NewJack
    Hi,

    I am currently taking a Java Programming course and the assignment is to create an application (or applet) in which a caterpillar or any other type of bug goes up a set of steps. If anyone can help me out with some code, advice, etc, that would be very VERY helpful.

    Thank you,
    NewJack
    And what have you done so far.

    Comment

    • NewJack
      New Member
      • Oct 2006
      • 6

      #3
      I have already finished an application in which the caterpillar slinks down a set of stairs. I just need to make a different type of bug go UP the set. I'm having a lot of trouble with it so any help would be great.

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Originally posted by NewJack
        I have already finished an application in which the caterpillar slinks down a set of stairs. I just need to make a different type of bug go UP the set. I'm having a lot of trouble with it so any help would be great.
        Since you have already done a lot concerning this, it would be unfair to want people to help you by starting from scratch. Perhaps you can post what you have where you are stuck so that those helping can start from there otherwise we would all start it in a different way from the way you had already done it.

        Comment

        • NewJack
          New Member
          • Oct 2006
          • 6

          #5
          Allright, here's what I have so far...it's the lines of code to draw the set of stairs and that's where I need help at, in drawing the bug and moving it up the stairs..

          import java.awt.Color;
          import java.awt.Graphi cs;
          import javax.swing.JAp plet;

          public class Test extends JApplet
          {
          public void paint (Graphics g)
          {
          //stairs
          g.setColor(Colo r.BLACK);
          g.drawLine(0,90 ,90,90);
          g.drawLine(90,9 0,90,180);
          g.drawLine(90,1 80,180,180);
          g.drawLine(180, 180,180,270);
          g.drawLine(180, 270,270,270);
          }
          void marcsPause(long ms)
          {

          try
          {
          Thread.sleep(ms );
          }
          catch(Interrupt edException e) {}

          }

          }

          Comment

          • NewJack
            New Member
            • Oct 2006
            • 6

            #6
            any suggestions?

            Comment

            • r035198x
              MVP
              • Sep 2006
              • 13225

              #7
              Originally posted by NewJack
              any suggestions?
              O sorry about that. I think all you need to do is change the one you have a little to get the desired effect. The principles are still the same. So if you can post the one where you have the bug moving down ...

              Comment

              • NewJack
                New Member
                • Oct 2006
                • 6

                #8
                Yeah, I will post the original, it may take me some time b/c it is pretty long, but I'll have it up soon, thanks 4 your help.

                Comment

                • NewJack
                  New Member
                  • Oct 2006
                  • 6

                  #9
                  here's the orignal in its entirety...

                  import java.awt.Color;
                  import java.awt.Graphi cs;
                  import javax.swing.JAp plet;

                  public class Worms extends JApplet
                  {
                  int y = 0;

                  public void paint(Graphics g)

                  { //stairs
                  g.setColor(Colo r.BLUE);
                  g.drawLine(0,90 ,90,90);
                  g.drawLine(90,9 0,90,180);
                  g.drawLine(90,1 80,180,180);
                  g.drawLine(180, 180,180,270);
                  g.drawLine(180, 270,270,270);

                  //drawred1
                  for(int x = 0; x < 30; x = x + 30)
                  {
                  y = 60;
                  drawMyOvalred( g, x);
                  marcsPause(200) ;
                  }

                  showStatus("Com e on little guy, you can do it!");

                  //drawgreen1
                  for(int x = 30; x < 60; x = x + 30)
                  {
                  y = 60;
                  drawMyOvalgreen ( g, x);
                  marcsPause(200) ;
                  }

                  //drawblue1
                  for(int x = 60; x < 90; x = x + 30)
                  {
                  y = 60;
                  drawMyOvalblue( g, x);
                  marcsPause(200) ;
                  }

                  //erase red 2
                  for(int x = 90; x < 120; x = x + 30)
                  {
                  y = 60;
                  drawMyOvalred( g, x);
                  marcsPause(200) ;
                  }

                  //draw red1
                  for(int x = 0; x < 30; x = x + 30)
                  {
                  y = 60;
                  eraseMyOval( g, x);
                  marcsPause(200) ;
                  }
                  //erase green 2
                  for(int x = 90; x < 120; x = x + 30)
                  {
                  y = 90;
                  drawMyOvalgreen ( g, x);
                  marcsPause(200) ;

                  }

                  //draw green 1
                  for(int x = 30; x < 60; x = x + 30)
                  {
                  y = 60;
                  eraseMyOval( g, x);
                  marcsPause(200) ;
                  }
                  //erase blue 2
                  for(int x = 90; x < 120; x = x + 30)
                  {
                  y = 120;
                  drawMyOvalblue( g, x);
                  marcsPause(200) ;
                  }
                  //draw blue 1
                  for(int x = 60; x < 90; x = x + 30)
                  {
                  y = 60;
                  eraseMyOval( g, x);
                  marcsPause(200) ;
                  }
                  //erase red 3
                  for(int x = 90; x < 120; x = x + 30)
                  {
                  y = 150;
                  drawMyOvalred( g, x);
                  marcsPause(200) ;
                  }
                  //draw red 2
                  for(int x = 90; x < 120; x = x + 30)
                  {
                  y = 60;
                  eraseMyOval( g, x);
                  marcsPause(200) ;
                  }
                  //erase green 3
                  for(int x = 120; x < 150; x = x + 30)
                  {
                  y = 150;
                  drawMyOvalgreen ( g, x);
                  marcsPause(200) ;
                  }
                  //draw green 2
                  for(int x = 90; x < 120; x = x + 30)
                  {
                  y = 90;
                  eraseMyOval( g, x);
                  marcsPause(200) ;
                  }
                  //erase blue 3
                  for(int x = 150; x < 180; x = x + 30)
                  {
                  y = 150;
                  drawMyOvalblue( g, x);
                  marcsPause(200) ;
                  }
                  //draw blue 2
                  for(int x = 90; x < 120; x = x + 30)
                  {
                  y = 120;
                  eraseMyOval( g, x);
                  marcsPause(200) ;
                  }
                  //erase red 4
                  for(int x = 180; x < 210; x = x + 30)
                  {
                  y = 150;
                  drawMyOvalred( g, x);
                  marcsPause(200) ;
                  }
                  //draw red 3
                  for(int x = 90; x < 120; x = x + 30)
                  {
                  y = 150;
                  eraseMyOval( g, x);
                  marcsPause(200) ;
                  }
                  //erase green 4
                  for(int x = 180; x < 210; x = x + 30)
                  {
                  y = 180;
                  drawMyOvalgreen ( g, x);
                  marcsPause(200) ;
                  }
                  //draw green 3
                  for(int x = 120; x < 150; x = x + 30)
                  {
                  y = 150;
                  eraseMyOval( g, x);
                  marcsPause(200) ;
                  }
                  //erase blue 4
                  for(int x = 180; x < 210; x = x + 30)
                  {
                  y = 210;
                  drawMyOvalblue( g, x);
                  marcsPause(200) ;
                  }
                  //draw blue 3
                  for(int x = 150; x < 180; x = x + 30)
                  {
                  y = 150;
                  eraseMyOval( g, x);
                  marcsPause(200) ;
                  }
                  //erase red 5
                  for(int x = 180; x < 210; x = x + 30)
                  {
                  y = 240;
                  drawMyOvalred( g, x);
                  marcsPause(200) ;
                  }
                  //draw red 4
                  for(int x = 180; x < 210; x = x + 30)
                  {
                  y = 150;
                  eraseMyOval( g, x);
                  marcsPause(200) ;
                  }
                  //erase green 5
                  for(int x = 210; x < 240; x = x + 30)
                  {
                  y = 240;
                  drawMyOvalgreen ( g, x);
                  marcsPause(200) ;
                  }
                  //draw green 4
                  for(int x = 180; x < 210; x = x + 30)
                  {
                  y = 180;
                  eraseMyOval( g, x);
                  marcsPause(200) ;
                  }
                  //erase blue 5
                  for(int x = 240; x < 270; x = x + 30)
                  {
                  y = 240;
                  drawMyOvalblue( g, x);
                  marcsPause(200) ;
                  }
                  //draw blue 4
                  for(int x = 180; x < 210; x = x + 30)
                  {
                  y = 210;
                  eraseMyOval( g, x);
                  marcsPause(200) ;
                  }

                  showStatus("Ta-Da!");

                  //draw red 6
                  for(int x = 240; x < 270; x = x + 30)
                  {
                  y = 210;
                  drawMyOvalred( g, x);
                  drawMyLeftExe( g, x);
                  drawMyRightExe( g, x);
                  drawMySmile( g, x);
                  marcsPause(200) ;
                  }



                  }

                  public void drawMyOvalred (Graphics g, int x)
                  {
                  //yellow circle
                  g.setColor(Colo r.YELLOW);
                  g.fillOval(x,y, 30,30);
                  g.drawOval(x,y, 30,30);

                  }
                  public void drawMyOvalgreen (Graphics g, int x)
                  {
                  //black circle
                  g.setColor(Colo r.BLACK);
                  g.fillOval(x,y, 30,30);
                  g.drawOval(x,y, 30,30);

                  }
                  public void drawMyOvalblue (Graphics g, int x)
                  {
                  //green circle
                  g.setColor(Colo r.GREEN);
                  g.fillOval(x,y, 30,30);
                  g.drawOval(x,y, 30,30);

                  }

                  public void drawMyLeftExe (Graphics g, int x)
                  {
                  //draw exe, blue circle
                  g.setColor(Colo r.BLUE);
                  g.fillOval(x + 10,y + 10,1,1);
                  g.drawOval(x + 10,y + 10,1,1);

                  }
                  public void drawMyRightExe (Graphics g, int x)
                  {
                  //draw exe, blue circle
                  g.setColor(Colo r.BLUE);
                  g.fillOval(x + 20,y + 10,1,1);
                  g.drawOval(x + 20,y + 10,1,1);

                  }
                  public void drawMySmile (Graphics g, int x)
                  {
                  //draw smile, red line
                  g.setColor(Colo r.RED);
                  g.drawLine(250, 230,260,230);

                  }

                  public void eraseMyOval (Graphics g, int x)
                  {
                  //white circle
                  g.setColor(Colo r.WHITE);
                  g.fillOval(x,y, 30,30);
                  g.drawOval(x,y, 30,30);

                  }


                  void marcsPause(long ms)
                  {

                  try
                  {
                  Thread.sleep(ms );
                  }
                  catch(Interrupt edException e) {}

                  }

                  }

                  Comment

                  • r035198x
                    MVP
                    • Sep 2006
                    • 13225

                    #10
                    Originally posted by NewJack
                    here's the orignal in its entirety...

                    import java.awt.Color;
                    import java.awt.Graphi cs;
                    import javax.swing.JAp plet;

                    public class Worms extends JApplet
                    {
                    int y = 0;

                    public void paint(Graphics g)

                    { //stairs
                    g.setColor(Colo r.BLUE);
                    g.drawLine(0,90 ,90,90);
                    g.drawLine(90,9 0,90,180);
                    g.drawLine(90,1 80,180,180);
                    g.drawLine(180, 180,180,270);
                    g.drawLine(180, 270,270,270);

                    //drawred1
                    for(int x = 0; x < 30; x = x + 30)
                    {
                    y = 60;
                    drawMyOvalred( g, x);
                    marcsPause(200) ;
                    }

                    showStatus("Com e on little guy, you can do it!");

                    //drawgreen1
                    for(int x = 30; x < 60; x = x + 30)
                    {
                    y = 60;
                    drawMyOvalgreen ( g, x);
                    marcsPause(200) ;
                    }

                    //drawblue1
                    for(int x = 60; x < 90; x = x + 30)
                    {
                    y = 60;
                    drawMyOvalblue( g, x);
                    marcsPause(200) ;
                    }

                    //erase red 2
                    for(int x = 90; x < 120; x = x + 30)
                    {
                    y = 60;
                    drawMyOvalred( g, x);
                    marcsPause(200) ;
                    }

                    //draw red1
                    for(int x = 0; x < 30; x = x + 30)
                    {
                    y = 60;
                    eraseMyOval( g, x);
                    marcsPause(200) ;
                    }
                    //erase green 2
                    for(int x = 90; x < 120; x = x + 30)
                    {
                    y = 90;
                    drawMyOvalgreen ( g, x);
                    marcsPause(200) ;

                    }

                    //draw green 1
                    for(int x = 30; x < 60; x = x + 30)
                    {
                    y = 60;
                    eraseMyOval( g, x);
                    marcsPause(200) ;
                    }
                    //erase blue 2
                    for(int x = 90; x < 120; x = x + 30)
                    {
                    y = 120;
                    drawMyOvalblue( g, x);
                    marcsPause(200) ;
                    }
                    //draw blue 1
                    for(int x = 60; x < 90; x = x + 30)
                    {
                    y = 60;
                    eraseMyOval( g, x);
                    marcsPause(200) ;
                    }
                    //erase red 3
                    for(int x = 90; x < 120; x = x + 30)
                    {
                    y = 150;
                    drawMyOvalred( g, x);
                    marcsPause(200) ;
                    }
                    //draw red 2
                    for(int x = 90; x < 120; x = x + 30)
                    {
                    y = 60;
                    eraseMyOval( g, x);
                    marcsPause(200) ;
                    }
                    //erase green 3
                    for(int x = 120; x < 150; x = x + 30)
                    {
                    y = 150;
                    drawMyOvalgreen ( g, x);
                    marcsPause(200) ;
                    }
                    //draw green 2
                    for(int x = 90; x < 120; x = x + 30)
                    {
                    y = 90;
                    eraseMyOval( g, x);
                    marcsPause(200) ;
                    }
                    //erase blue 3
                    for(int x = 150; x < 180; x = x + 30)
                    {
                    y = 150;
                    drawMyOvalblue( g, x);
                    marcsPause(200) ;
                    }
                    //draw blue 2
                    for(int x = 90; x < 120; x = x + 30)
                    {
                    y = 120;
                    eraseMyOval( g, x);
                    marcsPause(200) ;
                    }
                    //erase red 4
                    for(int x = 180; x < 210; x = x + 30)
                    {
                    y = 150;
                    drawMyOvalred( g, x);
                    marcsPause(200) ;
                    }
                    //draw red 3
                    for(int x = 90; x < 120; x = x + 30)
                    {
                    y = 150;
                    eraseMyOval( g, x);
                    marcsPause(200) ;
                    }
                    //erase green 4
                    for(int x = 180; x < 210; x = x + 30)
                    {
                    y = 180;
                    drawMyOvalgreen ( g, x);
                    marcsPause(200) ;
                    }
                    //draw green 3
                    for(int x = 120; x < 150; x = x + 30)
                    {
                    y = 150;
                    eraseMyOval( g, x);
                    marcsPause(200) ;
                    }
                    //erase blue 4
                    for(int x = 180; x < 210; x = x + 30)
                    {
                    y = 210;
                    drawMyOvalblue( g, x);
                    marcsPause(200) ;
                    }
                    //draw blue 3
                    for(int x = 150; x < 180; x = x + 30)
                    {
                    y = 150;
                    eraseMyOval( g, x);
                    marcsPause(200) ;
                    }
                    //erase red 5
                    for(int x = 180; x < 210; x = x + 30)
                    {
                    y = 240;
                    drawMyOvalred( g, x);
                    marcsPause(200) ;
                    }
                    //draw red 4
                    for(int x = 180; x < 210; x = x + 30)
                    {
                    y = 150;
                    eraseMyOval( g, x);
                    marcsPause(200) ;
                    }
                    //erase green 5
                    for(int x = 210; x < 240; x = x + 30)
                    {
                    y = 240;
                    drawMyOvalgreen ( g, x);
                    marcsPause(200) ;
                    }
                    //draw green 4
                    for(int x = 180; x < 210; x = x + 30)
                    {
                    y = 180;
                    eraseMyOval( g, x);
                    marcsPause(200) ;
                    }
                    //erase blue 5
                    for(int x = 240; x < 270; x = x + 30)
                    {
                    y = 240;
                    drawMyOvalblue( g, x);
                    marcsPause(200) ;
                    }
                    //draw blue 4
                    for(int x = 180; x < 210; x = x + 30)
                    {
                    y = 210;
                    eraseMyOval( g, x);
                    marcsPause(200) ;
                    }

                    showStatus("Ta-Da!");

                    //draw red 6
                    for(int x = 240; x < 270; x = x + 30)
                    {
                    y = 210;
                    drawMyOvalred( g, x);
                    drawMyLeftExe( g, x);
                    drawMyRightExe( g, x);
                    drawMySmile( g, x);
                    marcsPause(200) ;
                    }



                    }

                    public void drawMyOvalred (Graphics g, int x)
                    {
                    //yellow circle
                    g.setColor(Colo r.YELLOW);
                    g.fillOval(x,y, 30,30);
                    g.drawOval(x,y, 30,30);

                    }
                    public void drawMyOvalgreen (Graphics g, int x)
                    {
                    //black circle
                    g.setColor(Colo r.BLACK);
                    g.fillOval(x,y, 30,30);
                    g.drawOval(x,y, 30,30);

                    }
                    public void drawMyOvalblue (Graphics g, int x)
                    {
                    //green circle
                    g.setColor(Colo r.GREEN);
                    g.fillOval(x,y, 30,30);
                    g.drawOval(x,y, 30,30);

                    }

                    public void drawMyLeftExe (Graphics g, int x)
                    {
                    //draw exe, blue circle
                    g.setColor(Colo r.BLUE);
                    g.fillOval(x + 10,y + 10,1,1);
                    g.drawOval(x + 10,y + 10,1,1);

                    }
                    public void drawMyRightExe (Graphics g, int x)
                    {
                    //draw exe, blue circle
                    g.setColor(Colo r.BLUE);
                    g.fillOval(x + 20,y + 10,1,1);
                    g.drawOval(x + 20,y + 10,1,1);

                    }
                    public void drawMySmile (Graphics g, int x)
                    {
                    //draw smile, red line
                    g.setColor(Colo r.RED);
                    g.drawLine(250, 230,260,230);

                    }

                    public void eraseMyOval (Graphics g, int x)
                    {
                    //white circle
                    g.setColor(Colo r.WHITE);
                    g.fillOval(x,y, 30,30);
                    g.drawOval(x,y, 30,30);

                    }


                    void marcsPause(long ms)
                    {

                    try
                    {
                    Thread.sleep(ms );
                    }
                    catch(Interrupt edException e) {}

                    }

                    }
                    Just looking at your code now. Shouldn't take long...

                    Comment

                    • r035198x
                      MVP
                      • Sep 2006
                      • 13225

                      #11
                      Code:
                       
                      import java.awt.Color;
                      import java.awt.Graphics;
                      import javax.swing.JApplet;
                      public class Worms1 extends JApplet
                      {
                      int y = 0;
                      public void paint(Graphics g) 
                      { //stairs
                      g.setColor(Color.BLUE);
                      g.drawLine(0,90,90,90);
                      g.drawLine(90,90,90,180);
                      g.drawLine(90,180,180,180);
                      g.drawLine(180,180,180,270);
                      g.drawLine(180,270,270,270);
                      //drawred1
                      showStatus("Come on little guy, you can do it!");
                      for(int x = 240; x < 270; x = x + 30)
                      {
                      y = 240;
                      drawMyOvalred( g, x);
                      marcsPause(200);
                      }
                       
                      //drawgreen1
                      for(int x = 210; x < 240; x = x + 30)
                      {
                      y = 240;
                      drawMyOvalgreen( g, x);
                      marcsPause(200);
                      }
                      //drawblue1
                      for(int x = 180; x < 210; x = x + 30)
                      {
                      y = 240;
                      drawMyOvalblue( g, x);
                      marcsPause(200);
                      }
                      //erase red 2
                      for(int x = 180; x < 210; x = x + 30)
                      {
                      y = 210;
                      drawMyOvalred( g, x);
                      marcsPause(200);
                      }
                      //draw red1
                      for(int x = 240; x < 270; x = x + 30)
                      {
                      y = 240;
                      eraseMyOval( g, x);
                      marcsPause(200);
                      }
                      
                      //erase green 2
                      for(int x = 180; x < 210; x = x + 30)
                      {
                      y = 180;
                      drawMyOvalgreen( g, x);
                      marcsPause(200);
                      }
                      
                      for(int x = 210; x < 240; x = x + 30)
                      {
                      y = 240;
                      eraseMyOval( g, x);
                      marcsPause(200);
                      }
                      for(int x = 180; x < 210; x = x + 30)
                      {
                      y = 150;
                      drawMyOvalblue( g, x);
                      marcsPause(200);
                      }
                       
                      for(int x = 180; x < 210; x = x + 30)
                      {
                      y = 240;
                      eraseMyOval( g, x);
                      marcsPause(200);
                      }
                      
                      for(int x = 150; x < 180; x = x + 30)
                      {
                      y = 150;
                      drawMyOvalred( g, x);
                      marcsPause(200);
                      }
                      
                      for(int x = 180; x < 210; x = x + 30)
                      {
                      y = 210;
                      eraseMyOval( g, x);
                      marcsPause(200);
                      }
                       
                      for(int x = 120; x < 150; x = x + 30)
                      {
                      y = 150;
                      drawMyOvalgreen( g, x);
                      marcsPause(200);
                      }
                       
                      for(int x = 180; x < 210; x = x + 30)
                      {
                      y = 180;
                      eraseMyOval( g, x);
                      marcsPause(200);
                      }
                      for(int x = 180; x < 210; x = x + 30)
                      {
                      y = 150;
                      drawMyOvalblue( g, x);
                      marcsPause(200);
                      }
                      
                      showStatus("Ta-Da!");
                      //draw red 6
                      //for(int x = 240; x < 270; x = x + 30)
                      //{
                      //y = 210;
                      //drawMyOvalred( g, x);
                      //drawMyLeftExe( g, x);
                      //drawMyRightExe( g, x);
                      //drawMySmile( g, x);
                      //marcsPause(200);
                      //}
                       
                      }
                      public void drawMyOvalred (Graphics g, int x)
                      {
                      g.setColor(Color.RED);
                      g.fillOval(x,y,30,30);
                      g.drawOval(x,y,30,30);
                      }
                      public void drawMyOvalgreen (Graphics g, int x)
                      {
                      g.setColor(Color.GREEN);
                      g.fillOval(x,y,30,30);
                      g.drawOval(x,y,30,30);
                      }
                      public void drawMyOvalblue (Graphics g, int x)
                      {
                      g.setColor(Color.BLUE);
                      g.fillOval(x,y,30,30);
                      g.drawOval(x,y,30,30);
                      }
                      public void drawMyLeftExe (Graphics g, int x)
                      {
                      //draw exe, blue circle
                      g.setColor(Color.BLUE);
                      g.fillOval(x + 10,y + 10,1,1);
                      g.drawOval(x + 10,y + 10,1,1);
                      }
                      public void drawMyRightExe (Graphics g, int x)
                      {
                      //draw exe, blue circle
                      g.setColor(Color.BLUE);
                      g.fillOval(x + 20,y + 10,1,1);
                      g.drawOval(x + 20,y + 10,1,1);
                      }
                      public void drawMySmile (Graphics g, int x)
                      {
                      //draw smile, red line
                      g.setColor(Color.RED);
                      g.drawLine(250,230,260,230);
                      }
                      public void eraseMyOval (Graphics g, int x)
                      {
                      //white circle
                      g.setColor(Color.WHITE);
                      g.fillOval(x,y,30,30);
                      g.drawOval(x,y,30,30);
                      }
                      
                      void marcsPause(long ms)
                      {
                      try
                      {
                      Thread.sleep(ms);
                      }
                      catch(InterruptedException e) {}
                      }
                      }
                      I suppose you can finish it off from here. May I point out the approach may have been improved by using arrays somewhere in there to reduce the for loops. If you need to have the bugs on the same applet, you would need to add a boolean flag, say goingUp, and put an if else on the fors to execute.

                      Comment

                      Working...