Home E-Books Special Effects and Game Development in Java The Applet SlideShow II

Special Effects and Game Development in Java(TM) - The Applet SlideShow II

 

import java.applet.*;

import java.awt.*;



public class slideshow2 extends Applet implements Runnable {



public Thread animationthread = null;

int maxitems=0;

int sleeptime=0;

Image images[];

int currentimage=0;

int nextimage = 0;

int nextimagexposition = 0;

int nextimageyposition = 0;



//"bufferimage" is used as the buffer and

//"bufferg" is used to draw on this buffer.

Image bufferimage;

Graphics bufferg;



//"delay" will be used instead of "sleeptime" to

//delay the each animation. "sleeptime" will be used to

//add a configurable delay between each image instead.

int delay=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...");
}
//Fetch the size for the applet
Dimension d=size(); //createImage() creates an empty image //that has the same dimensions as the //applet screen bufferimage=createImage(d.width,d.height); //"bufferg" is now associated with the //buffer image. bufferg=bufferimage.getGraphics(); delay=getIntegerParameter("delay",10); } 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) { //First we should check that "bufferg" //is initialized, it is not always sure that it //is. if (bufferg!=null) { //We draw the two images into the buffer first. bufferg.drawImage(images[currentimage],0,0,this); bufferg.drawImage(images[nextimage] ,nextimagexposition,nextimageyposition,this); //Now we draw the buffer image into the applet //screen. g.drawImage(bufferimage,0,0,this); } } public void update(Graphics g) { paint(g); } public void run()
{
Dimension d=size();
int displacementx=0;
int displacementy=0;
int pathsize=0;
currentimage=0;
nextimage=1;
while (true)
{
int directionx=(int) (Math.random()*3.0)-1;
int directiony=(int) (Math.random()*3.0)-1;
if (directionx==0 && directiony==0) directiony=1;
if (d.width<d.height) pathsize=d.width;
else pathsize=d.height;
displacementx=directionx*d.width;
displacementy=directiony*d.height;
if (nextimage>maxitems-1) nextimage=0;
for (int i=0; i<=pathsize; i++)
{
nextimagexposition=displacementx
-directionx*(i*d.width)/pathsize;
nextimageyposition=displacementy
-directiony*(i*d.height)/pathsize;
update(getGraphics()); //Now "delay" is the delay in the loop. try {Thread.sleep(delay);} catch(InterruptedException e) {} } //"sleeptime" is used outside the loop. try {Thread.sleep(sleeptime);} catch(InterruptedException e) {} currentimage=nextimage; nextimage++; if (nextimage>maxitems-1) nextimage=0; } } }