This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes Problem in Image Loading in Swing Applet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Problem in Image Loading in Swing Applet" Watch "Problem in Image Loading in Swing Applet" New topic
Author

Problem in Image Loading in Swing Applet

Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
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.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
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.


"I'm not back." - Bill Harding, Twister
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
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.*;
import java.net.*;
import java.io.*;
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 : DE_Main.java \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")) {
Toolkit.getDefaultToolkit().beep();

showStatus("Double Entry Tutorial Button Pressed");

//doFrame();

} 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 {
UIManager.setLookAndFeel(laf);
// 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().setLayout(null);
getContentPane().setBackground(new java.awt.Color(121,132,134));
setSize(444,511);

Caption1.setHorizontalAlignment(JLabel.CENTER);
Caption1.setText("Web-Based Tutoring Systems for");
getContentPane().add(Caption1);
Caption1.setForeground(new java.awt.Color(39,176,216));
Caption1.setFont(new Font("Dialog", Font.BOLD, 22));
Caption1.setBounds(36,24,369,36);

Caption2.setHorizontalAlignment(JLabel.CENTER);
Caption2.setText("Double Entry");
getContentPane().add(Caption2);
Caption2.setForeground(new java.awt.Color(204,194,11));
Caption2.setFont(new Font("Dialog", Font.BOLD, 30));
Caption2.setBounds(96,60,252,42);

StartButton.setText("Start");
StartButton.setToolTipText("Click Here to Start");
StartButton.addActionListener(new MyButtonListener());
getContentPane().add(StartButton);
StartButton.setBackground(new java.awt.Color(84,101,171));
StartButton.setForeground(java.awt.Color.orange);
StartButton.setFont(new Font("MonoSpaced", Font.BOLD, 30));
StartButton.setBounds(132,372,173,51);

ImageCoin.setHorizontalAlignment(JLabel.CENTER);
getContentPane().add(ImageCoin);
ImageCoin.setBounds(36,108,368,240);

ITECaption.setHorizontalAlignment(JLabel.CENTER);
ITECaption.setIconTextGap(24);
ITECaption.setText("Institute of Technical Education");
getContentPane().add(ITECaption);
ITECaption.setForeground(java.awt.Color.white);
ITECaption.setFont(new Font("Dialog", Font.BOLD|Font.ITALIC, 17));
ITECaption.setBounds(12,444,312,36);
JavaLogo.setHorizontalAlignment(JLabel.CENTER);
JavaLogo.setIconTextGap(24);
getContentPane().add(JavaLogo);
JavaLogo.setForeground(java.awt.Color.white);
JavaLogo.setBounds(336,396,96,84);
ImageCoin.setIcon(imageIcon1);
ITECaption.setIcon(imageIcon2);
System.out.println("LOading Image..");

// getImageLoadStutas(
System.out.println(Integer.toString(javalogo.getImageLoadStatus()));



JavaLogo.setIcon(javalogo);

} // end of init()

public void start() {
addItem("starting... ");
}
public void stop() {
addItem("stopping... ");
}
public void destroy() {
addItem("preparing for unloading...");
}
void addItem(String newWord) {
System.out.println(newWord);
//buffer.append(newWord);
repaint();
}
// public void paint(Graphics g) {

// intensionally left blank
// }

}

[This message has been edited by Jim Yingst (edited March 14, 2000).]
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
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
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
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).]
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
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.
ImageIcon(Applet.getImage(Applet.getCodeBase(),"image.gif"));
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 -
ImageIcon(app.getImage(app.getCodeBase(),"image.gif"));
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
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
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).]
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
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?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problem in Image Loading in Swing Applet
 
Similar Threads
how to simulate a button click
Granting an applet read permission
Applets and OS X
Issues with Jar Files and loading images
Socket Permission in JavaPolicy file..