Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problems loading an image in an applet 1

Status
Not open for further replies.

timmay3141

Programmer
Dec 3, 2002
468
US
I'm trying to make an applet draw some dice images. The images are in files named die1.png through die6.png. Here's my code for loading and displaying them:

[source]
public void loadImages() {
for(int i = 0; i < 6; i++) {
URL imageUrl = getClass().getResource("/die" + (i+1) + ".png");
diceImages = Toolkit.getDefaultToolkit().getImage(imageUrl);
}
}

public void paint(Graphics g) {
super.paint(g);
Graphics2D g2 = (Graphics2D) g;

// draw the dice
for(int i = 0; i < 5; i++) {
g2.drawImage(diceImages[dice - 1], 40 * i + 50, 75, this);
}
}
[/source]

By the way, this is using swing, not AWT if it matters. I'm using eclipse to run this as an applet, and it works fine on my computer. However, when I load it on to the net, the images don't show up. I'm inexperienced debugging applets in Java - I have debugged applications enough in C++ and C#, but debugging applets seems to be trickier. Any tips for debugging applets in general would be appreciated.
 
Say your applet is in /myCodeBase, then you need to do this :

URL u = getCodeBase();

which will return a URL reference to the directory /myCodeBase.

The you can construct a new URL to the image :

URL imageUrl = u +"/die1.png";

and then load the image as normal.

--------------------------------------------------
Free Database Connection Pooling Software
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top