switching images

ok guys, im newbie at this, but i need to make an applet capable of switching images, please, any idea on how to make it easy?, i've thought mediatracker would help...but idunno how.
[197 byte] By [astarothh] at [2007-9-26 4:48:44]
# 1
what is "switching images"?
jsalonen at 2007-6-29 18:39:09 > top of Java-index,Archived Forums,Java Programming...
# 2

I take it your asking for help on the entire applet not just the switching images part. If so, try going through the tutorials on this site so you understand the construction of an applet.

As for the actual image switching bit you need something like this in your applet...

import java.awt.*;

import java.applet.*;

public class MyApplet extends Applet {

Image img1;

Image img2;

Image shownImage;

public void init() {

// Load in the images

img1 = Toolkit.getDefaultToolkit().getImage(...URL of image...);

img2 = Toolkit.getDefaultToolkit().getImage(...URL of second image...);

try {

// Wait for the images to be loaded

MediaTracker imgTracker = new MediaTracker(this);

imgTracker.addImage(img1, 0);

imgTracker.addImage(img2, 0);

imgTracker.waitForID(0);

} catch (Exception e) {

}

shownImage = img1;

}

public void paint(Graphics g) {

// Draws the currentl selected image

g.drawImage(shownImage, 0, 0, this);

}

// Method which does the image switching

public void methodTriggeredBySomeEvent(Event e) {

if(shownImage==img1) {

shownImage = img2;

} else {

shownImage = img1;

}

}

}

I havn't tested this and its not complete. You will have to put in the code which triggers the image switching bit.

Rob.

ted_trippin at 2007-6-29 18:39:09 > top of Java-index,Archived Forums,Java Programming...
# 3

So *that's* what he meant...

Do note that it would be a lot better to load the images in a separate thread. The mediatracker will block init until all the images have been loaded, displaying a dead, grey rectangle to the user who is left wondering if the applet is still loading or does it work at all.

jsalonen at 2007-6-29 18:39:09 > top of Java-index,Archived Forums,Java Programming...
# 4
I'm only guessing thats what he wants.True, should load in a different thread but he says he's a newbie so learn to crawl before you can walk. Also depends on how many images you're loading - a couple of 2k GIF's or 300 250k GIF's!Rob.
ted_trippin at 2007-6-29 18:39:09 > top of Java-index,Archived Forums,Java Programming...