...

View Full Version : How would i get these images to change?



pineappelle
02-15-2012, 03:14 AM
The code below changes the image to the next one but wont continue changing the image for all for of them. What am i doing wrong?


/**
* @(#)chapter16random.java
*
* chapter16random Applet application
*
* @author
* @version 1.00 2012/2/8
*/

import java.awt.*;

public class chapter17a extends java.applet.Applet implements Runnable {

Graphics offscreen;
Thread runner;
int left, top, xmove;
Image workspace;
Image[] ball;
int currentpicture;
MediaTracker tracker;

public void init() {

workspace=createImage(400,400);
ball=new Image[4];
offscreen=workspace.getGraphics();
left=10;
top=10;
xmove=1;
loadimages();
currentpicture=0;

}

public void loadimages() {

tracker=new MediaTracker(this);
ball[0]=getImage(getCodeBase(), "ball1.gif");
ball[1]=getImage(getCodeBase(), "ball2.gif");
ball[2]=getImage(getCodeBase(), "ball3.gif");
ball[3]=getImage(getCodeBase(), "ball4.gif");
tracker.addImage(ball[0],0);
tracker.addImage(ball[1],1);
tracker.addImage(ball[2],2);
tracker.addImage(ball[3],3);
try{tracker.waitForAll();
}
catch(InterruptedException e) {}

}

public void paint(Graphics screen) {

offscreen.setColor(Color.white);
offscreen.fillRect(0,0,400,400);
offscreen.setColor(Color.red);

if(currentpicture==0) {
offscreen.drawImage(ball[0],left,top,75,75,this);
}
else if(currentpicture==1){
offscreen.drawImage(ball[1],left,top,75,75,this);
}
else if(currentpicture==2) {
offscreen.drawImage(ball[2],left,top,75,75,this);
}
else {
offscreen.drawImage(ball[3],left,top,75,75,this);
}

screen.drawImage(workspace,0,0,400,400,this);

}

public void start() {

if(runner==null) {
runner=new Thread(this);
}
runner.start();

}

public void run() {

while(true) {
repaint();
checkedge();
left=left+xmove;
try {Thread.sleep(5); }
catch (InterruptedException e) {}
}

}

public void stop() {

if(runner!=null) {
runner.stop();
runner=null;
}

}

public void checkedge() {

if(left<0||left+75>400) {
currentpicture=1;
runner.suspend();
try {
Thread.sleep(10);
}
catch (InterruptedException e){}
runner.resume();
currentpicture=2;
currentpicture=3;
currentpicture=4;
}

}

public void update(Graphics screen) {
paint(screen);
}
}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum