Special Effects and Game Development
in Java(TM) - The Applet SlideShow
import java.applet.*;
import java.awt.*;
public class slideshow extends Applet implements Runnable {
public Thread animationthread = null;
int maxitems=0;
int sleeptime=0;
Image images[];
int currentimage=0;
//The "next"-variables specifies what image
//that will be displayed and it's position.
int nextimage = 0;
int nextimagexposition = 0;
int nextimageyposition = 0;
public void init()
{
maxitems=getIntegerParameter("maxitems",10);
sleeptime=getIntegerParameter("sleeptime",10);
images=new Image[maxitems];
MediaTracker tracker=new MediaTracker(this);
for (int i=0; i<maxitems; i++) { images[i]=getImage(getDocumentBase() ,getParameter("image"+i)); tracker.addImage(images[i],i); } try {tracker.waitForAll();} catch(InterruptedException e) { System.out.println("Something happened while reading images..."); } }
public int getIntegerParameter(String name, int base)
{
String value=getParameter(name);
int intvalue;
try {intvalue=Integer.parseInt(value,base);}
catch (NumberFormatException e) {return 0;}
return intvalue;
}
public void start()
{
if (animationthread == null)
{
animationthread = new Thread(this,"animationthread");
animationthread.start();
}
}
public void stop()
{
if ((animationthread != null) && animationthread.isAlive())
animationthread.stop();
animationthread = null;
}
public synchronized void paint(Graphics g)
{
g.drawImage(images[currentimage],0,0,this);
//The following image is drawn on top of the last
//image.
g.drawImage(images[nextimage]
,nextimagexposition,nextimageyposition,this);
}
public void update(Graphics g)
{
paint(g);
}
public void run()
{
//We must find out the applet's dimension and save it
//in the "d" variable. We can now get the applet
//width and height by writing d.width and d.height.
Dimension d=size();
//The "displacement" variables specify the
//starting coordinates for the image.
int displacementx=0;
int displacementy=0;
//"pathsize" will be used to know what distance
//that the new image will be moved.
int pathsize=0;
//"currentimage" is initialized to the first image,
//followed by the next image, who becomes image
//number 2, and so on.
currentimage=0;
nextimage=1;
while (true)
{
//We calculate from which direction that
//the new image should be moved from.
int directionx=(int) (Math.random()*3.0)-1;
int directiony=(int) (Math.random()*3.0)-1;
//"directionx" and "directiony" may have
//three values: -1, 0 and 1. It is not
//interesting if both variables get the value 0
//at the same time so therefore we change
//"directiony" so that the effect becomes
//more interesting.
if (directionx==0 && directiony==0) directiony=1;
//The variable "pathsize" is set to the applet //width or height depending on what side is the //shortest. if (d.width<d.height) pathsize=d.width; else pathsize=d.height; displacementx=directionx*d.width; displacementy=directiony*d.height;
//The following line is the protection to the //problem if the user should onlyu specify one //image. if (nextimage>maxitems-1) nextimage=0;
//The following loop handles the screen update //during the animation. It will move the image //the number of times specifies by "pathsize", //which is the short side of the applet screen. for (int i=0; i<=pathsize; i++) { //The image positions are moved //with the fraction (i/pathsize). nextimagexposition=displacementx -directionx*(i*d.width)/pathsize; nextimageyposition=displacementy -directiony*(i*d.height)/pathsize; update(getGraphics()); try {Thread.sleep(sleeptime);} catch(InterruptedException e) {} } //When the translation is finished, //then "currentimage" is set to the new //image. currentimage=nextimage;
//We increase "nextimage" so that it points to the
//next image. At the same time we make sure that
//"nextimage" extends above the array index but
//that it is forced to start over from 0 if it does.
nextimage++;
if (nextimage>maxitems-1) nextimage=0;
}
}
|
| |
|