Download presentation
Presentation is loading. Please wait.
Published byBritney Webster Modified over 9 years ago
1
Introduction to Java Chapter 8 - Introduction to Java Graphics1 Chapter 8 Introduction to Java Graphics
2
Introduction to Java Chapter 8 - Introduction to Java Graphics2 Java Graphics Systems The Java SDK contains to different graphics systems –The Abstract Windowing Toolkit (AWT), which was the original Java graphics system –The Swing package, which is a newer, more flexible graphics system Only the Swing graphics system is taught in this text
3
Introduction to Java Chapter 8 - Introduction to Java Graphics3 Components and Containers The two principal types of graphics objects are Containers and Components A Component is visual object containing text or graphics –Here, we will work with a completely blank component, known as a “Canvas” A Container is a graphical object that can hold components or other containers –The principal container is a Frame. It is a part of the computer screen surrounded by borders and title bars.
4
Introduction to Java Chapter 8 - Introduction to Java Graphics4 Displaying Java Graphics To display Java graphics: 1.Create the component or components to display 2.Create a frame to hold the component(s), and place the component(s) into the frame(s). 3.Create a “listener” object to detect and respond to mouse clicks, and assign the listener to the frame. The components in this chapter will be of class JCanvas, and the containers will be of class JFrame
5
Introduction to Java Chapter 8 - Introduction to Java Graphics5 Displaying Java Graphics (2) Required packages Create component Create “Listener” Create frame Add listener and com- ponent to frame
6
Introduction to Java Chapter 8 - Introduction to Java Graphics6 Listeners A “Listener” class listens for mouse clicks or keyboard input on a container or component, and responds when it occurs –In this chapter, we will use a “Window” listener to detect mouse clicks and to shut down the program Trap mouse clicks in the “Close Window” box, and exit when one occurs
7
Introduction to Java Chapter 8 - Introduction to Java Graphics7 Displaying Graphics on a Component The paintComponent method is used to draw graphics on a component. –The calling sequence is: paintComponent( Graphics g ) –The Graphics object must be immediately cast to a java.awt.Graphics2D object before it can be used with Swing graphics –Once this is done, all of the classes in java.awt.geom can be used to draw graphics on the component
8
Introduction to Java Chapter 8 - Introduction to Java Graphics8 Example: Drawing a Line Create Line2D object Draw line represented by object
9
Introduction to Java Chapter 8 - Introduction to Java Graphics9 The Graphics Coordinate System Java uses a graphics coor- dinate system with the origin (0,0) in the upper left-hand corner –x axis is positive to the right –y axis is positive down By default, the units of measure are pixels –There are 72 pixels / inch Unit of measure can be changed y-axisx-axis origin
10
Introduction to Java Chapter 8 - Introduction to Java Graphics10 The Line2D Classes There are two concrete classes for creating lines: Line2D.Float and Line2D.Double. The only difference between them is the units of the calling parameters. Constructors: Line2D.Double( double x1, double y1, double x2, double y2 ) Line2D.Float( float x1, float y1, float x2, float y2 ) These classes create a line from (x 1,y 1 ) to (x 2,y 2 )
11
Introduction to Java Chapter 8 - Introduction to Java Graphics11 Controlling Object Color The color of a graphics object is controlled by the Graphics2D method setColor. The color may be any object of class java.awt. Color, including the following pre-defined values:
12
Introduction to Java Chapter 8 - Introduction to Java Graphics12 Controlling Line Width and Style Line width and style is controlled with a BasicStroke object Constructors have the form: BasicStroke(float width); BasicStroke(float width, int cap, int join, float miterlimit, float[] dash, float dash_phase); Can control line width, line cap style, line join style, and dashing pattern
13
Introduction to Java Chapter 8 - Introduction to Java Graphics13 Example: Setting Color and Stroke Set color Set stroke Draw line Define stroke
14
Introduction to Java Chapter 8 - Introduction to Java Graphics14 The Rectangle2D Classes There are two classes for creating rectangles: Rectangle2D.Float and Rectangle2D.Double. The only difference between them is the units of the calling parameters. Constructors: Rectangle2D.Double( double x, double y, double w, double h ) Rectangle2D.Float( float x, float y, float w, float h ) These classes create a rectangle with origin (x,y), with width w and height h
15
Introduction to Java Chapter 8 - Introduction to Java Graphics15 Example: Creating a Rectangle
16
Introduction to Java Chapter 8 - Introduction to Java Graphics16 The Ellipse2D Classes There are two classes for creating circles and ellipses: Ellipse2D.Float and Ellipse2D. Double. The only difference between them is the units of the calling parameters. Constructors: Ellipse2D.Double( double x, double y, double w, double h); Ellipse2D.Float( float x, float y, float w, float h); These classes create the ellipse that fits in a rectangular box with origin (x,y), with width w and height h
17
Introduction to Java Chapter 8 - Introduction to Java Graphics17 Example: Creating an Ellipse
18
Introduction to Java Chapter 8 - Introduction to Java Graphics18 The Arc2D Classes There are two classes for creating arcs: Arc2D.Float and Arc2D.Double. Constructors: Arc2D.Double( double x, double y, double w, double h, double start, double extent, int type ); Arc2D.Float( float x, float y, float w, float h, float start, float extent, int type ); –These classes create an arc that fits in a rectangular box with origin (x,y), with width w and height h. The arc starts at start radians and extends for extent radians. –The type of arc is Arc2D.OPEN, Arc2D.CHORD, or Arc2D.PIE
19
Introduction to Java Chapter 8 - Introduction to Java Graphics19 Example: Creating Arcs
20
Introduction to Java Chapter 8 - Introduction to Java Graphics20 Displaying Text Text is displayed using the Graphics2D method drawString. Forms: drawString(String s, int x, int y); drawString(String s, float x, float y); These methods write String s on the component. The point (x,y) specifies the lower-left hand corner of the text box within the component. –Note that this differs from the convention for other 2D graphics objects, where (x,y) is the upper-left hand corner!
21
Introduction to Java Chapter 8 - Introduction to Java Graphics21 Example: Writing Text Strings This corner is (20,40)
22
Introduction to Java Chapter 8 - Introduction to Java Graphics22 Setting Fonts Fonts are created with the java.awt.Font class Constructor: Font( String s, int style, int size ) –s is the name for the font to use. –style is the style ( Font.PLAIN, Font.BOLD, Font.ITALIC, or a combination) –size is the font size in points Any font on the system may be used, but certain fonts are guaranteed to be present on any system
23
Introduction to Java Chapter 8 - Introduction to Java Graphics23 Standard Font Names The following standard fonts are present on any Java implementation:
24
Introduction to Java Chapter 8 - Introduction to Java Graphics24 Example: Defining Fonts
25
Introduction to Java Chapter 8 - Introduction to Java Graphics25 Getting Information About Fonts Class java.awt.FontMetrics can be used to get information about a font Constructor: FontMetrics fm = new FontMetrics( Font f ); FontMetrics fm = g2.getFontMetrics(); Methods:
26
Introduction to Java Chapter 8 - Introduction to Java Graphics26 Example: Getting Font Information Result: Code:
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.