CSC 205 – Java Programming II Lecture 23 Applet. Types of Java Programs Applets Applications...

29
CSC 205 – Java Programming II Lecture 23 Applet

Transcript of CSC 205 – Java Programming II Lecture 23 Applet. Types of Java Programs Applets Applications...

Page 1: CSC 205 – Java Programming II Lecture 23 Applet. Types of Java Programs Applets Applications Console applications Graphics applications Applications are.

CSC 205 – Java Programming II

Lecture 23

Applet

Page 2: CSC 205 – Java Programming II Lecture 23 Applet. Types of Java Programs Applets Applications Console applications Graphics applications Applications are.

Types of Java Programs

Applets Applications

Console applications Graphics applications

Applications are stand-alone programs An application must have a main method

Page 3: CSC 205 – Java Programming II Lecture 23 Applet. Types of Java Programs Applets Applications Console applications Graphics applications Applications are.

What Is Applet

An applet is a program that adheres to a set of

conventions run within a Java-compatible Web browser downloaded from a Web server

An applet must extend the Applet class

Page 4: CSC 205 – Java Programming II Lecture 23 Applet. Types of Java Programs Applets Applications Console applications Graphics applications Applications are.

Download From Web Server

Page 5: CSC 205 – Java Programming II Lecture 23 Applet. Types of Java Programs Applets Applications Console applications Graphics applications Applications are.

Advantages & Disadvantages

• Advantages – Can be accessed where Internet is available

– When GUIs need to be upgraded, just change them at one location: on the Web server

• Disadvantages – Time to download may be long

– Security could be an issue

Page 6: CSC 205 – Java Programming II Lecture 23 Applet. Types of Java Programs Applets Applications Console applications Graphics applications Applications are.

Security Policies

Browsers impose the following restrictions on any applet that is loaded over the network: An applet cannot load libraries or define native

methods. It cannot ordinarily read or write files, or start

any program on the host that's executing it. It cannot make network connections except to

the host that it came from. It cannot read certain system properties. Windows that an applet brings up look different

than windows that an application brings up.

Page 7: CSC 205 – Java Programming II Lecture 23 Applet. Types of Java Programs Applets Applications Console applications Graphics applications Applications are.

Inheritance Hierarchy

java.applet Class Appletjava.lang.Object

| +--java.awt.Component

| +--java.awt.Container

| +--java.awt.Panel

| +--java.applet.Applet

Page 8: CSC 205 – Java Programming II Lecture 23 Applet. Types of Java Programs Applets Applications Console applications Graphics applications Applications are.

A Typical Applet

import java.applet.Applet; import java.awt.Graphics; public class Simple extends Applet {

//methods to be overriddenpublic void init() {…} public void start() {…} public void stop() {…} public void destroy() {…} void addItem(String newWord) { …; repaint(); } public void paint(Graphics g) {…} //inherited from the Container class

}

Page 9: CSC 205 – Java Programming II Lecture 23 Applet. Types of Java Programs Applets Applications Console applications Graphics applications Applications are.

Life Cycle of an Applet

• Loading the Applet – An instance of the applet's controlling class (an

Applet subclass) is created

– The applet initializes itself

– The applet starts running

• Leaving and Returning to the Applet's Page– The applet stops running when leaving the page

– the applet can start itself again when returning

Page 10: CSC 205 – Java Programming II Lecture 23 Applet. Types of Java Programs Applets Applications Console applications Graphics applications Applications are.

Methods for Milestones

An applet can override the following methods init To initialize the applet each time it's

loaded (or reloaded). start To start the applet's execution, such

as when the applet's loaded or when the user revisits a page that contains the applet.

stop To stop the applet's execution, such as when the user leaves the applet's page or quits the browser.

destroy To perform a final cleanup in preparation for unloading

Page 11: CSC 205 – Java Programming II Lecture 23 Applet. Types of Java Programs Applets Applications Console applications Graphics applications Applications are.

Methods for Drawing

An applet can override the following two display methods (of the Container class) : paint

The basic display method. Many applets implement the paint method to draw the applet's representation within a browser page.

update

A method you can use along with paint to improve drawing performance

Page 12: CSC 205 – Java Programming II Lecture 23 Applet. Types of Java Programs Applets Applications Console applications Graphics applications Applications are.

Animation

To update applet changes invoke the repaint method (inherited from the Component class) periodically

The paint method will be invoked when the applet is repainted

Page 13: CSC 205 – Java Programming II Lecture 23 Applet. Types of Java Programs Applets Applications Console applications Graphics applications Applications are.

Event Handling

Applets inherit a group of event-handling methods from the Component class

To react to an event, an applet must override either the appropriate event-specific method,

or the handleEvent method (from the Component class)

Page 14: CSC 205 – Java Programming II Lecture 23 Applet. Types of Java Programs Applets Applications Console applications Graphics applications Applications are.

Event Handling – Example

Adding the following code to the Simple applet makes it respond to mouse clicks.

import java.awt.Event;

. . .

public boolean mouseDown(Event event, int x, int y) {

addItem("click!... ");

return true;

}

Page 15: CSC 205 – Java Programming II Lecture 23 Applet. Types of Java Programs Applets Applications Console applications Graphics applications Applications are.

Deprecation

Unfortunately, many of the samples available online or in textbooks use deprecated methods

You will be warned when you compile code with deprecated methods

Use the –deprecation option to see detailed infojavac –deprecation Simple.java

Replace deprecated methods with newer methods as recommended by latest version of Java API

Page 16: CSC 205 – Java Programming II Lecture 23 Applet. Types of Java Programs Applets Applications Console applications Graphics applications Applications are.

Deprecation

Page 17: CSC 205 – Java Programming II Lecture 23 Applet. Types of Java Programs Applets Applications Console applications Graphics applications Applications are.

Using UI Components

Because the Applet class inherits from the AWT Container class, it's easy to add components to applets and to use layout managers to control the components' onscreen positions. add

Adds the specified Component remove

Removes the specified Component setLayout

Sets the layout manager

Page 18: CSC 205 – Java Programming II Lecture 23 Applet. Types of Java Programs Applets Applications Console applications Graphics applications Applications are.

Testing Applets

Two ways to run an applet Use the applet viewer

appletviewer simple.html Embed applets into Web pages

Both need to use the applet tag in HTML files<APPLET CODE=AppletSubclass.class WIDTH=anInt HEIGHT=anInt>

</APPLET>

Page 19: CSC 205 – Java Programming II Lecture 23 Applet. Types of Java Programs Applets Applications Console applications Graphics applications Applications are.

HTML – An Overview

Markup language use tags to represent the meaning and/or

content of the enclosed data Some features

Not case sensitive Loose syntax Predefined tags Text is the only data type

Page 20: CSC 205 – Java Programming II Lecture 23 Applet. Types of Java Programs Applets Applications Console applications Graphics applications Applications are.

More Examples

The following two tags are equivalent <APPLET CODE=Simple.class WIDTH=100 HEIGHT=100>

</APPLET>

<applet code=“Simple.class” width=“100” height=“100”>

</applet>

Page 21: CSC 205 – Java Programming II Lecture 23 Applet. Types of Java Programs Applets Applications Console applications Graphics applications Applications are.

A Simple HTML File

<html> <body> <p>Click on the applet to start

the animation.</p> <!– this line is comment -->

<applet code="SelectionSortApplet.class" width="300" height="300">

</applet> </body></html>

Page 22: CSC 205 – Java Programming II Lecture 23 Applet. Types of Java Programs Applets Applications Console applications Graphics applications Applications are.

Thread

A thread is a program unit that is executed independently of other parts of the program

The JVM executes each thread for a short time and then switches to another thread

A programmer can concentrate on what tasks each thread need to perform and possibly, the communication between

threads

Page 23: CSC 205 – Java Programming II Lecture 23 Applet. Types of Java Programs Applets Applications Console applications Graphics applications Applications are.

Write a Thread

Follow the stepsWrite a class that extends the Thread

classPlace the code for the task in the run

methodCreate an object of the your thread

classCall the start method to activate your

thread

Page 24: CSC 205 – Java Programming II Lecture 23 Applet. Types of Java Programs Applets Applications Console applications Graphics applications Applications are.

Sample Thread Class

public class GreetingThread extends Thread{ public GreetingThread(String aGreeting){ greeting = aGreeting; }

public void run() { try { for (int i = 1; i <= REPETITIONS; i++) { Date now = new Date(); System.out.println(now + " " + greeting); sleep(DELAY); } } catch (InterruptedException exception){ } }

Page 25: CSC 205 – Java Programming II Lecture 23 Applet. Types of Java Programs Applets Applications Console applications Graphics applications Applications are.

Running A Thread – I

public class GreetingThreadTest{

public static void main(String[] args) { GreetingThread t1

= new GreetingThread("Hello, World!"); GreetingThread t2

= new GreetingThread("Goodbye, World!");

t1.start(); t2.start(); }}

Page 26: CSC 205 – Java Programming II Lecture 23 Applet. Types of Java Programs Applets Applications Console applications Graphics applications Applications are.

Sample Output

Page 27: CSC 205 – Java Programming II Lecture 23 Applet. Types of Java Programs Applets Applications Console applications Graphics applications Applications are.

The Runnable Interface

Problems of writing a thread which extends another class, e.g. the Frame class Since Java doesn’t allow multiple inheritance

Can implement the Runnable interface insteadpublic class MyFrame extends Frame

implements Runnable {

public MyFrame() {…}

public void run() {…}

}

Page 28: CSC 205 – Java Programming II Lecture 23 Applet. Types of Java Programs Applets Applications Console applications Graphics applications Applications are.

Running A Thread – II

Follow the stepsConstruct a Runnable objectCreate a thread using the Runnable

objectInvoke the start method of the

thread

MyFrame frame = new MyFrame();

Thread myThread = new Thread(frame);

myThread.start();

Page 29: CSC 205 – Java Programming II Lecture 23 Applet. Types of Java Programs Applets Applications Console applications Graphics applications Applications are.

start

interrupt