1 / 18

Applets & Graphics

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

camila
Download Presentation

Applets & Graphics

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Applets & Graphics

  2. 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

  3. 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>

  4. 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

  5. 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)

  6. Coordinate System • Graphics uses an integer coordinate system with origin at top left corner of the applet 0,0 x y width, height

  7. 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.

  8. Font Class • Constructor public Font (String name, int style, int size) • Names • “TimesRoman” • “Arial” • “Courier” • “Impact” • Logical names • “serif” // TimesRoman • “sanserif” // Helvetica • “monospaced” // Courier

  9. 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

  10. 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);

  11. 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); }}

  12. 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

  13. 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”);

  14. <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>

  15. 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); ... }}

  16. 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

More Related