You can make an applet. An applet is a Java program that can be run from within a Web browser (on a system that runs the Java Runtime Engine [JRE] that is, which, as of this writing, the iPhone doesn't).
Pretty much anything you can do with the Java language you can do within an Applet. Both use the Java Virtual Machine (JVM). Though setting up the program to run as an Applet is a wee bit different than setting up as a stand-alone program.
Once, embedding an Applet on a Web page was easy. You'd simply use the <Applet> tag. That W3C has deprecated that tag, which means you shouldn't use it any longer (Though it still works in most all browsers).
Instead, you have to use either the <embed> tag for Mozilla-based browsers, and the <object> tag for Internet Explorer browsers. And, realistically, if you're planning to have any visitors at all, you should use both.
(Before we begin, you should test to see if you have Java properly installed on your machine, go here.)
<OBJECT classid="clsid:8AD9C840-044E-11D1-B3E9-00805F499D93"> <PARAM name="code" value="[NAME OF THE YOUR PROGRAM].class"> </OBJECT>The markup above was derived from this Java documentation page.
The classid tells the browser what program (In this case, the JRE) on the user's computer should run the code being pointed to (in this case the class file). The above series of numbers tells the browser to use the latest version of the JRE, though you can also specify some specific version (see documentation). You can also use optional attributes for setting height, width or other aspects of the applet. See a list of attributes here.
<embed code="[NAME OF YOUR PROGRAM].class" type="application/x-java-applet;version=1.6"> </embed>Again there are additional options you can add in.
It is worth noting for the beginner that with both the embed tag and the object tag, supplying the correct plug-in name is crucial. In this instance, for instance, "version=1.6.0" would NOT work. If your Web page is not running the Applet by now, it may be due to you not using the correct name. Search the Web for answers!
<object classid="clsid:8AD9C840-044E-11D1-B3E9-00805F499D93"> <PARAM name="code" value="[NAME OF YOUR PROGRAM].class"> <comment> <embed code="[NAME OF YOUR PROGRAM].class" type="application/x-java-applet;version=1.6"> </embed> </comment>
<body> <applet code = "[NAME OF YOUR PROGRAM].class"> </applet>Please to note that all the description about the applet is actually in the opening tag. So you have to put a close bracket onto the end, and then you have to do a close applet ("</applet>") tag as well.
Within the Applet tag, you can also specify attributes like width ("width = [fill in value here]"), height ("height ="), vertical margin ("vspace =") horizontal margin ("hspace ="), alignment ("align =") and others. Go here for a full list of optional attributes.
Just like on the command line, what gets executed is a class file (a file with a .class extension), one that has been compiled from Java source code.
In the program code itself, there doesn't have to be a "main" method, for instance. Instead, this subclass is used:
import javax.swing.*; public class TestApplet2 extends JApplet { public void init() { add (new JLabel("Hello World", JLabel.CENTER)); } }After this, control of the applet is handed by "init" "start" "stop" and "destroy" control the applet.
For the user-interface, you can use some libraries, like Swing, but more on that later. If you don't wish to test your code through a browser each time out, you can also use the Java Applet Viewer, which is in the SDK, and can be evoked from the command line.