Java Applets and Applications

Java Applets and Applications
Java programs can take two forms, they can be a standalone application or they can be an applet – a (usually) small program that runs within a web browser. When Java first came out, applets were probably the most popular type of Java program because they were the best, if not only way, to add interactivity, animation, and useful functionality to web pages. Now there are many good ways to provide these things and programmers (and managers!) have become more aware of the benefits of Java as a general purpose programming language and applications get all the press. No matter what your interest in Java, it is useful to understand a little bit about both types of Java programs.

Below is an example of a simple (and pretty much useless) application, apply named SimpleApplication, that prints a single line of text. A hallmark of applications is that they always have a main method.

class SimpleApplication {
public static void main(String[] args) {
// Print the line of text
System.out.println("I am a very boring application.");
}
}

This results in the following application:
I am a very boring application.

Below is an example of a very simple applet, unsurprisingly named SimpleApplet. It too prints a single line of text, this time to the applet pane in a web browser. Applets do not have a main method and they need to import either the java.applet.Applet or the javax.swing.JApplet class. (The later is needed if you use swing components.)

import java.applet.Applet;
import java.awt.Graphics;

public class SimpleApplet extends Applet {
public void init() {
resize(150,25);
}
public void paint(Graphics g) {
g.drawString("I am a very boring applet", 50, 25);
}
}

The applet is then included in a web page using the object tag, for this applet. For instance:

<applet code="SimpleApplet.class" width="150" height="40">
Simple Java Applet
</applet>

You may also see the applet tag, however, be aware that it is depreciated. The equivalent applet tag would be:

<object codetype="application/java"
classid="java:SimpleApplet.class"
width="150" height="40" >
Simple Java Applet
</object>

You can see this applet in action and download the source code for both the applet and the application here.
Need ready to use applets for your web pages? Instant Java is your answer. Order from Amazon.com or Alibris.


This site needs an editor - click to learn more!


You Should Also Read:
Beginners Resources
Java for Programmers
Web Page Add-Ons

RSS
Related Articles
Editor's Picks Articles
Top Ten Articles
Previous Features
Site Map





Content copyright © 2023 by Julie L Baumler. All rights reserved.
This content was written by Julie L Baumler. If you wish to use this content in any manner, you need written permission. Contact BellaOnline Administration for details.