lecture 09 applets

18
Lecture 09 Applets

Upload: jessica-farmer

Post on 30-Dec-2015

18 views

Category:

Documents


1 download

DESCRIPTION

Lecture 09 Applets. Introduction to Applets. Applets should NOT have main method but rather init, stop, paint etc They should be run through javac compiler getting a .class file as before Create an HTML file (say HelloWorld.HTML in same directory as .class file) and include in this - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Lecture 09  Applets

Lecture 09 Applets

Page 2: Lecture 09  Applets

Introduction to Applets

Applets should NOT have main method but rather init, stop, paint etc

They should be run through javac compiler getting a .class file as before

Create an HTML file (say HelloWorld.HTML in same directory as .class file) and include in this

<applet code="Example.class" width=500 height=200 > </applet> in simplest case with no parameters where applet will run in window of given width and height (in pixels)

Page 3: Lecture 09  Applets

More on Applets

Given the following HTML <APPLET CODE="StockGraph" CODEBASE="http://www.javasoft.com/applets/"

WIDTH=200 HEIGHT=200> </APPLET> Runs the "StockGraph.class" executable as an

applet

Page 4: Lecture 09  Applets

Java applets

Java application executes from a command window an applet is a Java program that runs in the

appletviewer or in a Web browser The appletviewer (or browser) executes an applet

when a HTML document containing the applet is opened in the appletviewer (or browser).

Page 5: Lecture 09  Applets

Applets in Code Warrior

Start a new project in CW choose java stationary and applet (in a window

jdk1.3) Project with three source files: TrivialApplet.java,

TrivialApplet.html and TrivialAppletDebug.html Open the java file.

– import java.awt.*;– import java.applet.Applet;

We import objects from two packages which supports graphical user interface: AWT (abstract windowing tool). There is a newest version of class Applet called JApplet, from the new package java.swing (SWING is built on awt and exdends its capabilities).

Page 6: Lecture 09  Applets

Applets in Code Warrior

We import objects from two packages which supports graphical user interface:

– AWT (abstract windowing tool). – the new package java.swing (SWING is built on awt and exdends

its capabilities) public class TrivialApplet extends Applet

The extends keyword followed by a class name indicates the class (in this case Applet) from which our new class inherits existing pieces. In this inheritance relationship, Applet is called the superclass or base class and TrivialApplet is called the subclass or derived class. We will discuss inheritance in detail later. Using inheritance a new class TrivialApplet has the data and methods of the Applet class as well as the new features, like a method paint(), which overrides Applet's method paint().

three methods init, start and paint that are guaranteed to be called automatically for you when any applet begins execution.

– called exactly in that order

Page 7: Lecture 09  Applets

init, start methods

init public void init()

Called by the browser or applet viewer to inform this applet that it has been loaded into the system. It is always called before the first time that the start method is called. A subclass of Applet should override this method if it has initialization to perform.

start public void start()

Called by the browser or applet viewer to inform this applet that it should start its execution. It is called after the init method and each time the applet is revisited in a Web page. A subclass of Applet should override this method if it has any operation that it wants to perform each time the Web page containing it is visited. For example, an applet with animation might want to use the start method to resume animation, and the stop method to suspend the animation.

Page 8: Lecture 09  Applets

init, start and paint methods

The appletviewer or browser expects each of these methods to be defined so it can provide a consistent start-up sequence for an applet.

Some sample codeimport java.awt.*;import java.applet.Applet;public class test extends Applet{ // Initialize the applet public void init() { } // Paint graphics on the screen public void paint(Graphics g) { g.drawString( "Hello World!!!", 30, 30 ); }}

Page 9: Lecture 09  Applets

drawString method

The first argument to drawString is the string to draw

The last two arguments in the list, 30 and 30, are the coordinates at which the bottom-left corner of the string should be drawn in the applet's area on the screen

Coordinates are measured from the upper-left corner of the applet in pixels: first 30 (x-coordinate) is a number of pixel to the right, and the second parameter (y-coordinate) is number of pixel down.

Page 10: Lecture 09  Applets

Executing a Java Applet

provide an HTML text file that indicates which applet the appletviewer (or browser) should load and execute

<applet codebase="Java Classes" code="TrivialApplet.class" width=200 height=200></applet>

Page 11: Lecture 09  Applets

Java Resources

There is a large number of Java applet resources available to you. The best place to start is

http://java.sun.com/applets/index.html

Page 12: Lecture 09  Applets

Event Listeners

If a class want to respond to a user event, it must implement the interface (we will discuss interfaces in a few lectures) that deals with the event

These interfaces are called event listeners. Each listener handles a specific kind of event A sample of event listeners:

– ActionListener - action event that generated by a user taking an action such as a click on a button.

– AdjustmentListener - an event that generated when the componenet such as a scrollbar is moved.

– ItemListener - item event that generated when an item such as a check box has been changed.

Page 13: Lecture 09  Applets

Event Listeners

more sample event listeners:– KeyListener - keyboard event that occurs when a user types

on a keyboard.– MouseListener - mouse event that generated by a mouse

click.– MouseMotionListener - mouse event that generated by a

mouse movement.– WindowListener - window event that generated by window

move or adjustment.

Page 14: Lecture 09  Applets

an example of a proper class declaration:

public class Foo extends Applet implements ActionListener, MouseListener

{ ... } By making a class event listener you have to set up

a specific type of event You have to add a matching listener. Once a component (a button, for example) is created

you can add one of following methods associated with it:

Page 15: Lecture 09  Applets

Listener Methods

addActionListener() - Button, CheckBox, ComboBox, TextField, RadioButton.

addAdjustmentListener() - ScrollBar. addItemListener() - Button, CheckBox, ComboBox,

RadioButton. addKeyListener() - all components. addMouseListener() - all components. addMouseMotionListener() - all components.

Page 16: Lecture 09  Applets

Names

names of the component may be different depending on which package you use

All above components are in AWT (import java.applet.Applet; )

In the new package SWING (import javax.swing.JApplet; ), all above components have the capital letter J in front: JButton, JCheckBox and so on.

Page 17: Lecture 09  Applets

An Example

create a button and associate an action event listener

JButton b = new JButton("give him a name"); b.addActionListener(this); The ActionListener interface contains only one

method actionPerformed() public void actionPerformed(ActionEvent e) { //what to do when a user //clicks the button? }

Page 18: Lecture 09  Applets

Example

public void actionPerformed(ActionEvent e) { if (e.getSource() == buttonOne) ... else ... } If you have more than one componenet has an event listener,

you have to figure out which one does what. The class ActionEvent (a parameter in actionPerformed()) has a

method getSource() which can help to determine which component generated the event.