Using the paint method in an Applet or JApplet is tricky because it will draw on top of everything, including your components (see api). An easier way to get started is to use a separate image component as the content pane, set its layout and add the components to it. The image will be in the background. In Swing ('J' prefix on the components) we usually do not need to override the update method though it is common to do so for different uses in the AWT. Loading images inside an applet that has a main method is also tricky: you need an applet method, which you have above, for applet context and an application method, as in Toolkit with MediaTracker or ImageIcon, for the application context. You can avoid this with an inner or outer class that loads its own image (or with the newer ImageIO class - j2se 1.4).

Jamie Wool
Ranch Hand

Joined: May 03, 2004
Posts: 49

posted Apr 18, 2005 10:01:00

0

Thanks for that, but that seems to have brought on securtiy exceptions when loading the applet.

java.security.AccessControlException: access denied (java.io.FilePermission images/skin1.gif read) at java.security.AccessControlContext.checkPermission(Unknown Source) at java.security.AccessController.checkPermission(Unknown Source) at java.lang.SecurityManager.checkPermission(Unknown Source) at java.lang.SecurityManager.checkRead(Unknown Source) at sun.awt.SunToolkit.getImageFromHash(Unknown Source) at sun.awt.SunToolkit.getImage(Unknown Source) at javax.swing.ImageIcon.<init>(Unknown Source) at javax.swing.ImageIcon.<init>(Unknown Source) at AnImageComponent.<init>(JF1.java:42) at JF1.init(JF1.java:15) at sun.applet.AppletPanel.run(Unknown Source) at java.lang.Thread.run(Unknown Source)

Jamie Wool
Ranch Hand

Joined: May 03, 2004
Posts: 49

posted Apr 18, 2005 12:19:00

0

Ok, i've figurd out the permissions problem, but still having problems with the code. With your example i no longer get the applet pop-up in a frame and it doesn't seem to add the background or the burttons?

Jamie Wool
Ranch Hand

Joined: May 03, 2004
Posts: 49

posted Apr 18, 2005 12:28:00

0

Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535

posted Apr 18, 2005 21:52:00

0

The code in your last post runs okay in j2se 1.5. You might try another image file. The JFrame was meant to be a pop-up? I suggest you consider using a JOptionPane or JDialog for a pop-up container. You can launch it in your event code, say in the ActionListener of a JButton. If it must be launched during construction, ie, in init, I would opt for a JOptionPane.