Survey
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
Java Applets
! An applet is a Java program that can be embedded into
Introduction to Java Development
CS234
Week 1 Lecture 2
HTML much in the same way an image is included in a page
! The applet is sent over the internet and interpreted on a
client machine
! In HTML5, the <object> tag is used to embed an applet in a
web page
! Not all browsers current support the <object> tag, Firefox is
HTML5 compliant in this way
! Even supported browsers must have the correct plug-ins and
configuration to allow applets to run
Example
Our First Applet
! Embedding an applet using the <object> tag
// We grab the awt and applet class libraries – these are documented in the Java API
import java.awt.*;
import java.applet.*;
<object codetype="application/java"
classid="java:HelloWorldApplet.class”
width="500"
height="500">
</object>
This code assumes the applet is in the same directory as the HTML
page.
Additional details:
http://www.w3.org/TR/REC-html40/struct/objects.html#edef-OBJECT
// All applets must be public classes that extend the Applet class
// Note that the name of the class must match the name of the file
// All types of java files must also be named with the .java extension
public class HelloWorldApplet extends Applet {
// We override the paint method of java.applet.Applet to show our message
public void paint(Graphics g) {
g.drawString("Hello World!", 50, 25);
}
}
Creating our Applet
Compiling our Applet
! We will use Dr. Java to create our applet
! In Dr. Java, we use the compile button to compile our applet
! We may wish to configure Dr. Java under preferences
! Alternately, we may do this at the command line:
! Add Line Numbers
! javac HelloWorldApplet.java
! Either way, we rely on the Java SDK that is installed on your
machine to compile the file
! The compiler reads class and interface definitions written in
java and creates bytecode class files
! These mirror the names of the class but have a .class extension
! Ex: HelloWorldApplet.class
JVM
! The class files produced are platform independent
! Unique to Java
! The JVM then reads the bytecode and executes the
application i.e. the bytecode is a set of instructions for the
JVM to execute
! Originally interpreted, now most often JIT compiled
Compilation and execution
Running our Applet
! 1. Click the run button in Dr. Java
! 2. At the command line:
! appletviewer HelloWorldApplet.java
! 3. View the applet as embedded in our webpage
Java Applications
! Java applications are Java programs that are designed to stand
alone
! They may interact with the user through the console or through
a GUI
! Compilation is the same as for Java applets
! To run a Java program
! Select “run” in Dr. Java
! Enter “java HelloWorld” at the command line in the directory
containing your HelloWorld.class file
HelloWorld Java Application
// This is the definition of the HelloWorld class
// The class definition contains only the main method where
// execution begins
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello Again");
}
}