Download presentation
Presentation is loading. Please wait.
Published byMoses Bennett Modified over 9 years ago
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?
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 Your third programming assignment will be an Applet
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 the Applet 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 Graphics Class Frames and Applets have a graphics context associated with them. A Graphics object defines a graphics context on which we can draw shapes and text. The Graphics class is in the java.awt package. The state of a Graphics object includes such properties as height, width, foreground color and font. Positions within the Graphics context are measured from the upper left corner and they have units of pixels.
8
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 5 - 8 Graphics Class Accessor methods are provided for getting height, width, color, font, … Mutator methods are also provided for these properties. The Graphics class has several methods for drawing shapes There is a method for displaying text. You can also display images.
9
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 5 - 9 Drawing a Line X Y 10 20 150 45 g.drawLine (10, 20, 150, 45); g.drawLine (150, 45, 10, 20); or
10
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 5 - 10 The Effect of drawRect
11
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 5 - 11 Drawing an Oval X Y g.drawOval (175, 20, 50, 80); 175 20 50 80 bounding rectangle
12
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 5 - 12 Displaying a String The drawString method can be used to display text in a Graphics context. void drawString( String text, int x, int y; *The position of the text relative to x and y is shown in the figure
13
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 5 - 13 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.