If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Image loading and showing problem

Hi,

I'm trying to make a little java application for my website, but I have
to load images. The compiling of the code gives no errors, but when I
run it, it says "Applet not initialized" . I know wich row the message
causes, but I don't know how to solve it.
Here's the code:

Yes that does work, but my problem actually is that I want to make other classes as well who will draw images (this DeskTop class is just a small test to draw such an image in a class).
I have had one year of Java at school now an I know how to write these classes etc., but I never used images before.
They aren't handled in my book ("En dan is er... Java" from Academic Service) and the way you told me is the same way I found on the net .

But my way doesn't seem to work to load external images in other classes.
Maybe you know how I should do this or know a site where I can find a clear explanation?

A very basic image applet ++

If you want to make "something" in java that shows images you should make a class that extends a JPanel and limit its capabilities to the image rendering only. That way you have a component that you "feed" images to and it can be placed anywhere, in an applet or frame, as single or multiple instances.

I have also included a slightly more elaborate image applet that allows for client interaction for zooming and panning of an image. It takes an applet parameter named "image-url" for the image server path.

The idea is that I create some kind of MDI window (more windows desktop-style).
The DeskTop class shoul be the background, and a TaskBar class should hold some buttons, a clock, ...)
A WinForm class would represent a window with elements.
Etc.

Now these classes all use images, and to draw them easily in each class I am looking for a way to draw them in the class self (using a method paint( Graphics g) and in de main public class App I use the paint() method to call the paint methods of the subclasses.

I hope this brings some clearity

PS: it's only the image drawing that causes my problem , not the rest of the code if you should think "he wants to make something without knowing what he is doing" or so . I never worked with them and my book doesn't really give me a good explaination about images...

PPS: your (sjalle) code is working for my public class App but I would like to draw an image in

If you use swing you should override the paintComponent(Graphics g) method of JComponent. That class is part of the base for (nearly) all visible components. This method, if used in the ImagePanel, will give you a panel that you can draw on, while at the same time you add components to it, like buttons, other panels, etc. Just remember to use absolute positioning (setBounds) for these components and a layoutmanager = null for the panel.

You could also make your DeskTop an extension of JDesktopPane and override its paintComponent method for doing the drawing. The JDesktopPane is the base for MDI applications in java, you can add JInternalFrames to it. Check out the documentation for this.

Too high...

You have made your DeskTop class an extension of JComponent, don't do that, its too high in the class hierachy for your use. Make it a JApplet extension. An applet is really just a panel (component) equipped for travelling on the internet . - and its a JComponent descendant.

So. change

Code:

class DeskTop extends JComponent

to

Code:

class DeskTop extends JApplet

then just rename your constructor:

Code:

public DeskTop()

to

Code:

public void init()

the init method is invoked by the browser when your applet page is shown.

Btw: I have included a hybrid version (applet and application in one) here, its really no big deal..