在网站上运行时,Java Applet上的图像不显示;为什么?

时间:2014-12-16 00:04:18

标签: java html eclipse image applet

我正在编写一个Java Applet。我正在使用图像在Applet窗口上显示内容。我有一个单独的文件夹,其中包含所有这些图像。此文件夹不在src或bin文件夹中,而是在单独的文件夹中。此文件夹称为“资产”。在此文件夹中,有一个名为“images”的文件夹。在这个资产文件夹中,我有几个子文件夹。在这些子文件夹中,我有我正在使用的图像。我使用下面列出的方法加载这些图像并将它们显示在屏幕上。 Applet在Eclipse中运行良好。当我尝试将Applet放到网站上时会出现问题。我做了很多搜索,但是在我尝试过的事情中,没有一个能解决我的问题。我扫描图像的某些像素颜色。我用于图像的代码如下所示。当我查看我的.jar文件时,我的图像似乎在.jar文件中。 (我使用Unarchived和JD-GUI查看.jar​​文件)请告诉我应该怎么做。

〜录音棚

在屏幕上绘制图像的方法:

loadImg.renderImage("images/walls/lawn.png", xCoord, yCoord, imgWidth, imgHeight, graphics);

renderImage方法:

    public void renderImage(String path, int x, int y, int width, int height,
        Graphics g) {

    g.setColor(Color.BLACK);
    g.fillRect(x, y, width, height);
    g.drawImage(getImage(path), x, y, width, height, applet);

}

getImage方法:

    public Image getImage(String path) {

    Image img;
    URL url = null;

    try {
        url = applet.getDocumentBase();
    } catch (Exception e) {
        // TODO: handle exception
    }

    img = applet.getImage(url, path);

    return img;
}

我的HTML:

    <object type="application/x-java-applet" archive="Applet.jar" code="AppletGame.class" width="600" height="600"></object>

许多人中有两人回答说我不适合我:

http://www.java-gaming.org/index.php?topic=27203.0

Java Applet Images will not display when run in browser

1 个答案:

答案 0 :(得分:0)

我发现了我的问题。在我的Applet中,我通过扫描图像获取某个像素颜色值来加载游戏地图。在网站上,Applet是白色的。在Eclipse中,屏幕显示为我想要的。原因是由于网页上的Java Applet存在安全问题。每次尝试扫描图像时都会抛出一个安全异常,因为我使用的是File。我需要使用URL。我调整了我的代码,现在它在网站上运行正常。

没有你的帮助,MadProgrammer我无法做到这一点。你帮我指导了正确的方向。例如,如果没有您的建议,我就无法找到问题来查看Java控制台。谢谢!

〜录音棚

来源:

Java applet game has not granted permission to read images