Meaningless Drivel is fun!*
The moose likes Applets and the fly likes How Do I Turn My Java Application into a JApplet and keep it as an Java Application too Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Applets
Bookmark "How Do I Turn My Java Application into a JApplet and keep it as an Java Application too" Watch "How Do I Turn My Java Application into a JApplet and keep it as an Java Application too" New topic
Author

How Do I Turn My Java Application into a JApplet and keep it as an Java Application too

Ana Suvari
Ranch Hand

Joined: Aug 18, 2009
Posts: 43
The assign was a game called FortuneCity
The prof wanted us to follow the MODEL - VIEW - CONTROLLER implemention.
The MODEL contains only functions that can be used in the game. It has NO GUI stuff in it.
The VIEW contains only contains the GUI interface.
The CONTROLLER allows Model and View to interact.

He gave us the model class which was called FortuneCity.java but he expected us to create the View and Controller part. So i did.

Inside his MODEL, the FortuneCity class

public class FortuneCity
{

private FortuneCityView view; //that's the GUI view

private Player [ ] players = new Player[3];
...
...
...etc.
public FortuneCity() //constructor
{
view = new FortuneCityView(this); //passes the MODEL into the VIEW
....
....
....etc.


}
//methods in MODEL (NO GUI STUFF)
...
...
...etc.

public static void main(String[] args)
{
FortuneCity game = new FortuneCity();
JFrame frame = game.view;
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(1200,700);
frame.setVisible(true);
}
}

//FortuneView - I wrote this myself and includes CONTROLLER inside (ActionPerformed stuff)
public class FortuneCityView extends JFrame implements ActionListener, KeyListener {
private FortuneCity game;
JLabel heading;
....
....
....etc.
public FortuneCityView(FortuneCity w) { //I pass the MODEL object in it.
game = w; // I refer to the MODEL and call it game

//methods in FortuneView
//I use MODEL methods by using game.[something] ex. game.startARound()
...
...
...etc.
}

The above works as an APPLICATION.

But I need to turn it into AN APPLET AND APPLICATION.

I tried the following

#1. I extends JApplet in the FortuneCity (MODEL class):

public class FortuneCity extends JApplet
{
private FortuneCityView view; //that's the GUI view

private Player [ ] players = new Player[3];
...
...
...etc.

#2. I change FortuneCity constructor into an init() class
Thus public FortuneCity()

becomes public void init()

#3. I change the main to get the applet and put into the frame:

public static void main(String [ ] args)
{
JFrame frame - new JFrame("Fortune City");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(1200,700);

FortuneCity applet = new FortuneCIty() //create applet
applet.init(); //call init
frame.setVisible(true);
}

The result is that I get a blank frame when I compile and run it.

But I'm not sure if i am suppose to be adding the applet.view. But I tried frame.add(applet.view) and I get an run time error message on that if i do.

Please help me.

I also tried converting it to an APPLET ONLY by removing the main() but when i call the applet from an .html file, it says applet not initialized in the
browser.

I am new to this JAVA. I hope someone will be patient with me. The code works fine as an application until i try to change it to work for both an applet and application OR if i change it to be an APPLET only.
Fred Hamilton
Ranch Hand

Joined: May 13, 2009
Posts: 679
I'm not sure this will help you given the work you have already done, or the conditions imposed by the prof, but as much as possible in the development phase I try to put my entire GUI on a JPanel, so that my high level container class ( extends JFrame or JApplet) is just a shell. That way I can easily drop the JPanel into a JFrame or a JApplet without modification. So before the application including GUI is written I know the panel will load in a JFrame and a JApplet, then I don't touch my container classes after that, unless I want my desktop version to have functionality that won't work in an applet anyway.

But like I said, in this case it might not help you, but something to think about when you design your own applications.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: How Do I Turn My Java Application into a JApplet and keep it as an Java Application too
 
Similar Threads
designing for multiple layers
Start Frame In Applet
tree in frame, frame in applet
Need SERIOUS HELP!!
applet size