Frage

I've been going at it for 2 days now and I can't find a way how to load images properly. The applet is going to be run locally on a different computer, and it's not in a jar file (for one previous applet, for the same project, I've only used the .class files and used THEM in directly in the HTML source code). My questions is: how do you load and use images which have a relative path (possibly next to the .class files) ? Here's part of my code

try {
    image = null;
    InputStream is = new BufferedInputStream( new FileInputStream( System.getProperty("user.dir")+"\\toshiba.jpeg" )); 
    image = ImageIO.read(is); 
    image = image.getScaledInstance(400,200,Image.SCALE_SMOOTH); 
} 
catch (IOException e) {} 
Icon ic = new ImageIcon(image);
War es hilfreich?

Lösung 2

@RoxasShadow Try to use getDocumentBase().
@Nick992 tried it and it returns null, I think I've read somewhere that you can use that only with applets that are accessed on remote websites and not locally.

There is no accounting for what people think.

Where Am I? Applet

import java.awt.GridLayout;
import javax.swing.*;

// <applet code=WhereAmIApplet width=500 height=50></applet>
public class WhereAmIApplet extends JApplet {

    @Override
    public void init() {
        setLayout(new GridLayout(0,1,5,5));
        add(new JLabel("Document Base: " + getDocumentBase()));
        add(new JLabel("Code Base: " + getCodeBase()));
    }
}

Andere Tipps

Try to use getDocumentBase(). It returns the path of the folder where the applet is stored.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top