简体   繁体   中英

How to load and use images in applets?

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);

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

@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.

我在哪里?小程序

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()));
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM