Win a copy of The Journey To Enterprise Agility this week in the Agile and Other Processes forum! And see the welcome thread for 20% off.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

Simple Thread with image..  RSS feed

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello,
please complete this code.
objective: there are two image, i like to make each image sleep for 1 second so that it would change after 1 second. i used thread over here, but i am confused as how i should write the code in [ public void run()] so please kindly fill the codes.
thanks very much.
code:
******
import java.applet.*;
import java.awt.*;
//<applet code=pip.class height=300 width=400></applet>
public class pip extends Applet implements Runnable
{
Image img1,img2;
MediaTracker mt=new MediaTracker();
Thread t;
public void init()
{
t=new Thread(this);
img1=getImage(getDocumentBase(),"a.gif");
img2=getImage(getDocumentBase(),"b.gif");
mt.addImage(img1,0);
mt.addImage(img2,1);
try
{
mt.waitForAll();
}
catch(InterruptedException e)
{}
t.start();
}
public void run()
{
//how should i include the image overhere and make it sleep for 500 millisec
img1.
}
public void paint(Graphics g)
{
g.drawImage(img1,0,0,this);
g.drawImage(img2,0,0,this);
}
}

 
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is what you would do... you need to have another image that is defined as the current Image that is being displayed. Then you need to have the thread switch the image being displayed, repaint the applet, and sleep for 500 milliseconds.

Also, it is a good idea to override the start() and stop() methods of the applet to start and stop the thread... if you don't do this the thread continues to run, wasting resources even when the applet is not displaying.

Here is your updated code...


-Nate
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!