我正在努力让applet工作,但它拒绝了。这是applet的代码。
<html>
<head>
<title>GAME!</title>
</head>
<body>
<h1>GAME BY SID</h1>
<hr>
<applet code=GameManager.class width=240 height=300>
alt="Your browser understands the <APPLET> tag but isn't running the applet, for some reason."
Your browser is completely ignoring the <APPLET> tag!
</applet>
<hr>
</body>
</html>
以下是GameManager的代码
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package alienLanding;
import java.awt.*;
import java.applet.*;
import java.net.MalformedURLException;
import java.net.URL;
/**
*
* @author Admin
*/
public class GameManager extends Applet implements Runnable{
private int width;
private int height;
private Graphics offscreen;
private Thread animation;
private Image image;
private GunManager gm;
private UFOManager um;
private Image gunImage;
private Image ufoImages[];
private Image[] attackImages;
private Image[] explodeImages;
private AudioClip explosionSound;
private static final int REFRESH_RATE=80;
@Override
public void init(){
showStatus(">>Initializing<<");
width=bounds().width;
height=bounds().height;
setBackground(Color.black);
image=createImage(width,height);
offscreen=image.getGraphics();
loadImages();
um=new UFOManager(width,height,ufoImages,attackImages,explodeImages,this,explosionSound);
gm=new GunManager(width,height,gunImage,this,um.getUFOs());
um.intialize(gm);
}
private void loadImages(){
MediaTracker t=new MediaTracker(this);
try{
URL baseURL;
URL gunURL;
//G:\EBOOKS\Java Game Programming\CD\BOOK\chap06
baseURL=new URL("file:///C:/ShortenedURLProgramming/ch06/");
gunURL=new URL(baseURL,"image/gun.gif");
gunImage=getImage(gunURL);
t.addImage(gunImage,0);
ufoImages=new Image[6];
URL UFOImageURL;
for(int i=0;i<ufoImages.length;++i){
UFOImageURL=new URL(baseURL,"image/ufo"+i+".gif");
ufoImages[i]=getImage(UFOImageURL);
t.addImage(ufoImages[i],0);
}
attackImages=new Image[6];
URL attackImageURL;
for(int i=0;i<attackImages.length;++i){
attackImageURL=new URL(baseURL,"image/attack"+i+".gif");
attackImages[i]=getImage(attackImageURL);
t.addImage(attackImages[i],0);
}
explodeImages=new Image[4];
URL explodeImageURL;
for(int i=0;i<explodeImages.length;++i){
explodeImageURL=new URL(baseURL,"image/explode"+i+".gif");
explodeImages[i]=getImage(explodeImageURL);
t.addImage(explodeImages[i],0);
}
URL explosionSoundURL=new URL(baseURL,"Explosion.au");
try{
explosionSound=getAudioClip(explosionSoundURL);
}catch(Exception e){
System.err.println("Could not load sounds.");
}
}catch(MalformedURLException e){
System.err.println("Incorrect URL. Could not load images, and / or explosion sound.");
System.exit(1);
}
try{
t.waitForAll();
}catch(InterruptedException e){
}
if(t.isErrorAny()){
System.err.println(">>Media tracker error.<<");
// System.exit(1);
}else if(t.checkAll()){
showStatus(">>Successfully loaded images.<<");
}
}
@Override
public boolean mouseMove(Event e,int x,int y){
gm.moveGun(x);
return true;
}
@Override
public boolean mouseDrag(Event e,int x,int y){
gm.moveGun(x);
return true;
}
@Override
public boolean mouseUp(Event e,int x,int y){
gm.fireMissile(x);
return true;
}
@Override
public void update(Graphics g){
paint(g);
}
private void updateManagers(){
gm.update();
um.update();
}
@Override
public void paint(Graphics g){
offscreen.setColor(Color.black);
offscreen.fillRect(0,0,width,height);
gm.paint(offscreen);
um.paint(offscreen);
g.drawImage(image,0,0,this);
}
@Override
public void start(){
showStatus(">>Game starting<<");
animation=new Thread(this);
if(animation!=null){
animation.start();
}
}
@Override
public void stop(){
showStatus("Game stopped");
if(animation!=null){
animation.stop();
animation=null;
}
}
@Override
public void run(){
while(true){
repaint();
updateManagers();
Thread.currentThread().yield();
try{
Thread.sleep(REFRESH_RATE);
}catch(InterruptedException e){}
}
}
}
我已将applet代码粘贴到包含GameManager.class的目录中,但无济于事。我正在使用netbeans。
我很乐意提供更多信息。
请帮助我解决这个问题。
如果我在netbeans中提供“运行文件”,它就可以了。
答案 0 :(得分:1)
更改此
code=GameManager.class
到
code=alienLanding.GameManager.class
确保类文件位于名为alienLanding
的子文件夹中以匹配包。