E
Emilie Dorchies via JavaKB.com
The following code is ought to init my applet displaying a solution to the CIgarette Smokers Concurrency Problem.
But it returns a NullPointerException while trying to load the images. I've tried also with getDocumentBase : same error.
It drives me insane. Anyone 's got a workaround? Every contribution will be helpful.
Thank you in advance,
Emilie
public class CigarApplet
extends Applet {
public static final int NUM_SMOKER = 3;
public static final int NUM_INGREDIENT = 1;
public Lock lock_ = new Lock();
public Lock matches_ = new Lock();
public Lock paper_ = new Lock();
public Lock tobacco_ = new Lock();
Image[] imgs = new Image[5];
URL[] image;
CigarCanvas display;
Thread[] smoke = new Thread[NUM_SMOKER];
Thread agent = new Thread();
Scrollbar slider;
Button restart;
Button freeze;
boolean isStandalone;
//Initialize the applet
public void init() {
try {
MediaTracker mt = new MediaTracker(this);
image[0] = new URL("http://www.macs.hw.ac.uk/~ed21/MyApplet/images/agent.gif");
imgs[0] = getImage(image[0]);
mt.addImage(imgs[0], 0);
image[1] = new URL("http://www.macs.hw.ac.uk/~ed21/MyApplet/images/matches_waiting.gif");
imgs[1] = getImage(image[1]);
mt.addImage(imgs[1], 1);
image[2] = new URL("http://www.macs.hw.ac.uk/~ed21/MyApplet/images/paper_waiting.gif");
imgs[2] = getImage(image[2]);
mt.addImage(imgs[2], 2);
image[3] = new URL("http://www.macs.hw.ac.uk/~ed21/MyApplet/images/tobacco_waiting.gif");
imgs[3] = getImage(image[3]);
mt.addImage(imgs[3], 3);
image[4] = new URL("http://www.macs.hw.ac.uk/~ed21/MyApplet/images/matches_smoking.gif");
imgs[4] = getImage(image[4]);
mt.addImage(imgs[4], 4);
image[5] = new URL("http://www.macs.hw.ac.uk/~ed21/MyApplet/images/paper_smoking.gif");
imgs[5] = getImage(image[5]);
mt.addImage(imgs[5], 5);
image[6] = new URL("http://www.macs.hw.ac.uk/~ed21/MyApplet/images/tobacco_smoking.gif");
imgs[6] = getImage(image[6]);
mt.addImage(imgs[6], 6);
image[7] = new URL("http://www.macs.hw.ac.uk/~ed21/MyApplet/images/matches.gif");
imgs[7] = getImage(image[7]);
mt.addImage(imgs[7], 7);
image[8] = new URL("http://www.macs.hw.ac.uk/~ed21/MyApplet/images/paper.gif");
imgs[8] = getImage(image[8]);
mt.addImage(imgs[8], 8);
image[9] = new URL("http://www.macs.hw.ac.uk/~ed21/MyApplet/images/tobacco.gif");
imgs[9] = getImage(image[9]);
mt.addImage(imgs[9], 9);
try {
mt.waitForAll();
}
catch (java.lang.NullPointerException e) {
e.printStackTrace();
}
display.initPlacing();
JPanel p1 = new JPanel();
setLayout(new BorderLayout());
p1.add(slider = new Scrollbar(Scrollbar.HORIZONTAL, 50, 5, 0, 100));
p1.add(restart = new Button("Restart"));
p1.add(freeze = new Button("Freeze"));
p1.setLayout(new BorderLayout());
display = new CigarCanvas(this);
display.setSize(300, 320);
p1.add("Center", display);
}
catch (Exception e) {
e.printStackTrace();
}
}
But it returns a NullPointerException while trying to load the images. I've tried also with getDocumentBase : same error.
It drives me insane. Anyone 's got a workaround? Every contribution will be helpful.
Thank you in advance,
Emilie
public class CigarApplet
extends Applet {
public static final int NUM_SMOKER = 3;
public static final int NUM_INGREDIENT = 1;
public Lock lock_ = new Lock();
public Lock matches_ = new Lock();
public Lock paper_ = new Lock();
public Lock tobacco_ = new Lock();
Image[] imgs = new Image[5];
URL[] image;
CigarCanvas display;
Thread[] smoke = new Thread[NUM_SMOKER];
Thread agent = new Thread();
Scrollbar slider;
Button restart;
Button freeze;
boolean isStandalone;
//Initialize the applet
public void init() {
try {
MediaTracker mt = new MediaTracker(this);
image[0] = new URL("http://www.macs.hw.ac.uk/~ed21/MyApplet/images/agent.gif");
imgs[0] = getImage(image[0]);
mt.addImage(imgs[0], 0);
image[1] = new URL("http://www.macs.hw.ac.uk/~ed21/MyApplet/images/matches_waiting.gif");
imgs[1] = getImage(image[1]);
mt.addImage(imgs[1], 1);
image[2] = new URL("http://www.macs.hw.ac.uk/~ed21/MyApplet/images/paper_waiting.gif");
imgs[2] = getImage(image[2]);
mt.addImage(imgs[2], 2);
image[3] = new URL("http://www.macs.hw.ac.uk/~ed21/MyApplet/images/tobacco_waiting.gif");
imgs[3] = getImage(image[3]);
mt.addImage(imgs[3], 3);
image[4] = new URL("http://www.macs.hw.ac.uk/~ed21/MyApplet/images/matches_smoking.gif");
imgs[4] = getImage(image[4]);
mt.addImage(imgs[4], 4);
image[5] = new URL("http://www.macs.hw.ac.uk/~ed21/MyApplet/images/paper_smoking.gif");
imgs[5] = getImage(image[5]);
mt.addImage(imgs[5], 5);
image[6] = new URL("http://www.macs.hw.ac.uk/~ed21/MyApplet/images/tobacco_smoking.gif");
imgs[6] = getImage(image[6]);
mt.addImage(imgs[6], 6);
image[7] = new URL("http://www.macs.hw.ac.uk/~ed21/MyApplet/images/matches.gif");
imgs[7] = getImage(image[7]);
mt.addImage(imgs[7], 7);
image[8] = new URL("http://www.macs.hw.ac.uk/~ed21/MyApplet/images/paper.gif");
imgs[8] = getImage(image[8]);
mt.addImage(imgs[8], 8);
image[9] = new URL("http://www.macs.hw.ac.uk/~ed21/MyApplet/images/tobacco.gif");
imgs[9] = getImage(image[9]);
mt.addImage(imgs[9], 9);
try {
mt.waitForAll();
}
catch (java.lang.NullPointerException e) {
e.printStackTrace();
}
display.initPlacing();
JPanel p1 = new JPanel();
setLayout(new BorderLayout());
p1.add(slider = new Scrollbar(Scrollbar.HORIZONTAL, 50, 5, 0, 100));
p1.add(restart = new Button("Restart"));
p1.add(freeze = new Button("Freeze"));
p1.setLayout(new BorderLayout());
display = new CigarCanvas(this);
display.setSize(300, 320);
p1.add("Center", display);
}
catch (Exception e) {
e.printStackTrace();
}
}