This week's book giveaway is in the Open Source Projects forum.
We're giving away four copies of Spark in Action and have Jean-Georges Perrin on-line!
See this thread for details.
Win a copy of Spark in Action this week in the Open Source Projects forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Problem in Image Loading in Swing Applet

Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a swing applet that loads image files through ImageIcon("xxx.gif"). It works fine with appletviewer of Kawa and Visual Cafe (even it works fine with netscape when called from local harddisk). Problem started when I put my applet into Apache (and also WebSite) webserver. Initially got a security exception not having permission to read files (though these are server-side files and in theory it does not need any permission). Anyway, after consulting lots of documentation and postings in the forum, I managed to fix this by setting the Filepermision in the right java.policy file.
Now, my applet runs but with out any images loaded. Any help or pointer will be highly appreciated.
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you using the loadImage() method to force the application to wait for the image to be loaded? You may also want to use getImageLoadStatus() afterwards to see what it says. Also - you haven't put in any code that catches exceptions without doing anything about them, have you? I've seen people put in code like
<code><pre> catch (Exception e) {}</pre></code>
because the compiler forces them to put in a catch statement. But of course when an exception is thrown, this hides it from the viewer. Obviously, this would be bad.
You may also want to post your code here. Cut out everything but what's necessary for the image loading & displaying, and make sure that what you have still runs on Kawa and Visual Cafe, but not Apache. Maybe we'll have other ideas when we see just what you're doing.
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply.
I have used getImageLoadStatus(), getting 4 which is MediaTracker.ERRORED. But get 8 (MediaTracker.COMPLETED) when I run from KAWA Appletviewer.
I am attaching my code here. Pls run this program with some images as in the code - sorry I can't attach my image files with this message.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class DE_Main extends JApplet {

public JLabel Caption1 = new JLabel();
public JLabel Caption2 = new JLabel();
public JLabel ImageCoin = new JLabel();
public JLabel ITECaption = new JLabel();
public JButton StartButton = new JButton();
public JLabel JavaLogo = new JLabel();
// setting up the icons
//public Icon javalogo = new ImageIcon("javalogo.gif");
//public Icon javalogo = new ImageIcon("100_JAVA1.gif");
//public ImageIcon javalogo = new ImageIcon(getDocumentBase(), "100_JAVA1.gif");
// I got nullpointer error whenever I try to use
// getDocumentBase() or getCodeBase()
public ImageIcon javalogo = new ImageIcon("100_JAVA3.gif");

public Icon itelogo = new ImageIcon("itelogoIcon.gif");
public Icon imageIcon1 = new ImageIcon("coins.jpg");
public Icon imageIcon2 = new ImageIcon("itelogoIS.gif");

public DE_Main() { // Constructor

// this is left blank intensionally


public String getAppletInfo() {
return "Name : \r\n"+
"Author: Kazi Sabbir Ahmed \r\n"+
"Created with Kawa 3.2 and JDK 1.2";

// making button event listener - inner class
class MyButtonListener implements ActionListener {

public void actionPerformed (ActionEvent event) {
// do action
if (event.getActionCommand().equals("Start")) {

showStatus("Double Entry Tutorial Button Pressed");


} else if (event.getActionCommand().equals("Finish")) {
// do something

} // End of if-elseif

} // End of actionPerformed()
} // End of ActionListener

public void init () {

// Force SwingApplet to come up in the System L&F
String laf = UIManager.getSystemLookAndFeelClassName();
try {
// If you want the Cross Platform L&F instead, comment out the above line and
// uncomment the following:
// UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
} catch (UnsupportedLookAndFeelException exc) {
System.err.println("Warning: UnsupportedLookAndFeel: " + laf);
} catch (Exception exc) {
System.err.println("Error loading " + laf + ": " + exc);

getContentPane().setBackground(new java.awt.Color(121,132,134));

Caption1.setText("Web-Based Tutoring Systems for");
Caption1.setForeground(new java.awt.Color(39,176,216));
Caption1.setFont(new Font("Dialog", Font.BOLD, 22));

Caption2.setText("Double Entry");
Caption2.setForeground(new java.awt.Color(204,194,11));
Caption2.setFont(new Font("Dialog", Font.BOLD, 30));

StartButton.setToolTipText("Click Here to Start");
StartButton.addActionListener(new MyButtonListener());
StartButton.setBackground(new java.awt.Color(84,101,171));
StartButton.setFont(new Font("MonoSpaced", Font.BOLD, 30));


ITECaption.setText("Institute of Technical Education");
ITECaption.setFont(new Font("Dialog", Font.BOLD|Font.ITALIC, 17));
System.out.println("LOading Image..");

// getImageLoadStutas(


} // end of init()

public void start() {
addItem("starting... ");
public void stop() {
addItem("stopping... ");
public void destroy() {
addItem("preparing for unloading...");
void addItem(String newWord) {
// public void paint(Graphics g) {

// intensionally left blank
// }


[This message has been edited by Jim Yingst (edited March 14, 2000).]
Jim Yingst
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ugh. That looks at least six times as long as it needs to be for this discussion. How about we just focus on the code for one icon, such as javalogo, and cut out everything except what you need to display it? Then it will be much easier to see what's going on. Also, you can use the [ code ] tag to make the code more readable - read about it here.
You don't seem to have tried the loadImage() method I suggested. That's really the only other idea I have offhand. If it's not that, my guess is that it's something about how you have Apache set up, and I'm afraid I dont know anything about that. If no one else has suggestions, you may also want to try one of the other forums here, such as Linux/UNIX or perhaps Java in General, intermediate or advanced.
Jim Yingst
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oops - looking again at loadImage(), I see it doesn't do what I thought it did. Never mind. But here's a simplified version that isolates the ImageIcon stuff and may give you some more information about what's wrong. (It works fine on my system.) Note that I used a JFrame rather than a JApplet, simply because I'm more familiar with how they work. Shouldn't be too tough to convert it back to a JApplet if you prefer.

[This message has been edited by Jim Yingst (edited March 14, 2000).]
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Jim.
But still not good enough to work with JApplet. I have found that apache behaves differently than WebSite. When I use the following syntax for loading a Image in a ImageIcon variable, apache can't show the image whereas WebSite can.
But have no success when my Applet calls any JFrame class which has Images to display. I got "NullPointerError" when I use the above code to load an image.
I wonder for the following things:
1. Why I need to set File permission in java.policy file (at the client side) in order to get no security error even my applet just reading files from its originating server.
2. Why I can't load an image into ImageIcon in my JApplet by the following syntax:
ImageIcon Img = new ImageIcon("img.gif");
when .class file and the img.gif file are at the same directory of the web server.
3. Anyway, the following code can load an image -
but gives a NullPointerError when called from a class file which extends a JFrame - here "app" is a public instance of the main Applet.
Jim Yingst
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well to summarize my answers, I don't know . Perhaps someone else has suggestions? But here are a few thoughts:
  1. Maybe they just want to force you to set a policy? It seems silly to me too in this case - but at least you know the solution to this part.
  2. No idea. But if you can get #3 to work, it won't matter...
  3. I don't know why there's a NullPointerError, but you can get a better idea what's causing it by splitting the line up:
    <code><pre> URL codeBase = app.getCodeBase();
    System.out.println("codeBase: " + codeBase);
    Image image = app.getImage(codeBase,"image.gif");
    System.out.println("image: " + image);
    Icon icon = new ImageIcon(image);
    System.out.println("icon: " + icon);</pre></code>If only the first println gets executed, check to see if the codeBase URL that gets printed is the one where the image.gif is really located. If the second and maybe even third line are printed, look to see if they're null or not. (If not, they'll probably give you some gibberish that represents an object reference - that's fine).

  4. [This message has been edited by Jim Yingst (edited March 20, 2000).]
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Jim once again. Your thoughtful suggestions inspired me to think differently about my problems.
Now I got a workaround - passing Main Applet as a parameter to other JFrame classes fix my NullPointerError and get my Images displayed.
I simply don't understand why displaying images so difficult in the JDK 1.2 (specially when Swing is involved). What's the Sun's view/comment on this? Anybody got any clue?
Hot dog! An advertiser loves us THIS much:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
    Bookmark Topic Watch Topic
  • New Topic