How can I display an image in the Applet?

后端 未结 3 1416
执念已碎
执念已碎 2020-12-09 14:20

I have an image and I want to display it in the applet, The problem is the image wont display. Is there something wrong with my code?

Thanks...

Here\'s my co

3条回答
  •  难免孤独
    2020-12-09 14:46

    1) we living .. in 21century, then please JApplet instead of Applet

    import java.awt.*;
    import javax.swing.JApplet;
    
    public class LastAirBender extends JApplet {
    
        private static final long serialVersionUID = 1L;
        private Image aang;
    
        @Override
        public void init() {
            aang = getImage(getDocumentBase(), getParameter("images.jpg"));
        }
    
        @Override
        public void paint(Graphics g) {
            g.drawImage(aang, 100, 100, this);
        }
    }
    

    2) for Icon/ImageIcon would be better to look for JLabel

    3) please what's getImage(getDocumentBase(), getParameter("images.jpg"));

    there I'll be awaiting something like as

    URL imageURL = this.getClass().getResource("images.jpg");
    Image image = Toolkit.getDefaultToolkit().createImage(imageURL);
    Image scaled = image.getScaledInstance(100, 150, Image.SCALE_SMOOTH);
    JLabel label = new JLabel(new ImageIcon(scaled));
    

提交回复
热议问题