applet not displaying images

i have created a class which extends an abstract class that extends applet, and this class fails to show images when i use the graphics.drawImage command in paint.

note: i am using appletviewer

package hangman;

...

publicabstractclass Hangmanextends Applet

implements KeyListener

{

...

}

package christmas;

...

publicclass Christmasextends hangman.Hangman

{

...

private Image[]noosePosition;

private Imageescape;

privateintindex;

...

publicvoid init()

{

noosePosition =new Image[11];

for (int i = 0; i < noosePosition.length; i++)

noosePosition[i] = getImage(getCodeBase(),"images/noose" + i +".bmp");

...

escape= getImage(getCodeBase(),"images/escape.bmp");

index= 0;

...

}

...

publicvoid paint(Graphics g)

{

int x = 40;

switch (index)

{

case -1: g.drawImage(escape, 150, x, 50, 100,this);break;

default: g.drawImage(noosePosition[index], 150, x, 50, 100,this);

break;

}

...

}

}

upon winning the index is changed to -1 and each wrong guess increments the index by 1. I know that the paint command is called because other code in the paint method is executed. everything else works in the program and it produces no errors.

the directories are as such:

my stuff (base directory)

hangman (folder)

Hangman.class

[two inner classes of hangman]

jdk profiles (folder)

christmas (folder)

Christmas.class

Hangman.java

Christmas.java

images (folder)

[all bitmap images referred to in code]

Christmas.html

the html file is as such:

<HTML>

<HEAD>

<TITLE>A Christmas Program</TITLE>

</HEAD>

<BODY>

You may need to give authorization to run java applet.

<APPLET CODE="christmas\Christmas.class" WIDTH=500 HEIGHT=500>

</APPLET>

</BODY>

</HTML>

[3437 byte] By [no1unoa] at [2007-11-26 13:18:30]
# 1
Hi,Can you not just supply a URL straight to the source and see if that works? getImage(new URL("images/noose" + i + ".bmp"));Regards,Chris
lordflashearta at 2007-7-7 17:43:53 > top of Java-index,Java Essentials,New To Java...
# 2
i tried that and got a MalformedURLException error with the following message:no protocol: images/noose0.bmpshouldn't the URI give some reference to its parent directory?
no1unoa at 2007-7-7 17:43:53 > top of Java-index,Java Essentials,New To Java...
# 3
and is it URI or URL? cuz the API said it was URI, but it seems to only compile if i use URL.
no1unoa at 2007-7-7 17:43:53 > top of Java-index,Java Essentials,New To Java...
# 4
Personally I would try "../images/" instead and see if that works. I suspect your code base is the christmas directory, but I don't work with enough applets to be sure. You could also try moving your images folder under your christmas folder and if it works then you know that's the
kablaira at 2007-7-7 17:43:53 > top of Java-index,Java Essentials,New To Java...
# 5

> You could also try moving your

> images folder under your christmas folder and if it

> works then you know that's the issue.

i already tried that.

> I suspect your code base is the

> christmas directory.

no. as stated above, my code base is the christmas directory's parent directory.

but i will try your idea.

no1unoa at 2007-7-7 17:43:53 > top of Java-index,Java Essentials,New To Java...
# 6
Try puting the images in the code base and use Class.getResource method to get the images also
LRMKa at 2007-7-7 17:43:53 > top of Java-index,Java Essentials,New To Java...
# 7
As you get a MalformedURLException have you tried appending file:// to the start of your path?
lordflashearta at 2007-7-7 17:43:53 > top of Java-index,Java Essentials,New To Java...
# 8
Simple solution. Your java code is actually error-free. It's the HTML.Change<APPLET CODE="christmas\Christmas.class" WIDTH=500 HEIGHT=500>to<APPLET CODE="christmas.Christmas" WIDTH=500 HEIGHT=500>
Lord_Jirachia at 2007-7-7 17:43:53 > top of Java-index,Java Essentials,New To Java...
# 9

> to

> <APPLET CODE="christmas.Christmas" WIDTH=500

> HEIGHT=500>

I think it won't work because of parameter CODE. The browser or Appletviewer can just think that it's a file "christmas" with a Christmas-extension.

Any case, my applets work in a such way:

<APPLET CODEBASE="christmas" CODE="Christmas" WIDTH=500 HEIGHT=500>

Message was edited by:

stopper

stoppera at 2007-7-7 17:43:53 > top of Java-index,Java Essentials,New To Java...