Presentation is loading. Please wait.

Presentation is loading. Please wait.

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 5 - 1 Chapter 5 Java Graphics Applets.

Similar presentations


Presentation on theme: "©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 5 - 1 Chapter 5 Java Graphics Applets."— Presentation transcript:

1 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 5 - 1 Chapter 5 Java Graphics Applets

2 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 5 - 2 Graphics Classes We introduce four standard classes related to drawing geometric shapes on a window: –java.awt.Graphics –java.awt.Color –java.awt.Point –java.awt.Dimension

3 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 5 - 3 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 and background color and font. Positions within the Graphics context are measured from the upper left corner and they have units of pixels.

4 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 5 - 4 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 also a method for displaying text. You can also display images.

5 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 5 - 5 Graphics Methods

6 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 5 - 6 The Effect of drawRect

7 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 5 - 7 Drawing a Line X Y 10 20 150 45 g.drawLine (10, 20, 150, 45); g.drawLine (150, 45, 10, 20); or

8 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 5 - 8 Drawing an Oval X Y g.drawOval (175, 20, 50, 80); 175 20 50 80 bounding rectangle

9 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 5 - 9 Drawing an Arc An arc is a part of an oval To draw you need to specify –the bounding rectangle for the oval –the starting angle (measured from the x-axis in degrees –the angle the arc subtends

10 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 5 - 10 Drawing an Arc void drawArc(int x, int y, int width, int height, int startAngle, int arcAngle) g.drawArc (75, 20, 50, 80, 90, 90);

11 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 5 - 11 Drawing 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 below

12 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 5 - 12 Rectangles with a 3D Look Another method in the Graphics class allows you to display rectangles that look 3D fill3DRect( int x, int y, int width, int height, boolean raised) The boolean variable will be true if you want the rectangle to look raised and false to make it look lowered.

13 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 5 - 13 Polygons A polygon is a closed figure which is defined by a sequence of points called vertexes. The awt package has a Polygon class Create an empty Polygon with Polygon poly = new Polygon() Add another vertex using poly.addPoint( x, y); Use the Graphics drawPolygon and fillPolygon method to display the Polygon g.drawPolygon( poly); g.fillPolygon( poly)

14 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 5 - 14 Color Class The java.awt.Color class allows us to designate the color of an object. The RGB scheme combines three values ranging from 0 to 255 for red, green, and blue. Color pinkColor; pinkColor = new Color(255,175,175)

15 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 5 - 15 Color Class There are public class constants defined in the Color class for common colors: –Color.black –Color.blue –Color.green –Color.magenta –Color.orange

16 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 5 - 16 The Color Class Every drawing surface has a background color g.setBackground(Color.white); Every graphics context has a current foreground color –The most recently set foreground color is what is used for drawing g.setColor(Color.blue); g.drawRect(50, 50, 100, 30);

17 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 5 - 17 Using Fonts Every Graphics object has a Font associated with it. You can find out what the Font is with Font getFont( void); You can change the Font with void setFont( Font); The Font is part of the state of the graphics object – a String will be drawn with the current Font.

18 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 5 - 18 The Font Class A Font has three components –Font name “Monospaced” “Serif” “SansSerif” –Font Style (add BOLD and ITALIC to get both) Font.BOLD Font.ITALIC Font.PLAIN Font Size - integer number of points

19 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 5 - 19 Creating a Font Constructor takes three parameters Font( String fontFace, int fontStyle, int size) Examples Font f1 = new Font( “SansSerif”, Font.BOLD, 24); Font f1 = new Font( “SansSerif”, Font.BOLD + Font.ITALIC, 16);

20 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 5 - 20 Font Measurements Sometimes, you want to know how big a String will be when you draw it Create a FontMetrics object for your Graphics context FontMetrics fm = g.getFontMetrics( f1) Use the stringWidth method int width = fm.getWidth( aString);

21 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 5 - 21 Point class A java.awt.Point object designates a point in two- dimensional space. The Point class has public instance variables To assign the position (10, 20) to a Point: Point pt = new Point(); pt.x = 10; pt.y = 20;

22 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 5 - 22 Dimension Class The java.awt.Dimension class can be used to create bounding rectangles that surround shapes. Dimension dim = new Dimension(); dim.width = 40; dim.height = 70; Bounding rectangles

23 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 5 - 23 Applets A Java application is a stand-alone program with a main method (like the ones we've seen so far) that you run from the console. A Java applet is a program that is intended to transported over the Web and executed using a web browser –An applet also can be executed using the appletviewer tool of the Java Software Development Kit –An applet doesn't have a main method –Instead, there are several instance methods that serve specific purposes

24 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 5 - 24 Applets The paint method, for instance, is executed automatically and is used to draw the applet’s contents The paint method accepts a parameter that is an object of the Graphics class

25 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 5 - 25 Applets The class that defines an applet extends the Applet class 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

26 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 5 - 26 The HTML applet Tag My Applet <applet code="Einstein.class" width=350 height=175>


Download ppt "©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 5 - 1 Chapter 5 Java Graphics Applets."

Similar presentations


Ads by Google