Download presentation
Presentation is loading. Please wait.
Published byDaisy Spencer Modified over 9 years ago
1
Internet Software Development Applets Paul J Krause
2
Contents Applet Basics Passing parameters to applets Simple graphics operations Animating applets
3
Hello.java // Filename: Hello.java 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”); }}
4
Applets Java applications are executed from the command line Applets are small applications that can be embedded in html pages They can then be downloaded via the world wide web and invoked in a browser
5
Template for an Applet Subclass one of the Java foundation classes: java.applet.Applet; java.applet.Applet; javax.swing.JApplet; javax.swing.JApplet; It’s behaviour is invoked in a “paint” method, not a “main” method
6
// File: MyApplet.java import java.awt.*; import java.applet.Applet; public class MyApplet extends Applet { public void paint(Graphics g) { public void paint(Graphics g) { // Do something interesting }}
7
Viewing Area of Applet y x(0,0) height width These dimensions are set in the HTML file
8
Contract for the paint method Dimensions of the region on the Web page assigned to the applet are set within an tag in an HTML file The paint( ) method defines the appearance of the applet within this rectangular region The paint( ) method is invoked when the applet is initially loaded in the browser (or applet viewer)
9
HelloFromVenus.java import java.awt.*; import java.applet.Applet; public class HelloFromVenus extends Applet { public void paint(Graphics g) { Dimension d = getSize( ); // Gets the size of the rectangle allocated to an // instance of this applet }}
10
painting the background import java.awt.*; import java.applet.Applet; public class HelloFromVenus extends Applet { public void paint(Graphics g) { Dimension d = getSize( ); g.setColor(Color.black); g.fillRect(0, 0, d.width, d.height); }}
11
painting the text public void paint(Graphics g) { Dimension d = getSize( ); g.setColor(Color.black); g.fillRect(0, 0, d.width, d.height); // set font style and size:- g.setFont(new Font(“Sans-serif”, Font.BOLD, 24); // change colour on paint brush:- g.setColor(255, 215, 0); // note different way! g.drawString(“Hello from Venus!”, 40, 25); }
12
finally add the image public void paint(Graphics g) { Dimension d = getSize( ); g.setColor(Color.black); g.fillRect(0, 0, d.width, d.height); g.setFont(new Font(“Sans-serif”, Font.BOLD, 24); g.setColor(255, 215, 0); g.drawString(“Hello from Venus!”, 40, 25); g.drawImage(getImage(getCodeBase( ), “Venus.gif”, 20, 60, this); }
13
Embedding Applets in HTML < applet code = bytecode-filename width = pixels height = pixels > </applet>
14
<HTML><HEAD> HelloFromVenus Applet </HEAD> <CENTER> Here is the Hello From Venus Applet Here is the Hello From Venus Applet </CENTER> Venus photo courtesy of NASA. </BODY>
15
Parameter passing To make applets reusable in different contexts, we need to be able to pass parameters from the html files Can do this by using: tag in html, and tag in html, and getParameter(String) method in the applet getParameter(String) method in the applet
16
in the html file < applet code = bytecode-filename width = pixels height = pixels > …</applet>
17
in the applet String input = getParameter(String);
18
Example In the html file: In the applet textV = getParameter(“text”); textV = getParameter(“text”);
19
Initialisation of an applet Initialisation of an applet is not done in the Constructor method, but in an init( ) method Why? An applet context interprets the tag in an html file The applet context first constructs an instance of the applet, then interprets the remaining parameters in the tag The init( ) method is invoked after the information in the tag has been processed
20
import java.awt.*; import java.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"); tmpSt = getParameter("size"); sizeV = Integer.parseInt(tmpSt) } public void paint(Graphics gg) { public void paint(Graphics gg) { … } }
21
<html><head> Font Rotation and Translation Font Rotation and Translation </head> Font Rotation and Translation Font Rotation and Translation </body></html>
22
shifting graphical objects To translate a graphical object, use: translate(xpos, ypos); translate(xpos, ypos); To rotate a graphical object, use: rotate(angle_in_radians); rotate(angle_in_radians);
23
public void paint(Graphics gg) { public void paint(Graphics gg) { int ii; Graphics2D g = (Graphics2D) gg; g.setFont( new Font("Arial", Font.BOLD, sizeV) ); g.setColor(Color.blue) g.translate(200, 200); 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);}
24
public void paint(Graphics gg) { public void paint(Graphics gg) { int ii; Graphics2D g = (Graphics2D) gg; g.setFont( new Font("Arial", Font.BOLD, sizeV) ); g.translate(200, 200); 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);}
25
Animated applets Create a thread for an instance of the applet paint( ) the applet into the applet context Each time the relevant graphical properties of the applet are changed, repaint( ) the applet
26
Applet Lifecycle init() is invoked when the applet is loaded start() is invoked when the page containing the applet has been entered stop() is invoked when the page is left destroy() is invoked when the page is discarded
27
Font03.java import java.awt.*; import java.applet.*; public class Font03 extends Applet implements 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 before public void init( ) { // as before}}
28
starting and stopping public void start( ) { if (paintThread == null) { paintThread = new Thread(this); paintThread = new Thread(this); paintThread.start( ); paintThread.start( );}} public void stop( ) { paintThread = null; }
29
running the applet public void run( ) { while (Thread.currentThread( ) == paintThread) { repaint( ); repaint( ); try { try { Thread.currentThread( ).sleep(100); rotation += Math.PI/8.0; } catch(InterruptedException e) { } catch(InterruptedException e) { }}}
30
public void paint(Graphics gg) { public void paint(Graphics gg) { int ii; Graphics2D g = (Graphics2D) gg; g.setFont( new Font("Arial", Font.BOLD, sizeV) ); g.translate(200, 200); g.rotate(rotation); 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); }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.