Java thread and applet is not running in eclipse




Asked on July 30, 2015
Hi I am reading Java Threads,by Henry Wong, Scott Oaks and execute the first program in eclipse ide but there is no output. When I am try t run Animate.java, a applet pop-up will open and nothing is there to show.
I have tried to many times but no result found.

TimerThread.java

public class TimerThread extends Thread {
Component comp;
int timediff;
volatile boolean shouldRun;
public TimerThread(Component comp, int timediff) {
this.comp = comp;
this.timediff = timediff;
shouldRun = true;
}
public void run() {
while(shouldRun) {
try {
comp.repaint();
sleep(timediff);
} catch(Exception e){
e.printStackTrace();
}
}
}

}

Animate.java

public class Animate extends Applet {
int count, lastcount;
Image pictures[];
TimerThread timer;
public void init() {
lastcount = 10; count = 0;
pictures = new Image[10];
MediaTracker tracker = new MediaTracker(this);
for (int a = 0; a < lastcount; a++) {
pictures[a] = getImage(getCodeBase(), new Integer(a).toString() +".jpg");
tracker.addImage(pictures[a], 0);
}
tracker.checkAll(true);
}
public void start() {
timer = new TimerThread(this, 1000);
timer.start();
}
public void stop() {
timer.shouldRun = false;
timer = null;
}
public void paint(Graphics g) {
g.drawImage(pictures[count++], 0, 0, null);
if (count == lastcount) {
count = 0;
}
}
}




Replied on July 30, 2015
Put your images(o.jpeg, 1.jpeg) in project root directory.


Replied on July 30, 2015
Did you doublecheck images location? you should have your images (0.jpeg, 1.jpeg, ...) in your codebase dir. (probably bin folder in your project). I tested your code and it works once you have images in the right place .


Write Answer










©2024 concretepage.com | Privacy Policy | Contact Us