简体   繁体   English

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

[英]Images on Java Applet not being displayed when run on a website; why?

I am writing a Java Applet. 我正在编写Java Applet。 I am using images to display content on the Applet window. 我正在使用图像在Applet窗口上显示内容。 I have a separate folder in which all of these images are contained. 我有一个单独的文件夹,其中包含所有这些图像。 This folder is not in the src or bin folder but a separate folder. 该文件夹不在src或bin文件夹中,而是一个单独的文件夹。 This folder is called 'assets'. 此文件夹称为“资产”。 Inside this folder, there is a folder called 'images'. 在此文件夹中,有一个名为“ images”的文件夹。 Inside this asset folder, I have a couple sub folders. 在此资产文件夹中,我有几个子文件夹。 In these sub folders, I have the images I am using. 在这些子文件夹中,我有正在使用的图像。 I use the methods listed below to load these images and display them onto the screen. 我使用下面列出的方法加载这些图像并将其显示在屏幕上。 The Applet runs fine in Eclipse. Applet在Eclipse中运行良好。 The problem occurs when I try putting the Applet onto a website. 当我尝试将Applet放到网站上时,会出现问题。 I have done quite a bit of searching, but of the things I tried, none of them solved my problem. 我已经做了很多搜索,但是在我尝试过的所有事情中,没有一个解决了我的问题。 I scan an image for certain pixel colors. 我扫描图像中的某些像素颜色。 My code I am using for the images is listed below. 下面列出了我用于图像的代码。 When I look at my .jar file, my images seem to be in the .jar file. 当我查看.jar​​文件时,我的图像似乎在.jar文件中。 (I used the Unarchived and JD-GUI to view the .jar file) Please let me know what I should do. (我使用Unarchived和JD-GUI查看.jar​​文件)请让我知道我该怎么做。

~Rane 〜雷恩

Method for drawing image onto the screen: 在屏幕上绘制图像的方法:

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

renderImage method: 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 method: 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;
}

My HTML: 我的HTML:

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

Two of the many answered that DIDN'T work for me: 许多回答中有两个对我没用:

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

Java Applet Images will not display when run in browser 在浏览器中运行时,Java Applet图像不会显示

I figured out my problem. 我发现了我的问题。 In my Applet, I was loading a game map by scanning an image for a certain pixel color value. 在我的Applet中,我通过扫描图像中某个像素颜色值来加载游戏地图。 On the website, the Applet was white. 在网站上,Applet是白色的。 In Eclipse, the screen appeared as I wanted it to. 在Eclipse中,屏幕出现了我想要的样子。 The reason for this is due to a security issue with Java Applets on webpages. 其原因是由于网页上的Java Applets的安全性问题。 It would throw a security exception every time it would try to scan the image because I was using a File. 由于我使用的是文件,因此每次尝试扫描图像时都会引发安全异常。 I needed to use a URL. 我需要使用一个URL。 I adjusted my code, and it works fine on the website now. 我调整了代码,现在在网站上运行良好。

I could not have done this without your help, MadProgrammer. MadProgrammer,如果没有您的帮助,我无法做到这一点。 You helped guide me into the right direction. 您帮助我指引了正确的方向。 For example, I couldn't have found the problem without your advice to view the Java Console. 例如,如果没有您的建议来查看Java控制台,我就找不到问题。 Thank You! 谢谢!

~Rane 〜雷恩

Sources: 资料来源:

Java applet game has not granted permission to read images Java applet游戏未授予读取图像的权限

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM