http:// japplet

28
http://www.hostitwise.com/java/japplet.html http://download.oracle.com/javase/tutorial/uiswing/ components/applet.html JAPPLET

Upload: bethanie-lyons

Post on 12-Jan-2016

214 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Http://  JAPPLET

•http://www.hostitwise.com/java/japplet.html•http://download.oracle.com/javase/tutorial/uiswing/components/applet.html

JAPPLET

Page 2: Http://  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

Page 3: Http://  JAPPLET

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().

Page 4: Http://  JAPPLET

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

Page 5: Http://  JAPPLET

JApplet hierarchyJApplet hierarchy is as follows:

java.lang.Object  java.awt.Component    java.awt.Container      java.awt.Panel        java.applet.Applet          javax.swing.JApplet

Page 6: Http://  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

Page 7: Http://  JAPPLET

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

Page 8: Http://  JAPPLET

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.

Page 9: Http://  JAPPLET

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.

Page 10: Http://  JAPPLET

Understanding the JApplet Life Cycle

Init()

start()

stop()

stop()

destroy()

Page 11: Http://  JAPPLET

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

Page 12: Http://  JAPPLET

Some more methods

Page 13: Http://  JAPPLET

Applet Methodsinit Method

Initializes variablesGets data from userPlaces various GUI components

paint MethodPerforms output

Page 14: Http://  JAPPLET

Skeleton of a Java Applet

import java.awt.Graphics;import javax.swing.JApplet;

public class WelcomeApplet extends JApplet{

}

Page 15: Http://  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   }}

Page 16: Http://  JAPPLET

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.

Page 17: Http://  JAPPLET

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.

Page 18: Http://  JAPPLET

18

A Simple Applet

Page 19: Http://  JAPPLET

19

Drawing rectangles

There are two ways to draw rectangles:g.drawRect( left , top , width , height );

g.fillRect(left , top , width , height );

Page 20: Http://  JAPPLET

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

Page 21: Http://  JAPPLET

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

Page 22: Http://  JAPPLET

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

Page 23: Http://  JAPPLET

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

Page 24: Http://  JAPPLET

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

Page 25: Http://  JAPPLET

Constructors and Methods of the class Graphics

Page 26: Http://  JAPPLET

Constructors and Methods for the class Graphics

Page 27: Http://  JAPPLET

Constructors and Methods for the class Graphics

Page 28: Http://  JAPPLET

Constructors and Methods for the class Graphics