Download presentation
Presentation is loading. Please wait.
Published byAlbert Wright Modified over 9 years ago
1
Applets
2
Internet/WWW How does it work?
3
Internet/WWW
4
Internet/WWW Client-server model HTML –HyperText Markup Language –Text and markup text
5
Simplest HTML document <html><head> My document’s title My document’s title </head><body> Hello, world! </body></html>
6
Simplest HTML document <html><head> My document’s title My document’s title </head><body> Hello, world! </body></html> start tag end tag tag’s content
7
Tags w/out content Some tags don’t have any content. Therefore they do not require separate start and end tags. Example (image tag):
8
HTML tags This is an example of a paragraph tag. attribute
9
HTML tags This is an example of a paragraph tag. attribute name attribute value
10
What do web pages contain? ImagesTextSoundApplets We saw how we can embed images and text in web pages. How can we embed an applet?
11
Applets <html> my applet my applet <body> here is my applet: here is my applet: <appletcode=“MyApplet.class” width=“300” height=“200”> Your browser may not be able to display applets! </applet></body></html>
12
MyApplet.java //----------------------------------------------------------------------/* file : MyApplet.java file : MyApplet.java date : 16-nov-2004 date : 16-nov-2004 author: george j. grevera, ph.d. author: george j. grevera, ph.d. desc. : this applet randomly displays a 'hello' message. desc. : this applet randomly displays a 'hello' message. note : i noticed that 'refresh' (in Netscape) doesn't actually reload note : i noticed that 'refresh' (in Netscape) doesn't actually reload the applet so any changes will be ignored. you have to actually the applet so any changes will be ignored. you have to actually close all of the browser windows and start over! close all of the browser windows and start over! here is a sample html document that contains this applet: here is a sample html document that contains this applet: <html> The MyApplet applet The MyApplet applet Here is the MyApplet applet Here is the MyApplet applet Your browser may not be able to display applets! </html>*/
13
MyApplet.java import java.awt.Color; import java.awt.Color; import java.awt.event.ActionEvent; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.ActionListener; import java.awt.Graphics; import java.awt.Graphics; import java.util.Random; import java.util.Random; import javax.swing.JApplet; import javax.swing.JApplet; import javax.swing.Timer; import javax.swing.Timer;//----------------------------------------------------------------------
14
MyApplet.java public class MyApplet extends JApplet implements ActionListener implements ActionListener { //instance variables //instance variables final int width = 300; final int width = 300; final int height = 200; final int height = 200; int count = 0; int count = 0; Timer t = new Timer( 200, this ); //time in milliseconds Timer t = new Timer( 200, this ); //time in milliseconds Random r = new Random(); Random r = new Random(); // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - //this method is called once by the "browser" to allow the applet to //this method is called once by the "browser" to allow the applet to // initialize itself. // initialize itself. public void init ( ) { public void init ( ) { t.start(); t.start(); } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - //our applet needs to respond to timer events. therefore, our applet //our applet needs to respond to timer events. therefore, our applet // implements the ActionListener interface. this method is called by // implements the ActionListener interface. this method is called by // the timer every time that our timer goes off. it simply asks our // the timer every time that our timer goes off. it simply asks our // applet to repaint itself. // applet to repaint itself. public void actionPerformed ( ActionEvent e ) { public void actionPerformed ( ActionEvent e ) { repaint(); repaint(); } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
15
MyApplet.java // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - //this function 'paints' the contents of the applet window area. //this function 'paints' the contents of the applet window area. // // //graphical functions are described further in: //graphical functions are described further in: // http://java.sun.com/j2se/1.5.0/docs/api/java/awt/Graphics.html // http://java.sun.com/j2se/1.5.0/docs/api/java/awt/Graphics.html public void paint ( Graphics g ) { public void paint ( Graphics g ) { g.setColor( Color.yellow ); g.setColor( Color.yellow ); g.fillRect( 0, 0, width, height ); g.fillRect( 0, 0, width, height ); int x = r.nextInt( width ); int x = r.nextInt( width ); int y = r.nextInt( height ); int y = r.nextInt( height ); g.setColor( Color.black ); g.setColor( Color.black ); g.drawString( "hello", x, y ); g.drawString( "hello", x, y ); showStatus( "paint: frame number " + count ); showStatus( "paint: frame number " + count ); count++; count++; } }//----------------------------------------------------------------------
16
MyApplet.java complete code //----------------------------------------------------------------------/* file : MyApplet.java file : MyApplet.java date : 16-nov-2004 date : 16-nov-2004 author: george j. grevera, ph.d. author: george j. grevera, ph.d. desc. : this applet randomly displays a 'hello' message. desc. : this applet randomly displays a 'hello' message. note : i noticed that 'refresh' (in Netscape) doesn't actually reload note : i noticed that 'refresh' (in Netscape) doesn't actually reload the applet so any changes will be ignored. you have to actually the applet so any changes will be ignored. you have to actually close all of the browser windows and start over! close all of the browser windows and start over! here is a sample html document that contains this applet: here is a sample html document that contains this applet: <html> The MyApplet applet The MyApplet applet Here is the MyApplet applet Here is the MyApplet applet Your browser may not be able to display applets! </html>*/ import java.awt.Color; import java.awt.Color; import java.awt.event.ActionEvent; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.ActionListener; import java.awt.Graphics; import java.awt.Graphics; import java.util.Random; import java.util.Random; import javax.swing.JApplet; import javax.swing.JApplet; import javax.swing.Timer; import javax.swing.Timer;//---------------------------------------------------------------------- public class MyApplet extends JApplet public class MyApplet extends JApplet implements ActionListener implements ActionListener { //instance variables //instance variables final int width = 300; final int width = 300; final int height = 200; final int height = 200; int count = 0; int count = 0; Timer t = new Timer( 200, this ); //time in milliseconds Timer t = new Timer( 200, this ); //time in milliseconds Random r = new Random(); Random r = new Random(); // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - //this method is called once by the "browser" to allow the applet to //this method is called once by the "browser" to allow the applet to // initialize itself. // initialize itself. public void init ( ) { public void init ( ) { t.start(); t.start(); } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - //our applet needs to respond to timer events. therefore, our applet //our applet needs to respond to timer events. therefore, our applet // implements the ActionListener interface. this method is called by // implements the ActionListener interface. this method is called by // the timer every time that our timer goes off. it simply asks our // the timer every time that our timer goes off. it simply asks our // applet to repaint itself. // applet to repaint itself. public void actionPerformed ( ActionEvent e ) { public void actionPerformed ( ActionEvent e ) { repaint(); repaint(); } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - //this function 'paints' the contents of the applet window area. //this function 'paints' the contents of the applet window area. // // //graphical functions are described further in: //graphical functions are described further in: // http://java.sun.com/j2se/1.5.0/docs/api/java/awt/Graphics.html // http://java.sun.com/j2se/1.5.0/docs/api/java/awt/Graphics.html public void paint ( Graphics g ) { public void paint ( Graphics g ) { g.setColor( Color.yellow ); g.setColor( Color.yellow ); g.fillRect( 0, 0, width, height ); g.fillRect( 0, 0, width, height ); int x = r.nextInt( width ); int x = r.nextInt( width ); int y = r.nextInt( height ); int y = r.nextInt( height ); g.setColor( Color.black ); g.setColor( Color.black ); g.drawString( "hello", x, y ); g.drawString( "hello", x, y ); showStatus( "paint: frame number " + count ); showStatus( "paint: frame number " + count ); count++; count++; } }//----------------------------------------------------------------------
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.