Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Internet Software Development Applets Paul J Krause Contents Applet Basics Passing parameters to applets Simple graphics operations Animating applets Hello.java // Filename: Hello.java public class Hello { public static void main(String args) { System.out.println(“Hello fom Venus”); } } 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 Template for an Applet Subclass one of the Java foundation classes: java.applet.Applet; javax.swing.JApplet; It’s behaviour is invoked in a “paint” method, not a “main” method // File: MyApplet.java import java.awt.*; import java.applet.Applet; public class MyApplet extends Applet { public void paint(Graphics g) { // Do something interesting } } Viewing Area of Applet (0,0) x These dimensions are set in the HTML file height y width Contract for the paint method Dimensions of the region on the Web page assigned to the applet are set within an <applet> 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) 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 } } 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); } } 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); } 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); } Embedding Applets in HTML < applet code = bytecode-filename width = pixels height = pixels > </applet> <HTML> <HEAD> <TITLE> HelloFromVenus Applet </TITLE> </HEAD> <BODY BGCOLOR=BLACK TEXT=white> <CENTER> <H2>Here is the <EM>Hello From Venus</EM> Applet</H2> <APPLET CODE="HelloFromVenus.class" WIDTH=300 HEIGHT=350> </APPLET> </CENTER> Venus photo courtesy of NASA. </BODY> </HTML> 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: <param name> tag in html, and getParameter(String) method in the applet in the html file < applet code = bytecode-filename width = pixels height = pixels > <param name=String value=String> … </applet> in the applet String input = getParameter(String); Example In In the html file: <param name=“text” value=“Web Technology”> the applet textV = getParameter(“text”); 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 <applet> tag in an html file The applet context first constructs an instance of the applet, then interprets the remaining parameters in the <applet> tag The init( ) method is invoked after the information in the <applet> tag has been processed import java.awt.*; import java.applet.*; public class Font02 extends Applet { String tmpSt, textV; int sizeV; public void init( ) { textV = getParameter("text"); tmpSt = getParameter("size"); sizeV = Integer.parseInt(tmpSt) } public void paint(Graphics gg) { …} } <html> <head> <title>Font Rotation and Translation</title> </head> <body style="font-size:18pt; text-align:center;font-weight:bold"> Font Rotation and Translation<br /><br /> <applet code="Font02.class" width=450 height=400> <param name="text" value="....Web Technology"> <param name="size" value="18"> </applet> </body> </html> shifting graphical objects To translate(xpos, ypos); To translate a graphical object, use: rotate a graphical object, use: rotate(angle_in_radians); 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.drawString( textV, 20, 0); } 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.setColor( new Color( (int) (Math.random( )*256), (int) (Math.random( )*256), (int) (Math.random( )*256) )); g.drawString( textV, 20, 0); } 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 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 Font03.java import java.awt.*; import java.applet.*; public class Font03 extends Applet implements Runnable { protected Thread paintThread = null; protected String tmpSt, textV; protected int sizeV; protected double rotation=0.0; public void init( ) { // as before } } starting and stopping public void start( ) { if (paintThread == null) { paintThread = new Thread(this); paintThread.start( ); } } public void stop( ) { paintThread = null; } running the applet public void run( ) { while (Thread.currentThread( ) == paintThread) { repaint( ); try { Thread.currentThread( ).sleep(100); rotation += Math.PI/8.0; } catch(InterruptedException e) { } } } 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) )); g.drawString( textV, 20, 0); }