Applet to Application

Hello, so after about a month of work I have created my first Java game on my own. It is a 2D turn based strategy, don’t laugh too hard when you see it. Right now though it is run through an applet, but I wish to move it to a application before I continue with development.

It’s a lot of code so I’ve posted the source below if you care to take a look at it. I’m essentially looking to have the bottom 1/4 of the screen with all the info be static, while you could scroll around the actual game window. I know this might seem quite ambiguous, but if you run the applet I think you’ll get what I’m saying. Also, willing to post pictures if needed.

Thanks!

You can create an instance of the class that extends Applet and add it to a JFrame. However, the init(), start(), stop(), and destroy() methods are not called so you will have to manually call them after you add it :slight_smile:


JFrame frame = new JFrame("My Game");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(WIDTH,HEIGHT);

final MyApplet applet = new MyApplet();
frame.add(applet);

frame.setVisible(true);

applet.init();
applet.start();

frame.addWindowListener(new WindowAdapter() {
    public void windowClosing(WindowEvent we) {
        applet.stop();
        applet.destroy();
    }
});

Note: if you only use init(), then you don’t need to call the rest :slight_smile:

Ok did that, but I seem to be getting null pointer errors from a method that declares all my images and audio files:


public void declareImages() {
		tr = new MediaTracker(this);
		
		tank_fire = getAudioClip(getCodeBase(), "tank_fire.wav");
		tank_destroyed = getAudioClip(getCodeBase(), "tank_destroyed.wav");
		
		plains1 = getImage(getCodeBase(), "plains.png");
		tr.addImage(plains1, 0);
		tiles[0] = plains1;

                //similar code below
}

EDIT: first of many errors


Exception in thread "main" java.lang.NullPointerException
	at java.applet.Applet.getCodeBase(Unknown Source)
	at com.rolledback.game.Main.declareImages(Main.java:103)
	at com.rolledback.game.Main.init(Main.java:62)
	at com.rolledback.game.Frame.main(Frame.java:20)

Ah yes getCodeBase() won’t work. Use System.getProperty(“user.dir”) instead. :slight_smile:

???

plains1 = getImage(System.getProperty("user.dir"), "plains.png");
The method getImage(URL, String) in the type Applet is not applicable for the arguments (String, String)

PS: thank you for such patience

Ah, then just do this: getImage(.class.getResource(“plains.png”));

getResource returns a URL and searches for the file relative to the class.

Bleh, also does not work, it may have to do with how I’ve organized my images.

plains1 = getImage(Main.class.getResource("plains.png"));

http://gyazo.com/ed98477ac89861ff82e6c7cbef98e49a

Just put them in packages in your src folder, like this or something like this:


src
|--> code packages
|--> images
     |--> tiles
     |--> sprites
|--> sounds

Did not solve the issue…could you maybe show me what you want with a screenshot, just in case I did not do what you meant?

:emo:

Well it is best to use javax.imageio.ImageIO instead of createImage, but either way, you’ll need to do:


BufferedImage image = ImageIO.read(Main.class.getClassLoader().getResource("tiles/plains.png"));

Class.getClassLoader().getResource(…) returns a URL relative to the root of your project.

EDIT: It looks like you’ve added “tiles”, “original_models”, “team1_models”, “team2_modes” as source folders. Don’t do that, change them to normal folders under “src”.

This is how I do it. All of the code is in “zman.example” and it’s sub directories, and all the resources go in “resources” and it’s sub directories. I just have eclipse treat them as packages in the src folder.

http://i1220.photobucket.com/albums/dd457/ZMan/example.png

I LOVE YOU GUYS!

It took a bit of messing around, but the end result was this declaration:

plains1 = ImageIO.read(Main.class.getClassLoader().getResource("plains.png"));

I guess I didn’t have to reference the folder the image was in. Applet runs great. Now I just need to separate the 2 parts of the game screen.

That looks neat.

Did it take you long?

The project was started about a month and 2 weeks ago. All the artwork you see is original. I’m a senior in high school, so I’m pretty impressed on what I’ve been able to do with only 2 years of formal CS/Java and about a year of self taught under my belt. There is no animation though. I’ll see if I can export it to an .exe and I’ll put it on mediafire.

Here’s the jar file for anyone who wants to play around with this, comments and suggestions are appreciated. :slight_smile:

Bleh, having bugs with the sounds. I’ll eventually reexport it. Too lazy though. So any ideas on how I could go about separating the dashboard looking thing from the map? I want to be able to have unlimited map sizes.

Where’s the JAR? :wink:

Just a quick question! Are those trees in the water?

Ok fine, I’ll reupload it. The sounds have been removed since I can’t figure out how to declare them in a way that won’t give me errors and exceptions. I’ll figure that out tomorrow. ;D

http://www.mediafire.com/?97kb0wayagm00rq

No they are islands. MS Paint is only so amazing.

Oh, I was talking about the image that you posted earlier. It looks like you’re painting your water (With a slight transparency) on top of various other ground types (Plains, Plains with big, fluffy trees, Plains with conifers.)

Oh no. That is showing you where you can move your unit. And since this thread is starting to move onto the general development of this game, I feel like I should start a new more permanent thread. What forum should I put it in?