Introduction to Java Chapter 8 Introduction to Java


























- Slides: 26

Introduction to Java Chapter 8 Introduction to Java Graphics Chapter 8 - Introduction to Java Graphics 1

Introduction to 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 Chapter 8 - Introduction to Java Graphics 2

Introduction to Java 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. Chapter 8 - Introduction to Java Graphics 3

Introduction to Java 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 Chapter 8 - Introduction to Java Graphics 4

Introduction to Java Displaying Java Graphics (2) Required packages Create “Listener” Create component Create frame Add listener and component to frame Chapter 8 - Introduction to Java Graphics 5

Introduction to Java 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 Chapter 8 - Introduction to Java Graphics 6

Introduction to Java Displaying Graphics on a Component • The paint. Component method is used to draw graphics on a component. – The calling sequence is: paint. Component( Graphics g ) – The Graphics object must be immediately cast to a java. awt. Graphics 2 D 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 Chapter 8 - Introduction to Java Graphics 7

Introduction to Java Example: Drawing a Line Draw line represented by object Create Line 2 D object Chapter 8 - Introduction to Java Graphics 8

Introduction to Java The Graphics Coordinate System origin • Java uses a graphics coordinate 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-axis Chapter 8 - Introduction to Java Graphics x-axis 9

Introduction to Java The Line 2 D Classes • There are two concrete classes for creating lines: Line 2 D. Float and Line 2 D. Double. The only difference between them is the units of the calling parameters. • Constructors: Line 2 D. Double( double x 1, double y 1, double x 2, double y 2 ) Line 2 D. Float( float x 1, float y 1, float x 2, float y 2 ) • These classes create a line from (x 1, y 1) to (x 2, y 2) Chapter 8 - Introduction to Java Graphics 10

Introduction to Java Controlling Object Color • The color of a graphics object is controlled by the Graphics 2 D method set. Color. • The color may be any object of class java. awt. Color, including the following pre-defined values: Chapter 8 - Introduction to Java Graphics 11

Introduction to Java Controlling Line Width and Style • Line width and style is controlled with a Basic. Stroke object • Constructors have the form: Basic. Stroke(float width); Basic. Stroke(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 Chapter 8 - Introduction to Java Graphics 12

Introduction to Java Example: Setting Color and Stroke Set color Define stroke Set stroke Draw line Chapter 8 - Introduction to Java Graphics 13

Introduction to Java The Rectangle 2 D Classes • There are two classes for creating rectangles: Rectangle 2 D. Float and Rectangle 2 D. Double. The only difference between them is the units of the calling parameters. • Constructors: Rectangle 2 D. Double( double x, double y, double w, double h ) Rectangle 2 D. Float( float x, float y, float w, float h ) • These classes create a rectangle with origin (x, y), with width w and height h Chapter 8 - Introduction to Java Graphics 14

Introduction to Java Example: Creating a Rectangle Chapter 8 - Introduction to Java Graphics 15

Introduction to Java The Ellipse 2 D Classes • There are two classes for creating circles and ellipses: Ellipse 2 D. Float and Ellipse 2 D. Double. The only difference between them is the units of the calling parameters. • Constructors: Ellipse 2 D. Double( double x, double y, double w, double h); Ellipse 2 D. 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 Chapter 8 - Introduction to Java Graphics 16

Introduction to Java Example: Creating an Ellipse Chapter 8 - Introduction to Java Graphics 17

Introduction to Java The Arc 2 D Classes • There are two classes for creating arcs: Arc 2 D. Float and Arc 2 D. Double. • Constructors: Arc 2 D. Double( double x, double y, double w, double h, double start, double extent, int type ); Arc 2 D. 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 Arc 2 D. OPEN, Arc 2 D. CHORD, or Arc 2 D. PIE Chapter 8 - Introduction to Java Graphics 18

Introduction to Java Example: Creating Arcs Chapter 8 - Introduction to Java Graphics 19

Introduction to Java Displaying Text • Text is displayed using the Graphics 2 D method draw. String. • Forms: draw. String(String s, int x, int y); draw. String(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 2 D graphics objects, where (x, y) is the upper-left hand corner! Chapter 8 - Introduction to Java Graphics 20

Introduction to Java Example: Writing Text Strings This corner is (20, 40) Chapter 8 - Introduction to Java Graphics 21

Introduction to Java 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 Chapter 8 - Introduction to Java Graphics 22

Introduction to Java Standard Font Names • The following standard fonts are present on any Java implementation: Chapter 8 - Introduction to Java Graphics 23

Introduction to Java Example: Defining Fonts Chapter 8 - Introduction to Java Graphics 24

Introduction to Java Getting Information About Fonts • Class java. awt. Font. Metrics can be used to get information about a font • Constructor: Font. Metrics fm = new Font. Metrics( Font f ); Font. Metrics fm = g 2. get. Font. Metrics(); • Methods: Chapter 8 - Introduction to Java Graphics 25

Introduction to Java Example: Getting Font Information Code: Result: Chapter 8 - Introduction to Java Graphics 26