title: How to make animation move its legs while moving?
body: <p>So right now when you press go it just moves across the screen, but I also want it to move its legs so its doing something and not just standing still. Any tips on how to do that? I know it would be moving the 2nd leg pointers back and forth but I'm not sure how to do that</p>
  
  <p>Class Stick2 :</p>
  
  <pre><code>import javax.swing.*;
  import java.awt.*;
  import java.awt.event.*;
  
  
  
  public class Stick2 extends JFrame implements ActionListener {
  
      // Declares constants 
      public static final int FRAME_WIDTH = 500;
      private static final int FRAME_HEIGHT = 500;
      private static final int FRAME_X_ORIGIN = 150;
      private static final int FRAME_Y_ORIGIN  = 200;
      private static final int BUTTON_WIDTH =80;
      private static final int BUTTON_HEIGHT = 30;
  
      JPanel buttonPanel, panel;
  
      MovingBanner2 myBanner;
      JButton startButton, stopButton;
  
      Thread thrd;
  
  
      public static void main(String[] args) 
      {
          Stick2 frame = new Stick2();
          frame.setVisible(true);
      }
  
      public Stick2(){
  
  
           Container contentPane= getContentPane();
  
          // Sets Frame
          setSize(FRAME_WIDTH,FRAME_HEIGHT);
          setResizable(false);
          setTitle("Animation");
          setLocation(FRAME_X_ORIGIN, FRAME_Y_ORIGIN);
  
          // Sets layout manager
          contentPane.setLayout(new BorderLayout(10,0));
  
  
          buttonPanel = new JPanel();
  
          JButton startButton = new JButton("Start");
          startButton.setSize(BUTTON_WIDTH, BUTTON_HEIGHT);
          buttonPanel.add(startButton);
          startButton.addActionListener(this);
  
          JButton stopButton = new JButton("Stop");
          stopButton.setSize(BUTTON_WIDTH,BUTTON_HEIGHT);
          buttonPanel.add(stopButton);
          stopButton.addActionListener(this);
  
          contentPane.add (buttonPanel, BorderLayout.SOUTH);
  
          // Creates a balloon
          myBanner = new MovingBanner2();
          panel = myBanner;
          panel.setBorder(BorderFactory.createLineBorder(Color.BLUE));
          contentPane.add(panel, BorderLayout.CENTER);
  
  
  
      }
  
      public void actionPerformed (ActionEvent event){
          JButton clickedButton = (JButton) event.getSource();
  
          String buttonText = clickedButton.getText();
  
          if (buttonText.equals("Stop")) {
  
              myBanner.stopAnimation();
              thrd = null;
          }
          else {
  
              myBanner.startAnimation();
              thrd = new Thread (myBanner);
              thrd.start();
  
  
         }
      }            
  }
  </code></pre>
  
  <p>Class  MovingBanner2:   </p>
  
  <pre><code>class MovingBanner2 extends JPanel implements Runnable {
  
          private int x;
  
          private Boolean animate;
  
          int bodyX = 250;
          int bodyY1 = 160;
          int bodyY2 = 210;
          int armHeight = 190;
          int armLength = bodyX + 30;
          int armLength1 = bodyX - 30;
          int legY = 340;
  
          public MovingBanner2() {
              x=10;
              animate = true;
          }
  
          // Draws the String
          public void paintComponent (Graphics g) {
              super.paintComponent(g);
  
              g.setColor(Color.RED);
              //g.drawString("I love Java", x,50);
              g.drawLine(bodyX + x, bodyY1, bodyX + x, bodyY2); //body
              g.drawOval(bodyX + x - 15, bodyY1 - 40, 40, 40); //head
              g.drawLine(armLength + x,armHeight,  armLength1 + x, armHeight); //arms
              g.drawLine(bodyX + x, bodyY2, bodyX + 20 + x,legY); //leg
              g.drawLine(bodyX + x, bodyY2, bodyX - 20 + x, legY);    //leg
          }
  
          public void run() {
              while (animate) { 
                  changeX();
                  repaint();     
                  try {Thread.sleep(100); } catch(Exception e){};
              }
          }
  
  
  
          public void changeX() {
  
              if (x &lt;= Stick2.FRAME_WIDTH - 240)
                   x++;
              else x = 10;
  
          }
  
          public void stopAnimation() {
              animate = false;
          }
  
          public void startAnimation() {
              animate = true;
          }
  }
  </code></pre>