0

I have made a game using Java, my game is a state based game, so it extends StateBasedGame, I want to put the game on a website which will require me to make it an applet (unless there is another way) so it must also extend JApplet, after trying to extend multiple class's and reading online I had no luck and read on a forum post that it is not possible to have multiple extends. Now does this mean that I can not put my game on a website?

Here is my main class so far, it extends StateBasedGame:

    package javagame;

import org.newdawn.slick.*;
import org.newdawn.slick.state.*;

public class Game extends StateBasedGame{

    public static final String gamename = "Croft";
    public static final int menu = 0;
    public static final int play = 1;

    public Game(String gamename){//create window on statup
        super(gamename);//adds title to screen
        this.addState(new Menu(menu));//"this" means get from this class
        this.addState(new Play(play));
    }

    public void initStatesList(GameContainer gc) throws SlickException{
//we need this because we inhereted from StateBasedGame, gc manages behind the scene stuff
        this.getState(menu).init(gc, this);//telling java what states we have
        this.getState(play).init(gc, this);
        this.enterState(menu);//tells java that you want to show menu before play
    }

    public static void main(String[] args) {
        AppGameContainer appgc;//the window for your game
        try{
            appgc = new AppGameContainer(new Game(gamename));//window holding the Game
            appgc.setDisplayMode(640, 360,false);//size, sizetrue would make it full screen //640,360
            appgc.start();//creates the window
        }catch(SlickException e){//built into slick for error handelling
        e.printStackTrace();}
    }

}

EDIT1:

html code:

    <applet code="org.lwjgl.util.applet.AppletLoader" 
        archive="lwjgl_util_applet.jar" 
        codebase="." 
        width="640" height="480">

  <param name="al_title" value="Ham Blaster"> 
  <param name="al_main" value="org.newdawn.slick.AppletGameContainer"> 
  <param name="game" value="org.javagame.Game">

  <param name="al_jars" value="slick.jar, lwjgl.jar, slick.jar"> 

  <param name="al_windows" value="windows_natives.jar"> 
  <param name="al_linux" value="linux_natives.jar"> 
  <param name="al_mac" value="macosx_natives.jar"> 

  <param name="separate_jvm" value="true"> 
</applet> 

My jar file name is racegame, my main class is Game, my package name is javagame.

Error: ClassNotFoundException org.lwjgl.util.applet.AppletLoader

Rahul Khosla
  • 349
  • 9
  • 21

4 Answers4

6

Java does not support multiple inheritance. For typing problems, use interfaces. But in this case you're more interested in behavior, so I'd use composition.

public class GameApplet extends JApplet {
  private Game game = new Game();

  public void init() {
    game.foo();
    ...
  }

  ...
}
tsm
  • 3,598
  • 2
  • 21
  • 35
4

You cannot have a class extend from more than 1 class. There are ways to get what you want though, the easiest of which might be to have another class that extends JApplet, and have that class use your game class. This is called composition -- it's when a class uses instances of one or more other classes.

Typically if you want to extend from more than 1 source, you would use interfaces, and implement from multiple sources. However, this is not the same, since using interfaces doesn't actually provide functionality; it only defines behavior, i.e. specifies which methods the implementing class must implement.

hvgotcodes
  • 118,147
  • 33
  • 203
  • 236
2

If I'm not wrong, that StateBasedGame class comes from the Slick 2D API. You should look for information about making applets with Slick 2D, such as this post.

madth3
  • 7,275
  • 12
  • 50
  • 74
  • Thank you for the link and answering my question, I have done as the link said and used the code but when I try run my java game I get the following error: ClassNotFoundException org.lwjgl.util.applet.AppletLoader, could you please help me solve this problem? I have added my html code and information about my app in my original post. – Rahul Khosla Jan 07 '13 at 23:43
  • You're probably missing a Jar file. Did you check the link about deploying? http://ninjacave.com/appletloader – madth3 Jan 07 '13 at 23:47
  • Thank you for the fast reply and the link, I read trough it and understand now what AppletLoader actually is but got a bit confused at where it started explaining signing, I downloaded lwjgl from here http://lwjgl.org/download.php would I have to worry about signing? Also, I am a bit confused because I dont have any more jar files, I made one for my game and the others are the same as the ones in the tutorial, do newer versions of lwjgl require more jar files, if so, how would I find this jar file? – Rahul Khosla Jan 08 '13 at 00:03
0

You need to encapsulate this class within an object which could be bootstrapped from within an Applet.

That said, Java does not allow extending multiple classes, so what you read is correct.

dardo
  • 4,870
  • 4
  • 35
  • 52