Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

animated stickfigure walk across screen

843806Mar 28 2009 — edited Mar 29 2009
I am working on an animated stick figure that walks across the applet screen. I was advised by someone in the New to Java Forum to post in the Swing forum for better direction with my problem. I am fairly new to Java and Swing and can use some help. I can get the stick figure to walk across the screen, but I can't seem to get the leg to go in the same direction as the body. The walking leg doesn't keep moving forward with the body. Instead, it just seems to keep stretching like the end of the leg is stuck in one spot. I know it has to do with that particular point, but I can't seem to get it to increment with the rest of the body. Can anyone give any advice on what I actually need to do to get the leg walking in the same increment as the body? Thank you in advance for any help that can be provided. Code is below...
import java.applet.*;*
*import java.awt.*;
import javax.swing.*;
public class Walkers extends Applet //implements Runnable
{
private static final long serialVersionUID = 1L;
private int body = 85; //set horizontal body location
private int leg1 = 125;
private int leg2 = 155;
private int index = 0;
//private int[] vert = {310,310};
private int[] horiz = {leg1,leg2};
private int oval = 30;
private int rightarm = 40;//right arm position
private int leftarm = 140;//left arm position
private int step = 25;
private int incremoval = oval+step;//increment the head oval across the screen
private int LIMIT = 800;
private final int SLEEP_TIME = 400;

public void start() 
{ 
index = 0; 
} 

public void paint(Graphics gr)
{
gr.setColor(Color.WHITE);
gr.drawOval(oval,30,85,80);
gr.drawLine(body,110,body,310); 
gr.drawLine(body,140,rightarm,160);
gr.drawLine(body,140,leftarm,160);

oval = oval + step;
body = body + step;
rightarm = rightarm + step;
leftarm = leftarm + step;


gr.setColor(Color.BLACK);
gr.drawOval(oval,30,85,80);
gr.drawLine(body,110,body,310);
gr.drawLine(body,140,rightarm,160);
gr.drawLine(body,140,leftarm,160);
gr.drawLine(body,210,horiz[index],310); 
horiz = horiz+step; 

++index; 
if(index ==horiz.length) 
index = 0; 


try
{
Thread.sleep(SLEEP_TIME);
}

catch(InterruptedException e)
{
}

if (body < LIMIT)
repaint();
}
}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Apr 26 2009
Added on Mar 28 2009
8 comments
1,027 views