APPLETS CSC 171 FALL 2004 LECTURE 6 APPLETS

  • Slides: 27
Download presentation
APPLETS CSC 171 FALL 2004 LECTURE 6

APPLETS CSC 171 FALL 2004 LECTURE 6

APPLETS l Graphical Java programs l Run inside web browser l Platform-neutral l Easy

APPLETS l Graphical Java programs l Run inside web browser l Platform-neutral l Easy deployment--loads when needed l Secure

Applets l Applets are Java prgrams that can be embedded in Hypertext Markup Language

Applets l Applets are Java prgrams that can be embedded in Hypertext Markup Language (HTML) documents l The browser that executes an applet is generically knows as the applet container l So, often we use appletviewer

How Applets Work

How Applets Work

HTML Text and tags: Java is an <i>object-oriented</i> programming language l Browser renders the

HTML Text and tags: Java is an <i>object-oriented</i> programming language l Browser renders the tags: Java is an object-oriented programming language l Bulleted list (like this one) is defined by tags <ul> <li>. . . </li> </ul> l Use < > for <> symbols l

Simple HTML <html> <head> <title> Ted Pawlicki's CSC 171 HTML</title> </head> <body> Hello Ted!

Simple HTML <html> <head> <title> Ted Pawlicki's CSC 171 HTML</title> </head> <body> Hello Ted! </applet> </html>

Applet Methods l Applets have no “main()” l Rather, it has key methods that

Applet Methods l Applets have no “main()” l Rather, it has key methods that deal with the unique situation of applets

Key Applet Methods l init l start l paint l stop l destroy

Key Applet Methods l init l start l paint l stop l destroy

Key Applet Methods l public void init() l public void start() l public void

Key Applet Methods l public void init() l public void start() l public void paint(Graphics g) l public void stop() l public void destroy()

public void init() l Called once by appletviewer or browser l Called when an

public void init() l Called once by appletviewer or browser l Called when an applet is loaded l Performs initialization – Instance variables – GUI components – Loading sounds & images (multimedia) – Creation of threads (animation)

public void start() l Called after the init method completes l Called every time

public void start() l Called after the init method completes l Called every time the user of the browser returns to the HTML page l Performs tasks that must be repeated when the applet is revisited – Restarting an animation

public void paint(Graphics g) l Called after init() method completes and start() has started

public void paint(Graphics g) l Called after init() method completes and start() has started l “Draws stuff” on the applet l The system hands the applet a Graphics object to draw stuff on l Automatically recalled whenever the applet needs to be repainted – uncovering a covered window

public void stop() l Called when the applet should stop executing – When the

public void stop() l Called when the applet should stop executing – When the user leaves the HTML page l Perform tasks necessary to suspend applet – “pausing” animations

public void destroy() l Called when the applet is removed from memory l Performs

public void destroy() l Called when the applet is removed from memory l Performs tasks to de-allocate resources

repaint() l l l Paint is normally called by the applet container We sometimes

repaint() l l l Paint is normally called by the applet container We sometimes change the applet’s appearance based on user input We might like to call paint() directly However, in order to call paint, we have to pass it a Graphics object But the container owns the Graphics object repaint() invokes update() which invokes paint with the appropriate graphics object

Some HTML <html> <head> <title> Ted Pawlicki's CSC 171 First Applet</title> </head> <body> <applet

Some HTML <html> <head> <title> Ted Pawlicki's CSC 171 First Applet</title> </head> <body> <applet code=“First. Applet. class" width = 256 height = 550 > </applet> </html>

Some Java import java. awt. *; import java. applet. Applet; public class First. Applet

Some Java import java. awt. *; import java. applet. Applet; public class First. Applet extends Applet { public void paint(Graphics g) { g. draw. String(“Hello Ted!", 20, 40); } }

Cooler Graphics class My. Applet extends Applet { public void paint(Graphics g){ Graphics 2

Cooler Graphics class My. Applet extends Applet { public void paint(Graphics g){ Graphics 2 D g 2 = (Graphics 2 D)g; // add drawing operations. . . } }

Graphical Shapes l Shape classes Ellipse 2 D. Double, Line 2 D. Double, etc.

Graphical Shapes l Shape classes Ellipse 2 D. Double, Line 2 D. Double, etc. l import java. awt. geom. Ellipse 2 D; // no. Double l Must construct and draw the shape Ellipse 2 D. Double easter. Egg = new Ellipse 2 D. Double(5, 10, 15, 20); g 2. draw(easter. Egg)

Ellipse

Ellipse

Lines l Line 2 D. Double segment = new Line 2 D. Double(x 1,

Lines l Line 2 D. Double segment = new Line 2 D. Double(x 1, x 2, y 1, y 2); l More object-oriented to use Point 2 D. Double for the end points: Point 2 D. Double from = new Point 2 D. Double(x 1, y 1); Point 2 D. Double to = new Point 2 D. Double(x 2, y 2); Line 2 D. Double segment = new Line 2 D. Double(from, to); l Draw thick lines: g 2. set. Stroke(new Basic. Stroke(4. 0 F)); // 4 pixels

Colors l Specify red, green, blue between 0. 0 F and 1. 0 F

Colors l Specify red, green, blue between 0. 0 F and 1. 0 F Color magenta = new Color(1. 0 F, 0. 0 F, 1. 0 F) Standard colors Color. black Color. yellow Color. pink. . . l Set color in graphics context: g 2. set. Color(Color. pink); l l Then draw or fill shapes g 2. fill(easter. Egg);

FONTS Specify text and base point: g 2. draw. String("Applet", 50, 100); l Font

FONTS Specify text and base point: g 2. draw. String("Applet", 50, 100); l Font object has l – face name (Serif, Sans. Serif, Monospaced, . . . ) – style (Font. PLAIN, Font. BOLD, Font. ITALIC ) – point size (12 point = normal size) l g 2. set. Font(new Font("Serif", Font. BOLD, 36));

Baseline

Baseline

“Local” or “Relative” Coordinate System

“Local” or “Relative” Coordinate System

The whole object has a location public Car(double x, double y){ x. Left =

The whole object has a location public Car(double x, double y){ x. Left = x; y. Top = y; }

The object is drawn “relative” to the whole object’s location public void draw(Graphics 2

The object is drawn “relative” to the whole object’s location public void draw(Graphics 2 D g 2) { Rectangle 2 D. Double body = new Rectangle 2 D. Double(x. Left, y. Top+10, 60, 10); … Ellipse 2 D. Double rear. Tire = new Ellipse 2 D. Double(x. Left + 40, y. Top + 20, 10); …. .