开发者

How can I display an image in the Applet?

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 code :

import java.applet.Applet;
import java.awt.*;


 public class LastAirBender extends App开发者_运维知识库let
 {

 Image aang;

 public void init()
 {

  aang = getImage(getDocumentBase(), getParameter("images.jpg"));
 }

 public void paint(Graphics g) 
 {

    g.drawImage(aang, 100, 100, this);
 }

}


aang = getImage(getDocumentBase(), getParameter("images.jpg"));

I suspect you are doing something wrong, and that should be just plain:

aang = getImage(getDocumentBase(), "images.jpg");

What is the content of HTML/applet element? What is the name of the image? Is the image in the same directory as the HTML?

Update 1

The 2nd (changed) line of code will try to load the images.jpg file in the same directory as the HTML.

Of course, you might need to add a MediaTracker to track the loading of the image, since the Applet.getImage() method returns immediately (now), but loads asynchronously (later).

Update 2

Try this exact experiment:

Save this source as ${path.to.current.code.and.image}/FirstAirBender.java .

/*
<applet class='FirstAirBender' width=400 height=400>
</applet>
*/
import javax.swing.*;
import java.awt.*;
import java.net.URL;
import javax.imageio.ImageIO;

public class FirstAirBender extends JApplet {

    Image aang;

    public void init() {
        try {
            URL pic = new URL(getDocumentBase(), "images.jpg");
            aang = ImageIO.read(pic);
        } catch(Exception e) {
            // tell us if anything goes wrong!
            e.printStackTrace();
        }
    }

    public void paint(Graphics g) {
        super.paint(g);
        if (aang!=null) {
            g.drawImage(aang, 100, 100, this);
        }
    }
}

Then go to the prompt and compile the code then call applet viewer using the source name as argument.

C:\Path>javac FirstAirBender.java
C:\Path>appletviewer FirstAirBender.java
C:\Path>

You should see your image in the applet, painted at 100x100 from the top-left.


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


Well , above answers are correct. This is the code I used to display image. Hope it helps:

/*
    <applet code = "DisplayImage.class" width = 500 height = 300>
    </applet>
*/

import java.applet.Applet;
import java.awt.*;

public class DisplayImage extends Applet
{
    Image img1;

    public void init(){
        img1 = getImage(getCodeBase(),"Nature.jpg" );
    }

    public void paint(Graphics g){
        g.drawImage(img1, 0,0,500,300,this);
    }

}

In above code, we create an image class object and get image from location specified by codebase. Then plot the image using drawImage method. Those who are interested in knowing value of getCodeBase() and getDocumentBase() methods can add following code in paint method. They are actually location of src folder in your project folder:-

    String msg;
    URL url=getDocumentBase();
    msg="Document Base "+url.toString();
    g.drawString(msg,10,20);

    url=getCodeBase();
    msg="Code Base "+url.toString();
    g.drawString(msg,10,40);

One more point to note:- Make sure images and classes don't have same name in src folder. This was preventing my image to be displayed.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜