Hi again.

I'm trying to recreate the digital timer in Minesweeper:
Top right ----- http://www.uberreview.com/wp-content/uploads/506x363-minesweeper.jpg

000 -> 002 -> ... -> 009 -> 010 -> etc...


I couldn't think of an easier way to do it except to create an Icon for each number and match it to it's corresponding integer..

BUT, I cant seem to figure out why this piece of code doesn't draw the image onto the JPanel:

public class DigitalTimer extends JPanel{
		Image ZERO = Toolkit.getDefaultToolkit().getImage("images/Numbers/0.gif");

		public void paintComponent(Graphics g) {
			g.drawImage(ZERO, 30, 30, Color.black, this);
			g.drawLine(0, 0, getWidth(), getHeight());
		}
	}

Here is my transparent .gif for the number 0:
http://img372.imageshack.us/img372/2681/73510927gk3.gif

As you can see from:

g.drawImage(ZERO, 30, 30, Color.black, this);

I'm trying to draw it on a black background so it looks nice.

when I do drawLine(....), it seems to work fine...

Any ideas? For how to do it this way, or if you can think of a better way?

You are just hitting a resource location problem. Your relative file path is not being found and because of the specifics of getImage() and drawImage(), the only visible symptom is no image being drawn. The MediaTracker that load the image is actually creating an image with no dimensions because it can't find the file resource.

I'd recommend loading the image as an ImageIcon through the class loader like this:

Image ZERO = new ImageIcon(getClass().getResource("images/0.gif")).getImage();

If you plan on building the game in a jar file, you would need to include the "images" directory as a package in the jar. You can read a bit more about this here if you like: http://java.sun.com/docs/books/tutorial/uiswing/components/icon.html#getresource

To make it easier on yourself, I would also suggest putting the gifs in an array of Image[] so you can access 0-9 by index instead of having to translate digits to each named variable like ZERO.

To make it easier on yourself, I would also suggest putting the gifs in an array of Image[] so you can access 0-9 by index instead of having to translate digits to each named variable like ZERO.

Yep. Thats what I did :).. I just tried to simplify the source for the question as much as possible :)


Images are loading perfectly.. Thnx :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.