csc 308 2.0 system development with java introduction to ... · what is an applet? • applet: a...

23
CSC 308 2.0 System Development with Java Budditha Hettige Department of Statistics and Computer Science Introduction to Java Applets

Upload: others

Post on 02-Mar-2020

4 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CSC 308 2.0 System Development with Java Introduction to ... · What is an applet? • applet: a Java program that can be inserted into a web page and run by loading that page in

CSC 308 2.0

System Development with Java

Budditha Hettige

Department of Statistics and Computer Science

Introduction to

Java Applets

Page 2: CSC 308 2.0 System Development with Java Introduction to ... · What is an applet? • applet: a Java program that can be inserted into a web page and run by loading that page in

What is an applet?

• applet: a Java program that can be inserted into a

web page and run by loading that page in a browser

– Brings web pages to life with interactive content,

multimedia, games, and more

– The feature of Java that is primarily responsible for its initial

popularity

– Users can run applets simply by

visiting a web page that

contains an applet program

(if they have the Java runtime

environment installed on their

computer)

Budditha Hettige 2

Page 3: CSC 308 2.0 System Development with Java Introduction to ... · What is an applet? • applet: a Java program that can be inserted into a web page and run by loading that page in

Applet classes in Java

• implementation

– a top-level container, like a JFrame

– behaves more like a JPanel

– class javax.swing.JApplet

java.lang.Object

• java.awt.Component

– java.awt.Container

• java.awt.Panel

– java.applet.Applet

» javax.swing.JApplet

Budditha Hettige 3

Page 4: CSC 308 2.0 System Development with Java Introduction to ... · What is an applet? • applet: a Java program that can be inserted into a web page and run by loading that page in

Applets contd.

• Program that runs in

– appletviewer (test utility for applets)

– Web browser (IE, Communicator)

• Executes when HTML (Hypertext Markup

Language) document containing applet is

opened and downloaded

• Applications run in command windows

Budditha Hettige 4

Page 5: CSC 308 2.0 System Development with Java Introduction to ... · What is an applet? • applet: a Java program that can be inserted into a web page and run by loading that page in

Sample Program

import java.awt.Graphics; // import class Graphics import javax.swing.JApplet; // import class JApplet public class WelcomeApplet extends JApplet { // draw text on applet’s background

public void paint( Graphics g ) { // call superclass version of method paint super.paint( g ); // draw a String at x-coordinate 25 and y-coordinate g.drawString( "Welcome to Java Programming!", 25, 25 ); } // end method paint } // end class WelcomeApplet

Budditha Hettige 5

Page 6: CSC 308 2.0 System Development with Java Introduction to ... · What is an applet? • applet: a Java program that can be inserted into a web page and run by loading that page in

Running the applet

• Compile – javac WelcomeApplet.java – If no errors, bytecodes stored in WelcomeApplet.class

• Create an HTML file – Loads the applet into appletviewer or a

browser

– Ends in .htm or .html

• To execute an applet – Create an HTML file indicating which applet the

browser (or appletviewer) should load and execute

Budditha Hettige 6

Page 7: CSC 308 2.0 System Development with Java Introduction to ... · What is an applet? • applet: a Java program that can be inserted into a web page and run by loading that page in

Create a HTML file

• Simple HTML file (WelcomeApplet.html) – Usually in same directory as .class file

– Remember, .class file created after compilation

• HTML codes (tags) – Usually come in pairs

– Begin with < and end with >

• Lines 1 and 4 - begin and end the HTML tags

• Line 2 - begins <applet> tag – Specifies code to use for applet

– Specifies width and height of display area in pixels

• Line 3 - ends <applet> tag

1 <html> 2 <applet code = "WelcomeApplet.class" width = "300" height = "45"> 3 </applet> 4 </html>

Budditha Hettige 7

Page 8: CSC 308 2.0 System Development with Java Introduction to ... · What is an applet? • applet: a Java program that can be inserted into a web page and run by loading that page in

Execute an applet

• appletviewer only understands

<applet> tags

– Ignores everything else

– Minimal browser

• Executing the applet

– appletviewer WelcomeApplet.html

– Perform in directory containing .class file

Budditha Hettige 8

Page 9: CSC 308 2.0 System Development with Java Introduction to ... · What is an applet? • applet: a Java program that can be inserted into a web page and run by loading that page in

The paint method

• paint needs to be told where on the screen it can

draw

– This will be the only parameter it needs

• paint doesn’t return any result

• A Graphics (short for ―Graphics context‖) is an object

that holds information about a painting

– It remembers what color you are using

– It remembers what font you are using

– You can ―paint‖ on it (but it doesn’t remember

what you have painted)

Budditha Hettige 9

Page 10: CSC 308 2.0 System Development with Java Introduction to ... · What is an applet? • applet: a Java program that can be inserted into a web page and run by loading that page in

Colors

• The java.awt package defines a class named Color

• There are 13 predefined colors—here are their fully-qualified names:

Color.BLACK Color.PINK Color.GREEN

Color.DARK_GRAY Color.RED Color.CYAN

Color.GRAY Color.ORANGE Color.BLUE

Color.LIGHT_GRAY Color.YELLOW

Color.WHITE Color.MAGENTA

Budditha Hettige 10

Page 11: CSC 308 2.0 System Development with Java Introduction to ... · What is an applet? • applet: a Java program that can be inserted into a web page and run by loading that page in

Colors

• New Colors

– Every color is a mix of red, green, and blue

– You can make your own colors: new Color( red , green , blue )

– Amounts range from 0 to 255

– Black is (0, 0, 0), white is (255, 255, 255)

– We are mixing lights, not pigments

– Yellow is red + green, or (255, 255, 0)

• Setting a color

– To use a color, we tell our Graphics g what color we want:

g.setColor(Color.RED);

– g will remember this color and use it for everything until we tell it some different color

Budditha Hettige 11

Page 12: CSC 308 2.0 System Development with Java Introduction to ... · What is an applet? • applet: a Java program that can be inserted into a web page and run by loading that page in

Pixels

• A pixel is a picture (pix) element

– one pixel is one dot on your screen

– there are typically 72 to 90 pixels per inch

• java.awt measures everything in pixels

Budditha Hettige 12

Page 13: CSC 308 2.0 System Development with Java Introduction to ... · What is an applet? • applet: a Java program that can be inserted into a web page and run by loading that page in

Java’s coordinate system

• Java uses an (x, y) coordinate system

• (0, 0) is the top left corner

• (50, 0) is 50 pixels to the right of (0, 0)

• (0, 20) is 20 pixels down from (0, 0)

• (w - 1, h - 1) is just inside the bottom right corner, where w is the width of the window and h is its height

(0, 0)

(0, 20)

(50, 0)

(50, 20)

(w-1, h-1)

Budditha Hettige 13

Page 14: CSC 308 2.0 System Development with Java Introduction to ... · What is an applet? • applet: a Java program that can be inserted into a web page and run by loading that page in

Drawing rectangles

• There are two ways to draw rectangles:

– g.drawRect( left , top , width , height );

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

Budditha Hettige 14

Page 15: CSC 308 2.0 System Development with Java Introduction to ... · What is an applet? • applet: a Java program that can be inserted into a web page and run by loading that page in

Sample Applet

import java.applet.Applet;

import java.awt.*;

public class Drawing extends Applet

{

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

}

}

Budditha Hettige 15

Page 16: CSC 308 2.0 System Development with Java Introduction to ... · What is an applet? • applet: a Java program that can be inserted into a web page and run by loading that page in

More java.awt methods

• g.drawLine( x1 , y1 , x2 , y2 );

• g.drawOval( left , top , width , height );

• g.fillOval( left , top , width , height );

• g.drawRoundRect( left , top , width , height );

• g.fillRoundRect( left , top , width , height );

• g.drawArc( left , top , width , height ,

startAngle , arcAngle );

• g.drawString( string , x , y );

Budditha Hettige 16

Page 17: CSC 308 2.0 System Development with Java Introduction to ... · What is an applet? • applet: a Java program that can be inserted into a web page and run by loading that page in

Applet Methods

• public void init ()

• public void start ()

• public void stop ()

• public void destroy ()

• public void paint (Graphics)

• public void repaint()

• public void update (Graphics)

• public void showStatus(String)

• public String getParameter(String)

destroy()

init()

start()

stop()

java.applet.Applet

java.awt.Panel

Budditha Hettige 17

Page 18: CSC 308 2.0 System Development with Java Introduction to ... · What is an applet? • applet: a Java program that can be inserted into a web page and run by loading that page in

Applet Method contd.

import java.applet.*;

import java.awt.*;

public class BasicApplet extends Applet {

public void init() { // Called once by the browser when

// it starts the applet.

}

public void start() { // Called whenever the page containing

// this applet is made visible.

}

public void stop() { // Called whenever the page containing this

// applet is not visible.

}

public void destroyed() { // Called once when the browser

destroys

// this applet.

}

public void paint(Graphics g) {

// Called whenever this applet needs to

// repaint itself.

}

}

Budditha Hettige 18

Page 19: CSC 308 2.0 System Development with Java Introduction to ... · What is an applet? • applet: a Java program that can be inserted into a web page and run by loading that page in

The Life-Cycle of Applet

init() Called exactly once in an applet’s life.

Called when applet is first loaded, which is after object creation, e.g., when the browser visits the web page for the first time.

Used to read applet parameters, start downloading any other images or media files, etc.

start() Called at least once.

Called when an applet is started or restarted, i.e., whenever the browser visits the web page.

stop() Called at least once.

Called when the browser leaves the web page.

Budditha Hettige 19

Page 20: CSC 308 2.0 System Development with Java Introduction to ... · What is an applet? • applet: a Java program that can be inserted into a web page and run by loading that page in

Applet Life-Cycle (Cont.)

destroy()

Called exactly once.

Called when the browser unloads the applet.

Used to perform any final clean-up.

init

destroy start stop

start

Budditha Hettige 20

Page 21: CSC 308 2.0 System Development with Java Introduction to ... · What is an applet? • applet: a Java program that can be inserted into a web page and run by loading that page in

Passing parameter to applet

• Parameters can be placed in the HTML code of your web page, which your applet can read:

<APPLET code="mypackage/MyApplet.class" width=400 height=300>

<PARAM name="password" value="Tacoma"> </PARAM>

</APPLET>

• Methods in the JApplet class:

– public String getParameter(String name)

Returns the value of the parameter with the given name. String password = this.getParameter("password");

– public String[][] getParameterInfo()

Returns an array of all parameter names, descriptions, and values.

Budditha Hettige 21

Page 22: CSC 308 2.0 System Development with Java Introduction to ... · What is an applet? • applet: a Java program that can be inserted into a web page and run by loading that page in

Applet on NetBeans

Budditha Hettige 22

Page 23: CSC 308 2.0 System Development with Java Introduction to ... · What is an applet? • applet: a Java program that can be inserted into a web page and run by loading that page in

Sample Programs

• C:/Program Files/Java/jdk1.6.0_23/demo/applets/

Budditha Hettige 23