http:// japplet
TRANSCRIPT
•http://www.hostitwise.com/java/japplet.html•http://download.oracle.com/javase/tutorial/uiswing/components/applet.html
JAPPLET
2
Applets and applications
An application is an “ordinary” programExamples: Notepad, MS Word, Firefox, Halo, etc.
An applet is a Java program that runs “within” another program (usually a browser)Applets can be run within any browserTo run Java applets, browsers need an up-to-date
Java pluginappletviewer is a program that can run applets
When you download the Java SDK, appletviewer comes with it
appletviewer is always up-to-date with your Java systemEclipse has an built-in applet viewer
AppletsApplets are applications that cannot run by
themselves. They run in the context of a browser, or
software such as an appletviewer, that provides the interface in which the applet will run.
From a programming point of view applet classes are extensions of the JApplet class, which is a panel that has four methods that can be overridden, namely init(), start(), stop(), and destroy().
4
Packages and classes
Java supplies a huge library of pre-written “code,” ready for you to use in your programs
Code is organized into classesClasses are grouped into packagesOne way to use this code is to import itYou can import a single class, or all the
classes in a packageFor this applet, you will need to import two
drawing packages, awt and swing
JApplet hierarchyJApplet hierarchy is as follows:
java.lang.Object java.awt.Component java.awt.Container java.awt.Panel java.applet.Applet javax.swing.JApplet
init() methodThe init() method is called once by a browser
when the web page first creates the applet.The method usually contains code to perform
basic setup tasks. If you do not provide this method in your applet
then the method in JApplet is run
start() methodThe start() method is always called whenever
the applet becomes visible.
It is called immediately after the execution of init() on the first occasion, and then subsequently when the applet reappears after scrolling or browsing, for example
stop() methodThe stop() method is always called by a browser
whenever the applet becomes invisible. This allows any applet code producing effects
such as animation to be stopped.
destroy() methodThe destroy() method is called by a browser at
some convenient point when it decides to remove the resources of the applet.
It thus allows the applet a last chance to clean up before it is removed.
Understanding the JApplet Life Cycle
Init()
start()
stop()
stop()
destroy()
paint() methodThis applet provides a paint() method that
draws on its panel.
This is called by the browser each time the panel's visible area is affected and is supplied with a Graphics object that facilitates drawing on its surface.
Because paint() overrides the superclass method, a call of super.paint() is advisable since it ensures that any other components of the superclass are painted
Some more methods
Applet Methodsinit Method
Initializes variablesGets data from userPlaces various GUI components
paint MethodPerforms output
Skeleton of a Java Applet
import java.awt.Graphics;import javax.swing.JApplet;
public class WelcomeApplet extends JApplet{
}
Example of Basic Appletimport javax.swing.JApplet;import java.awt.Graphics;
public class AnyApplet extends JApplet{ // declare variables here
public void init( ) { // data initialization goes here }
public void paint( Graphics g ) { super.paint( g ); // your code goes here }}
Running an applet in browser We need to use html code to run applets. The minimum html required
to run applet with a browser (java host) is as follows:
<HTML><HEAD><TITLE>TitleName</TITLE></HEAD><BODY><APPLET> CODE = Classname.class CODEBASE = . directory of class file WIDTH = 50 width of window in pixels HEIGHT = 50 height of window in pixels</APPLET> </BODY></HTML>
Note that in the applet tag you include the. class bytecode file and not the .java.
Features provided by JAppletBecause JApplet is a top-level Swing container,
each Swing applet has a root pane. The most noticeable effects of the root pane's presence are support for adding a menu bar and the need to use a content pane. JApplet has a single content pane. The content
pane makes Swing applets different from regular applets in the following ways: You add components to a Swing applet's content pane,
not directly to the applet. You set the layout manager on a Swing applet's content
pane, not directly on the applet. The default layout manager for a Swing applet's content
pane is BorderLayout. This differs from the default layout manager for Applet, which is FlowLayout.
You should not put painting code directly in a JApplet object.
18
A Simple Applet
19
Drawing rectangles
There are two ways to draw rectangles:g.drawRect( left , top , width , height );
g.fillRect(left , top , width , height );
20
The complete appletimport javax.swing.JApplet;import java.awt.*;
public class Drawing extends JApplet {
public void paint(Graphics g) {
g.setColor(Color.BLUE); g.fillRect(20, 20, 50, 30);
g.setColor(Color.RED); g.fillRect(50, 30, 50, 30);
g.setColor(Color.BLACK); g.drawString("Example JApplet", 20, 80); }}
21
The HTML pageYou can only run an applet from an HTML pageThe HTML looks something like this:
<html> <body> <h1>Drawing Applet</h1> <applet code="Drawing.class" width="100" height="150"> </applet> </body></html>
Eclipse (or BlueJ) will create this HTML for youYou don’t even need to think about the HTML
just yet
Differences Between Applets and GUI Applications
Applets Derived from
JAppletNo main methodUses init method Displayed by HTML Sets title in HTMLSize set in HTMLApplet closes when
HTML doc closes
GUI applicationsclass extends
JFrameInvokes main
methodUses constructorsUses method
setVisibleUses setTitle
methodUses method
setSizeCloses with Exit
button
Converting a GUI Application to an Applet
Change JFrame to JApplet Change constructor to method initRemove method calls such as setVisible,
setTitle, setSizeRemove the method main If applicable, remove Exit button/all code
associated with it
class GraphicsProvides methods for drawing items such as
lines, ovals, and rectangles on the screenContains methods to set the properties of
graphic elements including clipping area, fonts, and colors
Contained in the package java.awt
Constructors and Methods of the class Graphics
Constructors and Methods for the class Graphics
Constructors and Methods for the class Graphics
Constructors and Methods for the class Graphics