如何在Applet中显示图像? [英] How can I display an image in the Applet?

查看:81
本文介绍了如何在Applet中显示图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个图像,我想在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?

谢谢...

这是我的代码:

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


 public class LastAirBender extends Applet
 {

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

HTML / applet元素的内容是什么?图像的名称是什么?该图像与HTML在同一目录中吗?

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?

第二行(已更改)代码将尝试在与HTML相同的目录中加载 images.jpg 文件。

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

当然,你可能需要添加 MediaTracker 来跟踪图像的加载,因为 Applet.getImage()方法立即返回(现在),但异步加载(稍后)。

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

试试这个确切的实验:

将此来源保存为 $ {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);
        }
    }
}

然后转到提示并且编译代码,然后使用源名称作为参数调用applet viewer。

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>

你应该在小程序中看到你的图像,从左上角以100x100绘制。

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

这篇关于如何在Applet中显示图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆