我正在尝试制作一个仅在屏幕上随机呈现 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>