【问题标题】:When running on Eclipse i can see results of paintComponent, but not on HTML在 Eclipse 上运行时,我可以看到paintComponent 的结果,但在 HTML 上看不到
【发布时间】:2012-07-25 18:19:44
【问题描述】:
public class Test extends JApplet {
    public void init () {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createGUI();
            }
        });
    }

    public void createGUI() {
        getContentPane().add(new GUIThing());
    }
}

public class GUIThing extends JPanel {
    BufferedImage image;
    public GUIThing() {
    try {
        image=ImageIO.read(new File("gladiator.gif"));
    } catch (IOException e) {
        e.printStackTrace();
    }
    }
    public void paintComponent(Graphics g) {
        g.drawString("WTF", 20, 20);
        g.drawImage(image,100,100,100, 100, null);
    }
}



<applet code="test.Test"
        archive="test.jar"
        width = 1000,
        height = 1000 >

gladiator.gif 与 HTML 文件放在同一目录中,但它不会被绘制。

我试过不包括gladiator.gif,而不是写字符串Coulnt!!!它显示一个空白的白页...

如果有人想试一试,这就是角斗士:

当我删除与 drawImage 相关的行时,它确实显示了 WT 字符串,所以我猜这只是图像绘制的一些奇怪之处,它可能是什么?

【问题讨论】:

  • 注意:IO不应该在paintComponent()方法中完成。该方法应尽可能保持快速和轻量级。
  • 你说得对,我只是想要一个快速的例子......
  • 我编辑了它,让它更有意义,谢谢!

标签: java html japplet


【解决方案1】:

典型的(不受信任的)小程序无法创建 File 对象,正如您的 Java 控制台会报告的那样。为图像形成一个URL,然后改用它。

【讨论】:

    猜你喜欢
    • 2021-02-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-09
    • 2015-12-17
    • 2018-01-01
    • 1970-01-01
    • 2020-06-04
    相关资源
    最近更新 更多