Download presentation
Presentation is loading. Please wait.
1
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 5 - 1 Java Applets What is an Applet? How do you create an Applet? Some Useful Classes
2
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 5 - 2 What is an Applet A Java applet is a program that is intended to transported over the Web and executed using a web browser –An applet is embedded into an HTML file using a tag that references the bytecode file of the applet class The bytecode version of the program is transported across the web and executed by a Java interpreter that is part of the browser –An applet also can be executed using the appletviewer tool of the Java Software Development Kit
3
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 5 - 3 Applet Classes For applets, there are two classes that can be used for creating applets. –Applet - draw directly on the applet –JApplet - create an object (derived from JPanel) to draw on LogoApplet, which you will use for your first programming assignment, is a JApplet
4
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 5 - 4 Applets The class that defines an applet extends either the Applet class or the JApplet class Applets have several methods that the browser calls –The init method is called once when the applet starts up –The paint method is used to draw the applet’s contents accepts a parameter that is an object of the Graphics class You need to write the body of these methods to make the applet look the way you want it to
5
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 5 - 5 Viewing Applets In order to view an applet, you need an html file which references the class file In a browser, put in the URL of the html page Use the appletviewer tool to look at the applet appletviewer myApplet.html –appletviewer shows only the applet and ignores any other content
6
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 5 - 6 The HTML applet Tag My Applet <applet code="MyApplet.class" width=350 height=175>
7
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 5 - 7 Creating a Content Pane A JApplet has a content pane which contains the code that controls what the applet looks like. The GraphicPane class can be used as the content pane of an applet
8
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 5 - 8 GraphicPane Class Constructors public GraphicPane() public GraphicPane( Color c) Methods void addShape( Shape s, Color c) void addOutline( Shape s, Color c) void addText( String text, Color c, int x, int y, int fontSize)
9
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 5 - 9 How the GraphicPane is Painted 1.All Shapes, both filled and outlined are drawn in the order they were added to the GraphicPane 2.All text is drawn in the order it was added to the GraphicPane
10
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 5 - 10 Creating a Content Pane In the init method of the applet –Create a new GraphicPane object GraphicPane content = new GraphicPane(); –Create desired shapes and add to the GraphicPane content.addShape( shape1, color1); –Add any desired text to the GraphicPane content.addText( "the text", textcolor, x, y, fontsize); –Set the content pane of the applet to the GraphicPane setContentPane( content);
11
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 5 - 11 Shapes The Java library has an interface called Shape. There are a number of classes that implement the Shape interface. Any of these classes can be added to a GraphicPane –java.awt.Rectangle A couple of Shape classes have been written to be used in LogoApplet. –Ellipse –Line
12
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 5 - 12 Graphics Coordinates Coordinates in a graphics context are measured in pixels from the upper left corner of the window –the x coordinate is the distance from the left edge –the y coordinate is the distance from the top
13
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 5 - 13 Rectangle Create a Rectangle by providing the location of the upper-left corner and the height and width Rectangle r = new Rectangle(10, 10, 40, 50); Rectangles can be added to a GraphicPane using either addShape or addOutline
14
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 5 - 14 Ellipse The Ellipse class allows you to add circles and ellipses to the applet Create an ellipse by specifying the x and y coordinates of the bounding rectangle followed by the width and height of the ellipse Ellipse e = new Ellipse( 100, 100, 20, 30); Ellipses can be added to a GraphicPane using either addShape or addOutline
15
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 5 - 15 Line The Line class creates a Shape that is drawn as a Line. Create a Line by specifying the x and y coordinates of the endpoints Line ln = new Line( 10, 10, 50, 50); Add a line to the graphicPane using addOutline content.addOutline( ln, myColor);
16
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 5 - 16 Polygons A polygon is a closed figure which is defined by a sequence of points called vertexes. The awt package has a Polygon class which implements the Shape interface Create an empty Polygon with Polygon poly = new Polygon() Add vertexes using poly.addPoint( xi, yi); Add the Polygon to the GraphicPane using either addShape or addOutline.
17
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 5 - 17 Color Class The java.awt.Color class allows us to create a color object. Color class has public constants for common colors: –Gray scale: Color.black –Primary colors: Color.red Color.green Color.blue –Secondary colors: Color.yellow Color.cyan Color.magenta –Others: Color.orange Create custom colors by specifying three values ranging from 0 to 255 for red, green, and blue. Color pinkColor; pinkColor = new Color(255,175,175)
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.