Arcade Village Blog

From applet to application (Part 1).

2019-04-02 Java programming
This article requires to know Java.

Applets are no longer supported by recent browsers. In most cases, the couple HTML + Javascript replaces them without difficulty.
But I wrote about 40 applet games in Java and starting from scratch was discouraging.
So I looked for a simple method to be able to offer them in the form of downloadable programs.
The idea was:
- to keep the language java
- modify the game program at minimum
- to be able to offer more than a dozen games quickly without spending too much time. I create these games on my free time... But how? And the passion? Well, passion is born from creation and redoing what I already did is not very exciting. On the other hand, finding a trick to propose the games interested me.

My example uses the Hebi game program. You can download AVJukeBox to test it.

A Java applet looks like this (I deliberately deleted the useless code for the demonstration):

public class Hebi extends Applet implements GameListener, ActionListener
{
avub ub; // This class allows the save of the best score in arcade village
Label lmsg;
HebiCanvas hc = null;
HebiDecors d = null;
OneImgProvider imgdico = null;
int phase;
StatusBar sp;
Color c = Color.blue;
Color ink = Color.yellow;
Button b;
AppletCookieMgr ck=null;

public void init()
{
hc = new HebiCanvas(); // All my game use the same Canvas model

String s = getParameter("bg");
if ( s != null )
{
try
{
c = new Color( Integer.parseInt(s,16) );
}
catch( Exception e )
{
}
}
s = getParameter("ink");
if ( s != null )
{
try
{
ink = new Color( Integer.parseInt(s,16) );
}
catch( Exception e )
{
}
}
ck = new AppletCookieMgr(this.getDocumentBase());
try
{
ck.load();
}
catch( Exception e )
{
}
setBackground(c);
setForeground(ink);



//===========================================================

java code for adding components (button, panels, ect ...)

//===========================================================


if ( !loadData() ) return;

this.addKeyListener( hc );
hc.setGameListener(this);

add( "Center", hc );
onNewGame();
doLayout();
validate();
}

public void stop()
{
hc.stopThread();
}

public boolean loadData()
{

//===========================================================

Java code where images and other files of the game are loaded.

//===========================================================
}

//
// Hebi contains a canvas for the game, a panel for the score and a panel for the game buttons
//

public void onNewGame()
{
sp.setScore(0);
sp.setLife(3);
setLevel(0);
repaint();
}


//===========================================================

..... Still a little java code without interest for the demonstration

//===========================================================

}

The first step is to create a program that wraps this applet. This is very simple because the applet is a component and can be included in a Frame.
Here is the code:

{
package avjukebox;
import javax.swing.JFrame;
public class avjukebox
{
static JFrame fenetre;


public static void main(String[] args)
{
fenetre = new JBMainFrame();
fenetre.addWindowListener(new JBWindowAdapter(fenetre));
fenetre.setLocationRelativeTo(null);
fenetre.setVisible(true);
fenetre.doLayout();
}
}

}

Then, the JBMainFrame object that calls the Java applet

public JBAppletPanel(...)

dd = new CCApplet();
....
this.add( dd); // Ajoute l'applet
doLayout(); // Force ce panel à se dessiner
dd.init(); // Appel init de l'applet
validate();

This step was not enough because applets retrieve data from the HTML page and it was necessary to create a new Applet object.
I will present this object in the next article.
ArcadeVillage.com 1999 - 2024