internet software development applets paul j krause

30
Internet Software Internet Software Development Development Applets Applets Paul J Krause Paul J Krause

Upload: daisy-spencer

Post on 29-Dec-2015

218 views

Category:

Documents


0 download

TRANSCRIPT

Internet Software Internet Software DevelopmentDevelopment

AppletsApplets

Paul J KrausePaul J Krause

ContentsContents

Applet BasicsApplet Basics Passing parameters to appletsPassing parameters to applets Simple graphics operationsSimple graphics operations Animating appletsAnimating applets

Hello.javaHello.java

// Filename: Hello.java// Filename: Hello.java

public class Hello {public class Hello {

public static void main(String args) {public static void main(String args) {

System.out.println(“Hello fom Venus”);System.out.println(“Hello fom Venus”);

}}

}}

AppletsApplets

Java applications are executed from the Java applications are executed from the command linecommand line

Applets are small applications that can be Applets are small applications that can be embedded in html pagesembedded in html pages

They can then be downloaded via the They can then be downloaded via the world wide web and invoked in a browserworld wide web and invoked in a browser

Template for an AppletTemplate for an Applet

Subclass one of the Java foundation Subclass one of the Java foundation classes:classes: java.applet.Applet;java.applet.Applet; javax.swing.JApplet;javax.swing.JApplet;

It’s behaviour is invoked in a “paint” It’s behaviour is invoked in a “paint” method, not a “main” methodmethod, not a “main” method

// File: MyApplet.java// File: MyApplet.java

import java.awt.*;import java.awt.*;import java.applet.Applet;import java.applet.Applet;public class MyApplet extends Applet {public class MyApplet extends Applet {

public void paint(Graphics g) {public void paint(Graphics g) {// Do something interesting// Do something interesting

}}

}}

Viewing Area of AppletViewing Area of Applet

y

x(0,0)

height

width

These dimensions are set in the HTML file

Contract for the Contract for the paintpaint method method

Dimensions of the region on the Web page Dimensions of the region on the Web page assigned to the applet are set within an assigned to the applet are set within an <applet><applet> tag in an HTML file tag in an HTML file

The The paint( )paint( ) method defines the method defines the appearance of the applet within this appearance of the applet within this rectangular regionrectangular region

The The paint( )paint( ) method is invoked when method is invoked when the applet is initially loaded in the browser the applet is initially loaded in the browser (or applet viewer)(or applet viewer)

HelloFromVenus.javaHelloFromVenus.java

import java.awt.*;import java.awt.*;import java.applet.Applet;import java.applet.Applet;public class HelloFromVenus extends Applet {public class HelloFromVenus extends Applet {

public void paint(Graphics g) {public void paint(Graphics g) {Dimension d = getSize( );Dimension d = getSize( );

// Gets the size of the rectangle allocated to an // Gets the size of the rectangle allocated to an // instance of this applet// instance of this applet

}}}}

painting the backgroundpainting the background

import java.awt.*;import java.awt.*;import java.applet.Applet;import java.applet.Applet;public class HelloFromVenus extends Applet {public class HelloFromVenus extends Applet {

public void paint(Graphics g) {public void paint(Graphics g) {Dimension d = getSize( );Dimension d = getSize( );g.setColor(Color.black);g.setColor(Color.black);g.fillRect(0, 0, d.width, d.height);g.fillRect(0, 0, d.width, d.height);

}}}}

painting the textpainting the text

public void paint(Graphics g) {public void paint(Graphics g) {Dimension d = getSize( );Dimension d = getSize( );g.setColor(Color.black);g.setColor(Color.black);g.fillRect(0, 0, d.width, d.height);g.fillRect(0, 0, d.width, d.height);

// set font style and size:-// set font style and size:-g.setFont(new Font(“Sans-serif”, Font.BOLD, 24);g.setFont(new Font(“Sans-serif”, Font.BOLD, 24);

// change colour on paint brush:-// change colour on paint brush:-g.setColor(255, 215, 0); g.setColor(255, 215, 0); // note different way!// note different way!g.drawString(“Hello from Venus!”, 40, 25);g.drawString(“Hello from Venus!”, 40, 25);}}

finally add the imagefinally add the image

public void paint(Graphics g) {public void paint(Graphics g) {Dimension d = getSize( );Dimension d = getSize( );g.setColor(Color.black);g.setColor(Color.black);g.fillRect(0, 0, d.width, d.height);g.fillRect(0, 0, d.width, d.height);g.setFont(new Font(“Sans-serif”, Font.BOLD, 24);g.setFont(new Font(“Sans-serif”, Font.BOLD, 24);g.setColor(255, 215, 0); g.setColor(255, 215, 0); g.drawString(“Hello from Venus!”, 40, 25);g.drawString(“Hello from Venus!”, 40, 25);g.drawImage(getImage(getCodeBase( ),g.drawImage(getImage(getCodeBase( ),

““ Venus.gif”, 20, 60, this);Venus.gif”, 20, 60, this);}}

Embedding Applets in HTMLEmbedding Applets in HTML

< applet < applet code = code = bytecode-filenamebytecode-filename

width = width = pixelspixels

height = height = pixels >pixels >

</applet></applet>

<HTML><HTML><HEAD><HEAD> <TITLE><TITLE>

HelloFromVenus AppletHelloFromVenus Applet </TITLE></TITLE></HEAD></HEAD>

<BODY BGCOLOR=BLACK TEXT=white><BODY BGCOLOR=BLACK TEXT=white><CENTER><CENTER> <H2>Here is the <EM>Hello From Venus</EM> Applet</H2><H2>Here is the <EM>Hello From Venus</EM> Applet</H2> <APPLET CODE="HelloFromVenus.class" WIDTH=300 <APPLET CODE="HelloFromVenus.class" WIDTH=300

HEIGHT=350>HEIGHT=350> </APPLET></APPLET></CENTER></CENTER>&nbsp; &nbsp; &nbsp; &nbsp; Venus photo courtesy of NASA.&nbsp; &nbsp; &nbsp; &nbsp; Venus photo courtesy of NASA.</BODY></BODY>

</HTML</HTML>>

Parameter passingParameter passing

To make applets reusable in different To make applets reusable in different contexts, we need to be able to pass contexts, we need to be able to pass parameters from the html filesparameters from the html files

Can do this by using:Can do this by using: <param name><param name> tag in html, and tag in html, and getParameter(String)getParameter(String) method in the method in the

appletapplet

in the html filein the html file

< applet < applet code = bytecode-filenamecode = bytecode-filename

width = pixelswidth = pixels

height = pixels >height = pixels >

<param name=<param name=StringString value= value=StringString>>

……

</applet></applet>

in the appletin the applet

String input = getParameter(String input = getParameter(StringString););

ExampleExample

In the html file:In the html file: <param name=“text” value=“Web Technology”><param name=“text” value=“Web Technology”>

In the appletIn the applet textV = getParameter(“text”);textV = getParameter(“text”);

Initialisation of an appletInitialisation of an applet

Initialisation of an applet is not done in the Initialisation of an applet is not done in the Constructor method, but in an Constructor method, but in an init( )init( ) method method

Why?Why? An An applet contextapplet context interprets the <applet> tag in interprets the <applet> tag in

an html filean html file The applet context first constructs an instance of The applet context first constructs an instance of

the applet, the applet, thenthen interprets the remaining interprets the remaining parameters in the parameters in the <applet><applet> tag tag

The The init( )init( ) method is invoked method is invoked afterafter the the information in the information in the <applet><applet> tag has been tag has been processedprocessed

import java.awt.*;import java.awt.*;import java.applet.*;import java.applet.*;

public class Font02 extends Applet {public class Font02 extends Applet { String tmpSt, textV;String tmpSt, textV; int sizeV;int sizeV;

public void init( ) {public void init( ) {textV = getParameter("text");textV = getParameter("text");tmpSt = getParameter("size");tmpSt = getParameter("size");sizeV = Integer.parseInt(tmpSt)sizeV = Integer.parseInt(tmpSt)

}} public void paint(Graphics gg) {public void paint(Graphics gg) {

… … }}}}

<html><html>

<head><head>

<title>Font Rotation and Translation</title><title>Font Rotation and Translation</title>

</head></head>

<body style="font-size:18pt; text-align:center;font-weight:bold"><body style="font-size:18pt; text-align:center;font-weight:bold">

Font Rotation and Translation<br /><br />Font Rotation and Translation<br /><br />

<applet code="Font02.class" width=450 height=400><applet code="Font02.class" width=450 height=400>

<param name="text" value="....Web Technology"><param name="text" value="....Web Technology">

<param name="size" value="18"><param name="size" value="18">

</applet></applet>

</body></body>

</html></html>

shifting graphical objectsshifting graphical objects

To translate a graphical object, use:To translate a graphical object, use: translate(xpos, ypos);translate(xpos, ypos);

To rotate a graphical object, use:To rotate a graphical object, use: rotate(angle_in_radians);rotate(angle_in_radians);

public voidpublic void paint(Graphics gg) {paint(Graphics gg) {

int ii;int ii;

Graphics2D g = (Graphics2D) gg;Graphics2D g = (Graphics2D) gg;g.setFont( new Font("Arial", Font.BOLD, sizeV) );g.setFont( new Font("Arial", Font.BOLD, sizeV) );g.setColor(Color.blue)g.setColor(Color.blue)g.translate(200, 200);g.translate(200, 200);for(ii = 1; ii <= 16; ii++)for(ii = 1; ii <= 16; ii++){{ g.rotate(Math.PI/8.0);g.rotate(Math.PI/8.0); g.drawString( textV, 20, 0);g.drawString( textV, 20, 0);}}

public void public void paint(Graphics gg) {paint(Graphics gg) {

int ii;int ii;

Graphics2D g = (Graphics2D) gg;Graphics2D g = (Graphics2D) gg;g.setFont( new Font("Arial", Font.BOLD, sizeV) );g.setFont( new Font("Arial", Font.BOLD, sizeV) );

g.translate(200, 200);g.translate(200, 200);for(ii = 1; ii <= 16; ii++)for(ii = 1; ii <= 16; ii++){{ g.rotate(Math.PI/8.0);g.rotate(Math.PI/8.0); g.setColor( new Color( (int) (Math.random( )*256),g.setColor( new Color( (int) (Math.random( )*256),

(int) (Math.random( )*256),(int) (Math.random( )*256), (int) (Math.random( )*256) ));(int) (Math.random( )*256) ));

g.drawString( textV, 20, 0);g.drawString( textV, 20, 0);}}

Animated appletsAnimated applets

Create a thread for an instance of the Create a thread for an instance of the appletapplet

paint( )paint( ) the applet into the applet the applet into the applet contextcontext

Each time the relevant graphical Each time the relevant graphical properties of the applet are changed, properties of the applet are changed, repaint( )repaint( ) the applet the applet

Applet LifecycleApplet Lifecycle

init()init() is invoked when the applet is is invoked when the applet is loadedloaded

start()start() is invoked when the page is invoked when the page containing the applet has been enteredcontaining the applet has been entered

stop()stop() is invoked when the page is left is invoked when the page is left destroy()destroy() is invoked when the page is is invoked when the page is

discardeddiscarded

Font03.javaFont03.java

import java.awt.*;import java.awt.*;import java.applet.*;import java.applet.*;

public class Font03 extends Applet public class Font03 extends Applet implements Runnableimplements Runnable { { protected Thread paintThread = null;protected Thread paintThread = null; protected String tmpSt, textV;protected String tmpSt, textV; protected int sizeV;protected int sizeV; protected double rotation=0.0;protected double rotation=0.0;

public void init( ) { // as beforepublic void init( ) { // as before}}

}}

starting and stoppingstarting and stopping

public void start( ) {public void start( ) {if (paintThread == null) {if (paintThread == null) { paintThread = new Thread(this);paintThread = new Thread(this); paintThread.start( );paintThread.start( );}}

}}

public void stop( ) {public void stop( ) {paintThread = null;paintThread = null;

}}

running the appletrunning the applet

public void run( ) {public void run( ) {while (Thread.currentThread( ) == paintThread) {while (Thread.currentThread( ) == paintThread) { repaint( );repaint( ); try {try {

Thread.currentThread( ).sleep(100);Thread.currentThread( ).sleep(100);rotation += Math.PI/8.0;rotation += Math.PI/8.0;

}} catch(InterruptedException e) { }catch(InterruptedException e) { }}}

}}

public void public void paint(Graphics gg) {paint(Graphics gg) {

int ii;int ii;

Graphics2D g = (Graphics2D) gg;Graphics2D g = (Graphics2D) gg;

g.setFont( new Font("Arial", Font.BOLD, sizeV) );g.setFont( new Font("Arial", Font.BOLD, sizeV) );

g.translate(200, 200);g.translate(200, 200);

g.rotate(rotation);g.rotate(rotation);

g.setColor( new Color( (int) (Math.random( )*256),g.setColor( new Color( (int) (Math.random( )*256),

(int) (Math.random( )*256),(int) (Math.random( )*256),

(int) (Math.random( )*256) ));(int) (Math.random( )*256) ));

g.drawString( textV, 20, 0);g.drawString( textV, 20, 0);

}}