marshby
marshby

Reputation: 23

Load images on an applet HTML

I'm trying to make a simple game that only renders 4 images on screen randomly, but I want to put try it on a website on an HTML. When I test on Eclipse it works just fine, but when I put it on an html and upload it to the websites it tells me: access denied ("java.io.FilePermission" "Sheet.png" "read"), I know I have to put getResourceAsStream("Sheet.png");

but it just doesn't work Please help!

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;
import java.util.Random;
import javax.imageio.ImageIO;

public class Game extends Canvas implements Runnable{

public BufferedImage icons = null;
public BufferedImage wall = null;
public Random r = new Random();
public boolean running;
private InputStream input;

public Game(){

    setBackground(Color.white);
    setSize(640, 320);

    input = Game.class.getResourceAsStream("Sheet.png");

    start();
    this.setSize(new Dimension(640, 320));

    try {
        icons = ImageIO.read(input);
    } catch (IOException e) {
        System.out.println(e.getMessage());
    }

    wall = icons.getSubimage(0, 0, 16, 16);
}

public static void main(String args[]){
    new Apple().init();
}

public void start(){
    running = true;
    new Thread(this).start();
}
public void stop(){
    running = false;
}

public void run() {
    while(running){
        try {
            new Thread().sleep(200);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        repaint();
    }
}
    int rendered;
public void paint(Graphics g){
    g.drawImage(wall, r.nextInt(600), r.nextInt(280),null);
    g.drawImage(wall, r.nextInt(600), r.nextInt(280),null);
    g.drawImage(wall, r.nextInt(600), r.nextInt(280),null);
    g.drawImage(wall, r.nextInt(600), r.nextInt(280),null);
    rendered++;
    g.drawString("Rendered: "+rendered, 0, 290);
}
}

Applet class:

import java.awt.Color;
import java.awt.Dimension;
import javax.swing.JApplet;

public class Apple extends JApplet{

public void init(){
    this.start();
    this.setBackground(Color.WHITE);
    this.setEnabled(true);
    this.setMinimumSize(new Dimension(640, 320));
    this.setMaximumSize(new Dimension(640, 320));
    this.setSize(new Dimension(640, 320));
    this.add(new Game());
}

}

HTML:

<html><body>
<p>
<applet code="Apple.class" archive="Applet.jar"
width="640" height="320"></applet>
</p>
</body></html>

Upvotes: 2

Views: 192

Answers (1)

Andrew Thompson
Andrew Thompson

Reputation: 168815

I have some 'bad news' for you. The applet works just fine here.

Working applet

This suggests that the problems you see are the result of caching of older classes. Ensure the Java Console is open and flush the cache before reloading the page.

Upvotes: 1

Related Questions