2012-12-14 95 views
2

我試圖做一個簡單的遊戲,只在屏幕上隨機呈現4個圖像,但我想把它放在HTML上的網站上。當我在Eclipse上測試時,它工作得很好,但是當我把它放在html上並將其上傳到網站時,它告訴我:access denied(「java.io.FilePermission」「Sheet.png」「read」),我知道我必須把getResourceAsStream(「Sheet.png」);在applet上加載圖像HTML

但它只是不起作用請幫忙!

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類:

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> 
+0

1)*「當我把它放在html上並上傳到網站時」*我們可以訪問applet的URL是什麼? 2)'width =「640」height =「320」'在HTML中是好的,但刪除所有其他對設置大小的引用 - 它們是不必要的和不太有用的。 3)不要在init()方法中調用'start()'。這些方法只能由JVM調用。 4)不要混合Swing和AWT組件,在這種情況下,用'JPanel'替換'Canvas'。 5)用'e.printStackTrace()'替換'System.out.println(e.getMessage());''6)'g.drawImage(wall,.. nextInt(280),this);' –

+0

我改變了全部你告訴我,但它仍然告訴我同樣的錯誤,但在Eclipse中它工作正常。網址是:http://dl.dropbox.com/u/53271378/Applet/untitled%20text.html – marshby

回答

1

我對你有一些 '壞消息'。這個小程序在這裏工作得很好。

Working applet

這表明,你所看到的問題是老年人類的高速緩存的結果。請確保Java Console處於打開狀態並刷新緩存,然後重新加載頁面。

+0

非常感謝,我希望如果遇到更多問題,我會有更多的「壞消息」,非常感謝: ) – marshby