Java Methods ObjectOriented Programming and Data Structures 2
Java Methods Object-Oriented Programming and Data Structures 2 nd AP edition with Grid. World Maria Litvin ● Gary Litvin Graphics Copyright © 2011 by Maria Litvin, Gary Litvin, and Skylight Publishing. All rights reserved. 1
Objectives: • Understand computer graphics basics • Learn about paint, paint. Component, and repaint methods • Learn about coordinates and colors • Review shape drawing methods • Learn to use fonts and draw graphics text 2
Computer Graphics Basics • There are two types of graphics devices: vector and raster. • A vector device has some kind of pen that can move and draw lines directly on the surface. • A raster device creates a picture by setting colors to individual pixels (picture elements) on a rectangular “raster. ” 3
Basics (cont’d) • A graphics adapter in your computer is a raster device. • VRAM (video RAM) contains the information about the colors of all pixels. • The screen displays the contents of VRAM. • To draw a shape, you need to set the exactly right set of pixels to the required colors. • The number of pixels in the raster vertically and horizontally is called the device resolution. 4
Basics (cont’d) • A graphics software package provides functions for: Ø Ø setting drawing attributes: color, line width and style, fill texture and pattern, font drawing lines, circles, rectangles, polygons, and other basic shapes. • In other words, a graphics package turns a raster device into a “logical” vector device. 5
Graphics in Java • Java library offers a graphics class Graphics and a graphics package Graphics 2 D. • Graphics provides only most basic capabilities. • Graphics 2 D and related classes in java. awt support better graphics with color gradients, line styles, etc. • Here we only deal with Graphics. 6
Graphics in Windows • In a windowing environment, a picture must be repainted every time we move, reopen or reshape the window. • The program must have one “central” place or method where all the drawing happens. • The operating system sends a “message” to the program to repaint its window when necessary. 7
paint and paint. Component • The javax. swing. JFrame class (which represents application windows) has one method, called paint, where all the drawing takes place. • In Swing, paint calls paint. Component for each of the GUI components in the window. • A programmer creates a picture by overriding the default paint. Component method (or the paint method). 8
paint and paint. Component (cont’d) • paint method takes one argument of the type Graphics: import java. awt. *; import javax. swing. *; public class My. Window extends JFrame {. . . public void paint(Graphics g) { Defines the graphics super. paint(g); “context” (location, size, . . . coordinates, etc. ) } } 9
paint and paint. Component (cont’d) • The same for paint. Component: import java. awt. *; import javax. swing. *; Or another Swing GUI component public class My. Canvas extends JPanel {. . . public void paint. Component(Graphics g) { super. paint. Component(g); . . . } } 10
paint and paint. Component (cont’d) • A programmer never calls paint or paint. Component directly. repaint is called instead whenever you need to refresh the picture (after adding, moving, or changing some elements, etc. ): public class My. Canvas extends JPanel {. . . balloon. move(dx, dy); repaint(); . . . repaint takes no parameters: the graphics context is restored and sent to paint. Component automatically 11
Coordinates (0, 0) Origin: the upper-left corner of the component x Units: pixels y y-axis points down, as in many other graphics packages 12
Coordinates (cont’d) • A GUI component provides get. Width and get. Height methods that return its respective dimension. • These methods can be used to produce scalable graphics. • get. Width and get. Height only work after the component has been placed (do not call them from a component’s constructor). 13
Coordinates (cont’d) • The position of a rectangle, oval, and even an arc is defined by using its “bounding rectangle, ” described by x, y, width, height: x, y 14
Coordinates (cont’d) g. draw. Oval(x - r, y - r, 2*r); (x-r, y-r) r (x, y) 2 r 2 r 15
Colors • The color attribute is set by calling g. set. Color and stays in effect until changed: g. set. Color(Color. BLUE); g. draw. . . g. set. Color(Color. LIGHT_GRAY); . . . • You can form a color with specified red, green, and blue (RGB) values: int r. Val = 5, g. Val = 255, b. Val = 40; Color your. Eyes = new Color (r. Val, g. Val, b. Val); 16
Colors (cont’d) • javax. swing. JColor. Chooser lets you choose a color in a GUI application: 17
Drawing Basic Shapes g. draw. Line (x 1, y 1, x 2, y 2); g. clear. Rect (x, y, w, h); g. draw. Rect (x, y, w, h); g. fill. Rect (x, y, w, h); g. draw. Round. Rect (x, y, w, h, horz. D, vert. D); g. fill. Round. Rect (x, y, w, h, horz. D, vert. D); g. draw. Oval (x, y, w, h); g. fill. Oval (x, y, w, h); g. draw. Arc (x, y, w, h, from. Degr, measure. Degrs); 18
Basic Shapes (cont’d) g. draw. Polygon (x. Coords, y. Coords, n. Points); g. fill. Polygon (x. Coords, y. Coords, n. Points); g. draw. Polyline (x. Coords, y. Coords, n. Points); x, y abc g. draw. String (str, x, y); g. draw. Image (img, x, y, this); Image. Observer, often this 19
Fonts Font font = new Font (name, style, size); "Serif" abc "Sans. Serif" abc "Monospaced" abc g. set. Font (font); int (pixels) Font. PLAIN Font. BOLD Font. ITALIC 20
Review: • Explain the difference between vector and raster graphics. • In what units are the coordinates measured in Graphics? • Where is the origin of the coordinate system? • How is the position and size of a rectangle or an oval is defined? • How do you set a drawing color? • Name a few drawing methods. 21
- Slides: 21