帮助在java小程序中显示图像

发布于 2024-12-03 19:30:00 字数 1166 浏览 1 评论 0原文

下面是我编写的一个简单的小程序,用于显示单张图片。代码编译良好,小程序会加载,但图像文件永远不会绘制到小程序中。我认为它无法使用 this.getImage(appletBaseURL, filename); 找到图像我将图像文件存储在与该包关联的所有文件夹中,但它仍然没有绘制它。

    import java.applet.Applet;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Image;
    import java.io.File;
    import java.io.IOException;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import javax.imageio.ImageIO;


   public class imageTest extends Applet {

    private Image spaceShip;    
    private final String filename = "spaceshipcropped.jpg";        
    public void init() {

        java.net.URL appletBaseURL = getCodeBase();
        File file = new File("spaceshipcropped.jpg");
        try {
            spaceShip = ImageIO.read(file);
        } catch (IOException ex) {
            Logger.getLogger(imageTest.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void paint(Graphics g)
    {
        Graphics2D g2d = (Graphics2D)g;    
        g2d.drawImage(spaceShip, 0,0, null);
    }

    public void update(Graphics g) {
       repaint();
    }
}

在我进行这些更改后,它起作用了。非常感谢大家的帮助!

Below is a simple applet im writing to display a single picture. The code compiles fine, and the applet loads but the image file is never drawn to the applet. Im thinking that it cant find the image using the this.getImage(appletBaseURL, filename); I have the image file stored in all the folders associated with this package but its still not drawing it.

    import java.applet.Applet;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Image;
    import java.io.File;
    import java.io.IOException;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import javax.imageio.ImageIO;


   public class imageTest extends Applet {

    private Image spaceShip;    
    private final String filename = "spaceshipcropped.jpg";        
    public void init() {

        java.net.URL appletBaseURL = getCodeBase();
        File file = new File("spaceshipcropped.jpg");
        try {
            spaceShip = ImageIO.read(file);
        } catch (IOException ex) {
            Logger.getLogger(imageTest.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void paint(Graphics g)
    {
        Graphics2D g2d = (Graphics2D)g;    
        g2d.drawImage(spaceShip, 0,0, null);
    }

    public void update(Graphics g) {
       repaint();
    }
}

After i made theses changes it worked. thank you all very much for your help!

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

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

发布评论

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

评论(2

〗斷ホ乔殘χμё〖 2024-12-10 19:30:00
  1. 不要在小程序中调用 setSize()。大小由 HTML 设置。
  2. 在这个世纪,不要使用 AWT 编写代码。
  3. 传递给 Swing 组件的对象应该是 Graphics2D 对象,但我从未听说过 Applet 也有同样的说法。您正在检查 Java 控制台吗?
  4. 该代码有一些多余的导入。
  5. paint 方法中,检查图像是否为 null
  6. getImage(URL,String) 方法的 JavaDocs 声明“此方法始终立即返回,无论图像是否存在。” 添加一个 MediaTracker code> 或在第三个千年加入我们并使用 ImageIO.read(URL) - 它会阻塞,直到图像加载完毕。

我希望修复第 6 点可以解决问题,但也要注意其他 5 点。

  1. Don't call setSize() in an applet. The size is set by HTML.
  2. Don't code in AWT in this millennium.
  3. The object passed to a Swing component should be a Graphics2D object, but I've never heard the same said of an Applet. Are you checking the Java Console?
  4. That code has some redundant imports.
  5. In the paint method, check to see if the image is null.
  6. The JavaDocs for the getImage(URL,String) method state "This method always returns immediately, whether or not the image exists." Either add a MediaTracker or join us in the 3rd millennium and use ImageIO.read(URL) - which blocks until the image is loaded.

I expect that fixing point 6 will solve the problem, but attend to the other 5 points as well.

前事休说 2024-12-10 19:30:00

添加这个

public void update(Graphcs g) {
    repaint();
}

add this

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