java applets. 2 introduction to java applet programs applications are stand alone programs executed...

22
Java Applets Java Applets

Upload: dennis-oneal

Post on 31-Dec-2015

240 views

Category:

Documents


4 download

TRANSCRIPT

Page 1: Java Applets. 2 Introduction to Java Applet Programs  Applications are stand alone programs executed with Java interpreter executed with Java interpreter

Java AppletsJava Applets

Page 2: Java Applets. 2 Introduction to Java Applet Programs  Applications are stand alone programs executed with Java interpreter executed with Java interpreter

22

Introduction to Java Applet Introduction to Java Applet ProgramsPrograms

Applications are stand alone programsApplications are stand alone programs executed with Java interpreterexecuted with Java interpreter

Applet is a small programApplet is a small program can be placed on a web pagecan be placed on a web page will be executed by the web browserwill be executed by the web browser give web pages “dynamic content”give web pages “dynamic content”

Page 3: Java Applets. 2 Introduction to Java Applet Programs  Applications are stand alone programs executed with Java interpreter executed with Java interpreter

33

Java AppletsJava Applets

Built using one of general definitions of Built using one of general definitions of appletsapplets AppletApplet class class JAappletJAapplet class class

Java applets are usually graphicalJava applets are usually graphical Draw graphics in a defined screen areaDraw graphics in a defined screen area Enable user interaction with GUI elementsEnable user interaction with GUI elements

Page 4: Java Applets. 2 Introduction to Java Applet Programs  Applications are stand alone programs executed with Java interpreter executed with Java interpreter

44

Java Applet ClassesJava Applet Classes

AAbstract bstract WWindowing indowing TToolkit oolkit AWTAWT Earlier versions of JavaEarlier versions of Java AppletApplet class is one of the AWT components class is one of the AWT components

JJava ava FFoundation oundation CClasses lasses JFCJFC Extension to Java in 1997Extension to Java in 1997 Has a collection of Has a collection of Swing componentsSwing components for for

enhanced GUIsenhanced GUIs

Swing component classes begin with Swing component classes begin with JJ

Page 5: Java Applets. 2 Introduction to Java Applet Programs  Applications are stand alone programs executed with Java interpreter executed with Java interpreter

55

Java AppletsJava Applets

Applets are Java programs that can be Applets are Java programs that can be embedded in HTML documentsembedded in HTML documents To run an applet you must create a .html file which To run an applet you must create a .html file which

references the appletreferences the applet Ready to ProgramReady to Program also will run an applet also will run an applet

When browser loads Web page containing When browser loads Web page containing appletapplet Applet downloads into Web browserApplet downloads into Web browser begins executionbegins execution

Can be tested using appletviewer programCan be tested using appletviewer program

Page 6: Java Applets. 2 Introduction to Java Applet Programs  Applications are stand alone programs executed with Java interpreter executed with Java interpreter

66

Contrast Application with Applet Contrast Application with Applet

Application Applet

•Object class extended•Class not declared public•Has a main()•static keyword used

•Uses System.exit(1)

•JApplet class extended•class declared to be public•init() instead of main()•init() not declared with static keyword

Page 7: Java Applets. 2 Introduction to Java Applet Programs  Applications are stand alone programs executed with Java interpreter executed with Java interpreter

77

Applet DeclarationApplet Declaration

Syntax (note difference from Syntax (note difference from application application declaration)declaration)public class ClassName extends JAappletpublic class ClassName extends JAapplet

ClassName is an object that will be a subclass of JApplet

Page 8: Java Applets. 2 Introduction to Java Applet Programs  Applications are stand alone programs executed with Java interpreter executed with Java interpreter

88

Body of an AppletBody of an Applet

Note there is no Note there is no main()main() method in an method in an appletapplet JAppletJApplet class provides other methods instead class provides other methods instead

of a of a mainmain method method First method executed is the First method executed is the init()init()

methodmethod

Page 9: Java Applets. 2 Introduction to Java Applet Programs  Applications are stand alone programs executed with Java interpreter executed with Java interpreter

99

AppletsApplets AppletApplet

Program that runs in Program that runs in • appletviewerappletviewer (test utility for applets) (test utility for applets)• Web browser (IE, Communicator)Web browser (IE, Communicator)

Executes when HTML (Hypertext Markup Language) Executes when HTML (Hypertext Markup Language) document containing applet is openeddocument containing applet is opened

Applications run in command windowsApplications run in command windows

NotesNotes Focus on fundamental programming concepts firstFocus on fundamental programming concepts first

• Explanations will come laterExplanations will come later

Page 10: Java Applets. 2 Introduction to Java Applet Programs  Applications are stand alone programs executed with Java interpreter executed with Java interpreter

1010

Applets and Web Pages – Applets and Web Pages – HTML HTML

Applets embedded in a web pageApplets embedded in a web page Executed when web page loaded by browserExecuted when web page loaded by browser

Web pages structured with Web pages structured with HTMLHTML codes codes HHyperyperTText ext MMark-upark-up LLanguageanguage

SyntaxSyntax<command><command> . . . . . .</command></command>

Turns format onTurns format on

Turns the format offTurns the format off

Page 11: Java Applets. 2 Introduction to Java Applet Programs  Applications are stand alone programs executed with Java interpreter executed with Java interpreter

1111

Applets and Web Pages – Applets and Web Pages – HTMLHTML

Embedding Java appletsEmbedding Java applets Insert applet tagsInsert applet tags<APPLET><APPLET></APPLET></APPLET>

Call the specific applet by its file nameCall the specific applet by its file name<APPLET CODE = "Whatever.class" <APPLET CODE = "Whatever.class" WIDTH = nnn HEIGHT = mmmm> WIDTH = nnn HEIGHT = mmmm><\APPLET><\APPLET>

Where Where nnnnnn and and mmmmmm are specific pixel sizes are specific pixel sizes

Page 12: Java Applets. 2 Introduction to Java Applet Programs  Applications are stand alone programs executed with Java interpreter executed with Java interpreter

1212

Applets and Web Pages – Applets and Web Pages – HTMLHTML

Create the web Create the web page code using a page code using a text editortext editor

Save it with Save it with an .html suffixan .html suffix

Open this file with Open this file with appletviewer or with appletviewer or with a web browser that a web browser that supports Javasupports Java

Java Plug-in must Java Plug-in must be installed (part of be installed (part of J2SDK 1.4.1 from J2SDK 1.4.1 from Sun)Sun)

<HTML>

<HEAD>

</HEAD>

<BODY>

<APPLET CODE = . . . >

</APPLET>

</BODY>

</HTML>

Page 13: Java Applets. 2 Introduction to Java Applet Programs  Applications are stand alone programs executed with Java interpreter executed with Java interpreter

1313

Applets and Web Pages – Applets and Web Pages – HTMLHTML

Client Web browser anywhere can access Client Web browser anywhere can access this web page from its host serverthis web page from its host server

Embedded Java applet runs on client Embedded Java applet runs on client browser (of any type platform)browser (of any type platform)

This means a client anywhere on any type This means a client anywhere on any type of platform can run a piece of software of platform can run a piece of software developed on any other type of platformdeveloped on any other type of platform

Platform Independence

Page 14: Java Applets. 2 Introduction to Java Applet Programs  Applications are stand alone programs executed with Java interpreter executed with Java interpreter

1414

Thinking About ObjectsThinking About Objects

Java an object-oriented languageJava an object-oriented language However, Java has constructs from structured However, Java has constructs from structured

programmingprogramming Object orientationObject orientation

Natural way to think about world and writing computer Natural way to think about world and writing computer programsprograms

• Object-oriented programming models the real worldObject-oriented programming models the real world Attributes - properties of objectsAttributes - properties of objects

• Size, shape, color, weight, etc.Size, shape, color, weight, etc. Behaviors - actions that objects can performBehaviors - actions that objects can perform

• AA ball rolls, bounces, inflates and deflatesball rolls, bounces, inflates and deflates

Page 15: Java Applets. 2 Introduction to Java Applet Programs  Applications are stand alone programs executed with Java interpreter executed with Java interpreter

1515

Thinking About ObjectsThinking About Objects

Object orientation (continued)Object orientation (continued) InheritanceInheritance

• New classes of objects absorb characteristics of New classes of objects absorb characteristics of existing classesexisting classes

Information hidingInformation hiding• Objects usually do not know how other objects are Objects usually do not know how other objects are

implementedimplemented• We can drive cars without knowing how every part We can drive cars without knowing how every part

works internallyworks internally

Page 16: Java Applets. 2 Introduction to Java Applet Programs  Applications are stand alone programs executed with Java interpreter executed with Java interpreter

1616

Thinking About ObjectsThinking About Objects

Class - unit of Java programmingClass - unit of Java programming Java focuses on nouns (classes) Java focuses on nouns (classes)

C focuses on verbs and is action orientedC focuses on verbs and is action oriented

Contain methodsContain methods Implement behaviorsImplement behaviors

Contain dataContain data Implement attributesImplement attributes

Classes are reusableClasses are reusable Standardized, interchangeable partsStandardized, interchangeable parts

Page 17: Java Applets. 2 Introduction to Java Applet Programs  Applications are stand alone programs executed with Java interpreter executed with Java interpreter

1717

A A Simple Java Applet: Drawing a StringSimple Java Applet: Drawing a String

Figure 3.6 Figure 3.6 – a welcome message applet– a welcome message applet The .html code to run the applet in a The .html code to run the applet in a

browserbrowser

The program output shown in the Applet The program output shown in the Applet ViewerViewer

<html><applet code = "WelcomeApplet.class" width = "300" height = "45"></applet></html>

<html><applet code = "WelcomeApplet.class" width = "300" height = "45"></applet></html>

Page 18: Java Applets. 2 Introduction to Java Applet Programs  Applications are stand alone programs executed with Java interpreter executed with Java interpreter

1818

Running the AppletRunning the Applet

CompileCompile Use Use Ready to ProgramReady to Program If no errors, bytecodes stored in If no errors, bytecodes stored in WelcomeApplet.class

We must create an HTML file We must create an HTML file Loads the applet into Loads the applet into appletviewerappletviewer or a browser or a browser Ends in Ends in .htm.htm or or .html.html

To execute an appletTo execute an applet Create an HTML file indicating which applet the Create an HTML file indicating which applet the

browser (or browser (or appletviewerappletviewer) should load and ) should load and executeexecute

Page 19: Java Applets. 2 Introduction to Java Applet Programs  Applications are stand alone programs executed with Java interpreter executed with Java interpreter

1919

Running the Applet - AlternativelyRunning the Applet - Alternatively

Run from within Run from within Ready to ProgramReady to Program Prompt for applet window size appearsPrompt for applet window size appears Applet window runsApplet window runs

Page 20: Java Applets. 2 Introduction to Java Applet Programs  Applications are stand alone programs executed with Java interpreter executed with Java interpreter

2020

import allows us to use predefined classes (allowing us to use applets and graphics, in this case).

extends allows us to inherit the capabilities of class JApplet.

Method paint is guaranteed to be called in all applets. Its first line must be defined as above.

Page 21: Java Applets. 2 Introduction to Java Applet Programs  Applications are stand alone programs executed with Java interpreter executed with Java interpreter

2121

Running An AppletRunning An Applet

import java.applet.Applet;import java.applet.Applet;import java.awt.Graphics;import java.awt.Graphics;

public class HelloApplet extends Applet {public class HelloApplet extends Applet {public void paint (Graphics g)public void paint (Graphics g){{

g.drawString ("Hello. Welcome to",25,25);g.drawString ("Hello. Welcome to",25,25);g.drawString ("Java Programming",25,40);g.drawString ("Java Programming",25,40);

}}}}

import java.applet.Applet;import java.applet.Applet;import java.awt.Graphics;import java.awt.Graphics;

public class HelloApplet extends Applet {public class HelloApplet extends Applet {public void paint (Graphics g)public void paint (Graphics g){{

g.drawString ("Hello. Welcome to",25,25);g.drawString ("Hello. Welcome to",25,25);g.drawString ("Java Programming",25,40);g.drawString ("Java Programming",25,40);

}}}}

• Enter this text into your Ready to Program editor

• Compile the Java code

• Enter this text into your Ready to Program editor

• Compile the Java code

Page 22: Java Applets. 2 Introduction to Java Applet Programs  Applications are stand alone programs executed with Java interpreter executed with Java interpreter

2222

Running An AppletRunning An Applet

Now create an .Now create an .htmlhtml file to run the applet file to run the applet

Save it as Save it as HelloApplet.htmlHelloApplet.html Make sure you save it in the same Make sure you save it in the same

directory as the directory as the ..javajava file file

<html><applet code = "HelloApplet.class" width=275, height = 100></applet></html>

<html><applet code = "HelloApplet.class" width=275, height = 100></applet></html>