180 likes | 326 Views
Applets & Graphics. Applets. programs that run inside a browser Java platform-independence makes applets possible security restrictions: cannot read or write to local disk cannot print can only access the server that delivered them signed applets compatibility issues
E N D
Applets • programs that run inside a browser • Java platform-independence makes applets possible • security restrictions: • cannot read or write to local disk • cannot print • can only access the server that delivered them • signed applets • compatibility issues • JDK built-in to browser (still at JDK1.1) • Java plug-in may be installed into browser • Java must be enabled in browser • download speed
HTML • A page-definition language for browsers • applets must be invoked via an HTML page <html> <head><title>My Applet</title></head> <body> Here is my first applet: <applet code=“MyApplet.class” width=200 height=200> MyApplet here </applet> </body> </html>
Creating an Applet • An applet must extend the Applet class public class MyApplet extendsApplet • this allows it to inherit the hooks which enable it to run in a browser • applets inherit a method: public void paint(Graphics g) from a superclass; • a browser will call an applet’s paint() method • in this case, the inherited method does nothing; but we may override it to draw a picture in our applet • the browser creates and supplies a Graphics object • the Graphics object has methods for drawing
Some Methods of the Graphics Class • void drawString (String s, int x, int y) • void drawRect (int x, int y, int width, int height) • void drawOval (int x, int y, int width, int height) • void drawLine (int x1, int y1, int x2, int y2) • void fillRect (int x, int y, int width, int height) • void fillOval (int x, int y, int width, int height) • void drawImage(Image img, int x, int y, ImageObserver observer) • void setColor (Color c) • void setFont (Font f)
Coordinate System • Graphics uses an integer coordinate system with origin at top left corner of the applet 0,0 x y width, height
Color Class • Constructor (values from 0 - 255) public Color (int red, int green, int blue) • Examples Color red = new Color (255, 0, 0); Color purple = new Color (255, 0, 255); Color whatever = new Color (25, 128, 200); • Predefined colors Color.lightGray Color.magenta Color.white Color.blue, etc.
Font Class • Constructor public Font (String name, int style, int size) • Names • “TimesRoman” • “Arial” • “Courier” • “Impact” • Logical names • “serif” // TimesRoman • “sanserif” // Helvetica • “monospaced” // Courier
Font Class (continued) • Styles • Font.BOLD • Font.ITALIC • Font.PLAIN • Font.BOLD+Font.ITALIC • Sizes (in points, 1/72 inch) • 8 // pretty small • 12 // readable • 20 // large
Font Class (continued) • Examples • Font normal = new Font (“serif”, Font.PLAIN, 12); • Font emphatic = new Font (“Helvetica”, Font.BOLD+ Font.ITALIC, 24); • Font program = new Font (“monospaced”, Font.PLAIN, 12);
import java.awt.*;import java.applet.*;public class SampleApplet extends Applet { public void paint(Graphics g) { Font f1 = new Font (“serif”, Font.PLAIN, 14); g.setFont (f1); g.drawString (“Let’s try some things”, 20, 20); g.setColor (new Color (100, 200, 100)); g.drawRect (5, 40, 50, 30); g.setColor (Color.green); g.fillRect (30, 60, 30, 50); }}
Other Methods of Applet • paint(Graphics g) is called whenever the applet needs repainting • the Applet class has other methods that may be overridden: • public void init() // called once,when the applet is loaded • public void start() // called whenever the applet’s page is viewed • public void stop() // called whenever the applet’s page is exited • public void destroy() // called when the browser exits
Applet Parameters • You may wish to change the behavior of your applet without modifying and recompiling it • parameters may be specified in the HTML applet start and end tags <applet code = “MyApplet.class” width = 200 height = 200> <param name=“…” value=“…”> <param name=“font” value=“monospaced”> </applet> • an applet can read these parameters String s = this.getParameter (“font”);
<html> <head><title>Sample Applet</title></head> <body> <applet code=SampleApplet.class width=350 height=150> <paramname=size value=24> <paramname=font value=monospaced> </applet> </body> </html>
import java.awt.*;import java.applet.*;public class SampleApplet extends Applet { private String fontName; private int fontSize; public void init() { fontName = this.getParameter (“font”); String s = this.getParameter (“size”); fontSize = Integer.parseInt (s); } public void paint(Graphics g) { Font f1 = new Font (fontName, Font.PLAIN, fontSize); g.setFont (f1); g.drawString (“Let’s try some things”, 20, 20); ... }}
Applet Size • in JDK1.1 Dimension size = this.getSize(); int width = size.width; int height = size.height; • in JDK1.2 int width = this.getWidth(); int height = this.getHeight(); • these methods are inherited from the Component class