Go Back   Java Programming Forums > Java Standard Edition Programming Help > Threads


Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 01-04-2009, 07:42 PM
Junior Member
 

Join Date: Mar 2009
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
antitru5t is on a distinguished road
Post Java Animation ASAP. Having Problems with Multithreading Two Animations

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.

Java Code
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"?



Reply With Quote Share this thread on Facebook
Sponsored Links
Java Training from DevelopIntelligence
  #2 (permalink)  
Old 01-04-2009, 07:47 PM
Freaky Chris's Avatar
Senile Half-Wit
 

Join Date: Mar 2009
Location: UK
Posts: 621
Thanks: 2
Thanked 64 Times in 60 Posts
Freaky Chris will become famous soon enoughFreaky Chris will become famous soon enoughFreaky Chris will become famous soon enough

I'm feeling Cheerful
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
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



Similar Threads
Thread Thread Starter Forum Replies Last Post
Funny Java based animation JavaPF The Cafe 1 07-06-2008 11:55 AM


100 most searched terms
Search Cloud
2 dimensional arraylist java 2d arraylist java actionlistener actionlistener in java addactionlistener addactionlistener java convert double to integer java double format java double to integer in java double to integer java drag en drop programmeren java eclipse shortcut keys exception in thread "awt-eventqueue-0" java.lang.outofmemoryerror: java heap space exception in thread "main" java.lang.nullpointerexception exception in thread "main" java.lang.outofmemoryerror: java heap space format double in java format double java get mouse position java java 2d arraylist java actionlistener java double format java double formatting java double to int java double to integer java format double java forum java forums java get mouse position java list to map java mouse position java programming forum java programming forums java send keystrokes to another application java two dimensional arraylist java.io.ioexception: premature eof java.lang.classformaterror: truncated class file java.lang.outofmemoryerror: java heap space java.util.arraylist jbutton action jbutton actionlistener jbutton java jtextarea font jtextfield font size jxl.read.biff.biffexception: unable to recognize ole stream programming mutators and generics smack api two dimensional arraylist two dimensional arraylist java unable to sendviapost to url what is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?

All times are GMT. The time now is 01:51 AM.
Powered by vBulletin® Copyright ©2000-2009, Jelsoft Enterprises Ltd.