简体   繁体   中英

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

I am writing a Java Applet. I am using images to display content on the Applet window. 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. This folder is called 'assets'. Inside this folder, there is a folder called '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. The problem occurs when I try putting the Applet onto a website. 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. (I used the Unarchived and JD-GUI to view the .jar file) Please let me know what I should do.

~Rane

Method for drawing image onto the screen:

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

renderImage method:

    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:

    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:

    <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

Java Applet Images will not display when run in browser

I figured out my problem. In my Applet, I was loading a game map by scanning an image for a certain pixel color value. On the website, the Applet was white. In Eclipse, the screen appeared as I wanted it to. The reason for this is due to a security issue with Java Applets on webpages. 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. I adjusted my code, and it works fine on the website now.

I could not have done this without your help, 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. Thank You!

~Rane

Sources:

Java applet game has not granted permission to read images

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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