Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: How to animate two animations/classes in a Java Applet?

  1. #1
    Junior Member
    Join Date
    Mar 2009
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post How to animate two animations/classes in a Java Applet?

    Hi everyone,

    I need help ASAP.

    If you can help me on this, that would be appreciated. Currently, I have two animations (two classes) in a Java Applet. I would like to know how to animate both at the same time. Here they are.

     
    public class Testing extends Thread implements Runnable   
    {   
     
      public static void main(String[] args)   
       {   
           Thread runner = new Thread();   
           Thread bannerThread new Thread();   
     
           ///////// start the threads //////////////   
     
             two.setPriority(one.getPriority() + 4);   
     
                                    one.start();   
             two.start();   
        }   
     
    }   
     
    class bannerThread extends Thread   
    //  public class ScrollingBanner extends java.applet.Applet implements Runnable   
    {   
     //  Thread bannerThread;   
      String text;   
      Font font = new java.awt.Font("Times New Roman", Font.BOLD, 30);   
      int x, y;   
      int delay = 20;   
      int offset = 1;   
      Dimension d;   
     
      public void init() {   
        // get parameters "delay" and "text"   
        String att = getParameter("delay");   
        if (att != null) {   
          delay = Integer.parseInt(att);   
        }   
        att = getParameter("text");   
        if (att != null) {   
          text = att;   
        } else {   
          text = "Buy More at Nathan Chan Movie Buy";   
        }   
     
        // set initial position of the text   
        d =  getSize();   
        x = d.width * 3;   
        y = 100;   
      }   
     
     
      public void paint(Graphics g) {   
        // get the font metrics to determine the length of the text   
        g.setFont(font);   
        FontMetrics fm = g.getFontMetrics();   
        int length = fm.stringWidth(text);   
     
        // adjust the position of text from the previous frame   
        x -= offset;   
     
        // if the text is completely off to the left end   
        // move the position back to the right end   
     
        if (x < -length)   
          x = d.width;   
     
        // set the pen color and draw the background   
        g.setColor(Color.white);   
        g.fillRect(0,0,d.width,d.height);   
     
        // set the pen color, then draw the text   
        g.setColor(Color.blue);   
        g.drawString(text, x, y);   
      }   
     
      public void start() {   
        bannerThread = new Thread(this);   
        bannerThread.start();   
      }   
     
      public void stop() {   
        bannerThread = null;   
      }   
     
      public void run() {   
        while (Thread.currentThread() == bannerThread) {   
          try {   
            Thread.currentThread().sleep(delay);   
          }   
          catch (InterruptedException e) {}   
          repaint();   
        }   
      }   
    }   
     
    class runner extends Thread   
    // public class MovieBuyScroll extends JApplet implements Runnable   
    {   
      Image austinpics[] = new Image[8];   
      Image currentimg;   
      Thread runner;   
      int xpos;   
      int ypos = 50;   
      Thread bannerThread;   
      String text;   
      Font font = new Font("Times New Roman", Font.BOLD, 30);   
      int xpos2, ypos2;   
      int delay = 200;   
      int offset = 1;   
      Dimension d;   
     
      public void init()   
      {   
         // put the gif names into array   
         String apowerssrc[] = {"apowers30.jpg","apowers31.jpg","apowers32.jpg","apowers33.jpg",   
                              "apowers34.jpg","apowers35.jpg","apowers36.jpg",   
                              "apowers37.jpg"};   
     
         // use the jpeg names in the above array to build up   
         // an array of jpeg images   
         for(int i =0; i < apowerssrc.length; i++)   
          {   
             austinpics[i] = getImage(getCodeBase(), apowerssrc[i]);   
          }    
      }      
     
      public void start()   
      {   
         if (runner == null)   
         {   
             runner = new Thread(this);   
             runner.start();   
         }   
       }   
     
       public void stop()   
       {   
         if (runner != null)   
         {   
             runner.stop();     //stop thread every time stop is called   
             runner = null;     //as the user leaves for another web page   
         }   
       }   
     
       public void run() // run the animation   
       {   
          setBackground(Color.white);     
           while(true)   
            {   
             austinpowers1(0,getWidth());   
             austinpowers2(getWidth(),getWidth());   
     
             austinpowers1(xpos, getWidth());   
             austinpowers2(0, getWidth());   
     
            }    
     
        }   
     
       void austinpowers1(int start, int end)   
       {   
           for(int i = start; i < end; i+=30)   
            {   
     
             xpos = i;  // move the image to xpos = i   
     
             if(currentimg == austinpics[0])   
               {currentimg = austinpics[1];   
                 repaint();   
                 pause(delay);             
                currentimg = austinpics[2];   
                 repaint();   
                 pause(delay);              
                currentimg = austinpics[3];   
                 repaint();   
                 pause(delay);              
                currentimg = austinpics[4];   
                 repaint();   
                 pause(delay);              
                currentimg = austinpics[5];   
                 repaint();   
                 pause(delay);              
                currentimg = austinpics[6];   
                 repaint();   
                 pause(delay);              
                currentimg = austinpics[7];   
                 repaint();   
                 pause(delay);   
                currentimg = austinpics[0];   
                 repaint();   
                 pause(delay);   
             }   
            else  
               {currentimg = austinpics[1];   
                 repaint();   
                 pause(delay);   
                currentimg = austinpics[2];   
                 repaint();   
                 pause(delay);                                     
                currentimg = austinpics[3];   
                 repaint();   
                 pause(delay);              
                currentimg = austinpics[4];   
                 repaint();   
                 pause(delay);              
                currentimg = austinpics[5];   
                 repaint();   
                 pause(delay);              
                currentimg = austinpics[6];   
                 repaint();   
                 pause(delay);              
                currentimg = austinpics[7];   
                 repaint();   
                 pause(delay);              
                 currentimg = austinpics[0];   
                 repaint();   
                 pause(delay);              
                }       //   close else   
            }        //  close for loop   
       }           //  close austinspowers1   
     
        void austinpowers2(int start, int end)   
       {   
           for(int i = start; i > end; i-=30)   
            {   
     
             xpos = i;   
     
             if(currentimg == austinpics[0])   
               {currentimg = austinpics[1];   
                 repaint();   
                 pause(delay);             
                currentimg = austinpics[2];   
                 repaint();   
                 pause(delay);              
                currentimg = austinpics[3];   
                 repaint();   
                 pause(delay);              
                currentimg = austinpics[4];   
                 repaint();   
                 pause(delay);              
                currentimg = austinpics[5];   
                 repaint();   
                 pause(delay);              
                currentimg = austinpics[6];   
                 repaint();   
                 pause(delay);              
                currentimg = austinpics[7];   
                 repaint();   
                 pause(delay);   
                currentimg = austinpics[0];   
                 repaint();   
                 pause(delay);   
             }   
            else  
               {currentimg = austinpics[1];   
                 repaint();   
                 pause(delay);   
                currentimg = austinpics[2];   
                 repaint();   
                 pause(delay);                                     
                currentimg = austinpics[3];   
                 repaint();   
                 pause(delay);              
                currentimg = austinpics[4];   
                 repaint();   
                 pause(delay);              
                currentimg = austinpics[5];   
                 repaint();   
                 pause(delay);              
                currentimg = austinpics[6];   
                 repaint();   
                 pause(delay);              
                currentimg = austinpics[7];   
                 repaint();   
                 pause(delay);              
                 currentimg = austinpics[0];   
                 repaint();   
                 pause(delay);              
                }       //   close else   
            }        //  close for loop   
       }           //  close austinspowers1   
     
         void pause(int time)   
         {   
            try { Thread.sleep(time); }   
            catch (InterruptedException e) {}   
         }   
     
         public void paint(Graphics g)   
         {   
             g.clearRect(0,0,getWidth(),getHeight());      
                g.drawImage(currentimg,xpos,ypos,this);   
         }   
    }

    If you cannot understand the code, here is a brief explaination of both of it's functionality.

    The 'Buy More at Nathan Chan Movie Buy' text is supposed to move from the right of the screen to the left of the screen.
    Moreover, the Austin Powers picture array is supposed to animate from the left of the screen to the right of the screen.

    My objective is to animate two of these animations both at the same time.

    What code is needed in order for both to "multithread"?


  2. #2
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default Re: Java Animation ASAP. Having Problems with Multithreading Two Animations

    You should be multithreading animation. When dealing with animation, you make all changes to the positions of the components on the applet, then redraw everything.

    Or at least thats the approach I have always taken when dealing animation within Java2D and Applications.

    Chris

Similar Threads

  1. How to make java based animation?
    By JavaPF in forum The Cafe
    Replies: 1
    Last Post: June 7th, 2008, 06:55 AM