Arcade Village Blog

From applet to application (Part 2) : getParameter

2019-05-23 Java programming
In the previous article, we saw what an applet looked like and how to encapsulate it in a Frame to make an application of it.
The applets were executed in browsers and retrieved information from the HTML code. We must simulate this ability.
To retrieve the parameters, the Applet object has a function called getParameter.

In the case of Hebi, the HTML code is as follows:

<APPLET CODE="Hebi.class" MAYSCRIPT ARCHIVE="hebi.jar" WIDTH="308" HEIGHT="350">
<param name="il" value="1">
<param name="ig" value="1">
<param name="bg" value="ffce63">
<param name="ink" value="000000">

The parameters provided are:
- it : code of the language used (ArcadeVillage "speaks" English, French and Japanese)
- ig : a code for the game
- bg : the background color
- ink : the color of the ink.

In the first article, we saw in Hebi's code the following lines:

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 )
{
}

An application is not linked to a html page, I decided to use the class Properties which offers the ease of being able to be loaded from an XML file.
The model of the XML file is as follows:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<entry key="url.documentbase">games/hebi/</entry>
<entry key="applet.width">310</entry>
<entry key="applet.height">360</entry>
<entry key="il">2</entry>
<entry key="ig">1</entry>
<entry key="fl">1</entry>
<entry key="bg">ffce63</entry>
<entry key="ink">000000</entry>
</properties>

This format is recognized by the Properties class that I use to retrieve the parameter values.
My Applet class will contain the code :

public class CCApplet extends Panel
{
private Properties params = new Properties();
..

public String getParameter( String key )
{
return params.getProperty(key);

}
public void setParameter( String key, String value )
{
params.setProperty(key,value);

}

public void loadParamsFromXML( String appfile )
{
InputStream f = null;
try
{
f = new FileInputStream(appfile);
// load a properties file
params.loadFromXML(f);
f.close();
}
catch (Exception ex)
{
ex.printStackTrace();
}
}

The application must contain a call to the loadParamsFromXML function at the beginning of the program to load the parameters.

Now, the getParameter function return the same values ​​as if we were in an applet called from an HTML page.
The smartest of you will have noted the following lines in the XML file:

<entry key="url.documentbase">games/hebi/</entry>
<entry key="applet.width">310</entry>
<entry key="applet.height">360</entry>

I'm also using this file to simulate the getDocumentBase() function.
The implementation of this one, as well as getCodeBase(), will be explained in the next article.
ArcadeVillage.com 1999 - 2024