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)
      {
      }
   }

Create three threads. Each thread represent a character you want to show.

You should create a Walker class, and outside of that class have an array of Walker, a Collection of Walker, or simply three separate Walker objects. The Walker class shouldn't extend JApplet. Have Walking extend JApplet. So have at least two classes. The below can be in the Walking class, which again extends JApplet.

Walker walker1;
Walker walker2;
Walker walker3;

Your ActionListener should remain in Walking. I would add a booleanvariable to the Walker class called isWalking . Have your ActionListener in Walking adjust the true/false values of this variable. If isWalking is true, you can execute line 102 to make the Walker move. Otherwise, don't. Or keep the paint function the way it is and simply don't call repaint () if isWalking isn't true. There are several ways to do this, but they pretty much all involve setting up a boolean variable in a Walker class and checking it, as well as toggling that boolean variable in ActionPerformed.

So I think the program needs a redesign with a Walker class. You should have multiple Walker objects and one Walking object (which is the applet).

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.