This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Applets and the fly likes Simple Thread with image.. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Applets
Bookmark "Simple Thread with image.." Watch "Simple Thread with image.." New topic

Simple Thread with image..

nima lama

Joined: Jun 18, 2001
Posts: 19
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.
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);
catch(InterruptedException e)
public void run()
//how should i include the image overhere and make it sleep for 500 millisec
public void paint(Graphics g)

Nathan Pruett

Joined: Oct 18, 2000
Posts: 4121

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...


Write once, run anywhere, because there's nowhere to hide! - /. A.C.
I agree. Here's the link:
subject: Simple Thread with image..
Similar Threads
Beginner game
Building a splash screen
Creating Java applet
about image. see this simple code please.
Applet(with images) running problem at Client