如何在Applet中显示图像?

发布于 2024-12-05 04:13:13 字数 384 浏览 2 评论 0原文

我有一个图像,我想在小程序中显示它,问题是图像不会显示。我的代码有问题吗?

谢谢...

这是我的代码:

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

}

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 Applet
 {

 Image aang;

 public void init()
 {

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

 public void paint(Graphics g) 
 {

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

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

始终不够爱げ你 2024-12-12 04:13:13
aang = getImage(getDocumentBase(), getParameter("images.jpg"));

我怀疑你做错了什么,这应该很简单:

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

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

更新 1

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

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

更新 2

尝试这个精确的实验:

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

然后转到提示符并编译代码,然后使用源名称作为参数调用小程序查看器。

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

您应该在小程序中看到您的图像,从左上角开始以 100x100 的尺寸绘制。

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.

油焖大侠 2024-12-12 04:13:13

1) 我们生活在 21 世纪,那么请使用 JApplet 而不是 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 最好寻找 JLabel

3)请问什么是 getImage(getDocumentBase(), getParameter("images.jpg"));

我将等待类似的内容

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

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));
心是晴朗的。 2024-12-12 04:13:13

嗯,上面的答案是正确的。这是我用来显示图像的代码。希望它有帮助:

/*
    <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);
    }

}

在上面的代码中,我们创建一个图像类对象并从代码库指定的位置获取图像。然后使用drawImage方法绘制图像。有兴趣了解 getCodeBase() 和 getDocumentBase() 方法的价值的人可以在 Paint 方法中添加以下代码。它们实际上是项目文件夹中 src 文件夹的位置:-

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

还有一点需要注意:- 确保 src 文件夹中的图像和类没有相同的名称。这导致我的图像无法显示。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文