2

我正在尝试制作一个仅在屏幕上随机呈现 4 张图像的简单游戏,但我想将它放在 HTML 上的网站上。当我在 Eclipse 上测试时它工作得很好,但是当我把它放在一个 html 上并将它上传到网站时它告诉我:访问被拒绝(“java.io.FilePermission”“Sheet.png”“read”),我知道我必须把 getResourceAsStream("Sheet.png");

但它只是不起作用请帮助!

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);
}
}

小程序类:

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>
4

1 回答 1

1

我有一些“坏消息”要告诉你。小程序在这里工作得很好。

工作小程序

这表明您看到的问题是缓存旧类的结果。确保Java 控制台已打开并在重新加载页面之前刷新缓存。

于 2012-12-14T03:21:06.673 回答