I need to write a applet that displays between one and three figures that walk across the screen at different speeds form left to right.
I have one figure walking across the screen but can't seem to get figure two or three to work correctly. This is what I have for my code so far:
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
public class Walking extends JApplet implements ActionListener
{
private int count = 0;
private int sleep = 100;
private int startx = 45,oldx = 42;
public void start()
{
count = 0;
startx = 45;
}
public void update(Graphics gr)
{
paint(gr);
}
public void paint(Graphics gr)
{
if((count%2)==0)
{
gr.setColor(getBackground());
gr.drawOval(oldx - 15,30,45,45);
gr.drawLine(oldx,75,oldx,160);
gr.drawLine(oldx,160,oldx+5,160);
gr.drawLine(oldx+5,110,oldx+10, 155);
gr.drawLine(oldx+10,155,oldx+20, 155);
gr.drawLine(oldx,110,oldx+20,140);
gr.drawLine(oldx+20,140,oldx+30,140);
gr.setColor(getForeground());
gr.drawOval(startx - 15,30,45,45);
gr.drawLine(startx,75,startx,110);
gr.drawLine(startx,110,startx,160);
gr.drawLine(startx,160,startx+5,160);
gr.drawLine(startx+5,110,startx+10,155);
gr.drawLine(startx+ 10,155,startx + 20,155);
gr.drawLine(0, 165,300,165);
}
else
{
gr.setColor(getForeground());
gr.drawLine(startx,110,startx+20,140);
gr.drawLine(startx+20,140,startx+30,140);
gr.setColor(getBackground());
gr.drawLine(startx+5,110,startx+10, 155);
gr.drawLine(startx+10,155,startx+20,155);
oldx = startx;
startx +=3;
}
++count;
if(startx>180 )
startx = 45;
try
{
Thread.sleep(sleep);
}
catch(InterruptedException e)
{
showStatus(e.toString());
}
repaint();
}
Button oneWalkerButton;
Button twoWalkersButton;
Button threeWalkersButton;
public void init()
{
setLayout(null);
oneWalkerButton = new Button("One walker");
oneWalkerButton.setBounds(0,180,75,20);
add(oneWalkerButton);
oneWalkerButton.addActionListener(this);
twoWalkersButton = new Button ("Two walkers");
twoWalkersButton.setBounds(0,210,75,20);
add(twoWalkersButton);
twoWalkersButton.addActionListener(this);
threeWalkersButton = new Button("Three walkers");
threeWalkersButton.setBounds(0,240,85,20);
add(threeWalkersButton);
threeWalkersButton.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
}
}