简体   繁体   English

无法运行NoClassDefFoundError的Java Applet游戏

[英]Can't get my java applet game running NoClassDefFoundError

I'm trying to get an applet to work but it refuses to. 我正在尝试使applet正常工作,但它拒绝。 Here's the code for the 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 &lt;APPLET&gt; tag but isn't running the applet, for some reason."
    Your browser is completely ignoring the &lt;APPLET&gt; tag!
      </applet>
      <hr>
  </body>
</html>

Here is the code for GameManager 这是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){}
        }
    }
}

I have pasted the applet code in the directory that contains GameManager.class but to no avail. 我已将applet代码粘贴到包含GameManager.class的目录中,但无济于事。 I am using netbeans. 我正在使用netbeans。

I will be glad to provide more information. 我很乐意提供更多信息。

Please help me to get this to work. 请帮助我使它工作。

It is working if I give "run file" in netbeans. 如果我在netbeans中提供“运行文件”,则它正在工作。

Change this 改变这个

code=GameManager.class

to

code=alienLanding.GameManager.class

And make sure the class file is in a sub-folder called alienLanding to match the package. 并确保该类文件位于名为alienLanding的子文件夹中,以匹配该包。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM