How To Convert Your Applet To An Application
<< How To Put A Running Version Of Your Project On The Web | FinalProjectsTrailIndex | How to make a Web Launched Application >>
Steps
- Make a New Project, say "MyGameApp" which will be made from the "MyGame" Applet
- Copy the classes from your Applet and paste them in your new Application Project
- Copy any sound or image files into your new project's folder
- Change
extends Applet
toextends Frame
- You might need these import statements:
import java.awt.Frame; import java.awt.event.WindowEvent; import java.awt.event.WindowListener;
- Convert the init() method into a constructor method by changing
public void init()
topublic MyGame
- Make the Title Bar by making the first line of your constructor method
super("My Awesome Game");
- Add to your new constructor method the size you desire:
setSize(500,500);
- Add a window listener in the constructor method so you can exit your game by closing the window:
this.addWindowListener(new WindowListener() { public void windowClosing(WindowEvent e) {System.exit(0);} public void windowClosed(WindowEvent e) {} public void windowOpened(WindowEvent e) {} public void windowIconified(WindowEvent e) {} public void windowDeiconified(WindowEvent e) {} public void windowActivated(WindowEvent e) {} public void windowDeactivated(WindowEvent e) {} });
- Make a main method
public static void main(String[] args) { MyGameApp app= new MyGame(); app.setVisible(true); }
- Compile and test out your application
- From the "Project" menu select "Make jar file" and select the main method you just wrote.
This file is double-clickable from the operating system.
For Macs only:
If you install XCode in OSX, look in Developer/Applications/Utilities for the Jar Bundler application. This will allow you to pick a pretty icon for your java app, and add a MacMenu (which includes Command-Q to Quit, and "About..." Menu items) which gives you a more "Mac look" to your application.
AudioClips
Unfortunately, since our application is a subclass of Frame and not Applet, we lose the Applet class' handy way of playing audio. The
good news is we can still use the static methods in the Applet class to play audio. The trick is to use the static method newAudioClip()
.
We need to add two imports:
import java.net.URL; import java.applet.*;
and make our own getAudioClip()
Method:
private AudioClip getAudioClip(String filename) { return Applet.newAudioClip(getClass().getResource(filename)); }
So now you can do things similar to before:
AudioClip g = getAudioClip("vG.wav"); g.play();
Image Buffers
If in the world of the applet, you have in your init()
method somthing like
virtualMem = this.createImage(this.getSize().width, this.getSize().height); gBuffer = virtualMem.getGraphics();
you might run into problems. The Image virtualMem
will be null in the constructor method. To fix this, you need to have a conditional statement in your paint
method so that if the image is null, you will create one:
if (virtualMem==null){ virtualMem = this.createImage(this.getSize().width, this.getSize().height); gBuffer = virtualMem.getGraphics(); }
Buttons
By default, Applets use a FlowLayout. Since our application is a subclass of Frame and not Applet, we need to explicitly define the FlowLayout before we add any buttons. This would be in the new constructor method:
setLayout(new FlowLayout()); // now we can add(myButton);
If we don't do this, our entire Frame would be a massive Button!
BlueJ may not make a manifest identifying the Main-Class
A manifast file is a text file that looks something like this
Manifest-Version: 1.0 Main-Class: Main Created-By: 1.5.0_16
and sometimes you have to edit the file yourself and remake the .jar file from the command line.