1 Graphics -- Introduction zThe use of graphics is common among modern software systems Java has strong API support for graphics in the java.awt (abstract windowing toolkit) package zThis lesson focuses on: ythe coordinate system for Java graphics ythe use of color ydrawing shapes such as lines, ovals, rectangles, etc. ythe use of fonts ybasic animation techniques
2 The Graphics Class An object of the Graphics class represents a particular drawing surface zIt defines a graphics context in which drawn shapes will be rendered The Graphics class contains methods for drawing various shapes and controlling visual aspects like font and color An applet has a graphics context, which is automatically passed to the paint method when it is called
3 The Coordinate System zA simple two-dimensional coordinate system exists for each graphics context (or drawing surface) zEach point on the coordinate system represents a single pixel zThe top left corner of the area is coordinate zA drawing surface has a particular width and height zAnything drawn outside of that area will not be visible See Coordinates.java show Coordinates.javashow
4 The Coordinate System x y Y X
5 Color The Color class is used to define and manage the color in which shapes are drawn zColors are defined by their RGB value, which defines the relative contribution of the primary colors red, green, and blue zEach drawing surface has a foreground color and a background color The setColor method of the Graphics class defines the foreground color, and the setBackground method of the component (the applet) sets the background color
6 Color The Color class contains several predefined colors, defined as public, static constants See Nature.java show it Nature.javashow it Many other colors can be defined using the constructor of the Color class zOver 16 million colors can be defined, but humans cannot distinguish between many similar colors zFurthermore, the hardware of most systems has limitations to the color options available
7 XOR Mode zDrawing in normal mode causes shapes of the same color to blend together zDrawing in XOR mode causes the overlapping portions of the shapes to be rendered in a contrasting color zThis effect can be used to "erase" a shape by redrawing it in the same color in the same spot while in XOR mode See XOR_Demo.java show itDemo.javashow it
8 Drawing Shapes The Graphics class contains methods for drawing several specific shapes: lines, ovals, rectangles, arcs, polygons, and polylines zMost shapes can be drawn filled or unfilled A line, drawn with the drawLine method, is always one pixel wide and cannot be filled
9 Ovals zAn oval is defined by its bounding rectangle: zThe methods that draw an oval take four parameters, all integers: drawOval (x, y, width, height) fillOval (x, y, width, height) width height
10 Ovals zThe first two parameters are the coordinate of the top-left corner of the bounding rectangle zThe third and fourth parameters specify the width and height of the bounding rectangle The drawOval method draws an unfilled oval and the fillOval method draws a filled (opaque) oval See Ovals.java and Rotating_Disk.java Ovals.java Rotating_Disk.java show it show itshow it
11 Rectangles zRectangles can be drawn yfilled or unfilled ywith squared or rounded corners ywith a slight three-dimensional effect or not zThe primary parameters for all rectangle drawing methods define the upper left corner of the rectangle and its width and height zThe shape of the rounded corner of a rounded rectangle are defined by an arc width and height
12 Rectangles zA three dimensional rectangle is shown using a small highlight on two sides of the rectangle zThe highlight appears on the bottom and right or the top and left as specified by a boolean parameter to the 3D drawing methods See Rectangles.java, show it Rounded_Rectangles.java, show it and Three_D_Rectangles.java show it Rectangles.javashow it Rounded_Rectangles.javashow it Three_D_Rectangles.javashow it
13 Arcs zAn arc is defined as a segment of an oval zThe first four parameters to the arc drawing methods define the bounding rectangle of the oval (top left corner, width, and height) zThe other two parameters define the start angle and the arc angle zThe start angle indicates where the arc begins and the arc angle determines how far the arc sweeps across its defining oval See Arc.java show it Arc.javashow it
14 Arcs zThe start angle can be specified using both positive and negative values:
15 Arcs zAn arc angle can also be positive or negative zA positive arc angle sweeps counterclockwise, and a negative arc angle sweeps clockwise zTherefore, the same arc can be specified using four different combinations of start and arc angles zArcs can also be filled or unfilled See Arcs.java show it Arcs.javashow it
16 Polygons zA polygon is a multisided figure defined by a series of ordered points zLine segments connecting the points form the polygon The points are defined by corresponding arrays of x and y coordinate values, and can already be incorporated into an object of the Polygon class zPolygons are closed, forming a line segment from the last point back to the first See Polygons.java show it Polygons.javashow it
17 Polylines zA polyline is similar to a polygon except that it is not closed zThat is, there is no line segment from the last point back to the first unless explicitly specified zThey are convenient for specifying certain kinds of complex shapes zPolylines cannot be filled See Polylines.java show Polylines.javashow
18 Fonts zA font defines the look of each character when it is printed or drawn The Font class provides methods for specifying fonts in a Java program zEach computer system supports a specific set of fonts See Font_Lister.java Font_Lister.java The setFont method defines the current font for a program
19 Fonts A font is defined using the Font class constructor and a combination of: yfont name yfont style: plain, bold, italic, or bold+italic yfont size, in points Constants are defined in the Font class to specify the font style See Entropy.java Entropy.java
20 Animations zSimple animations can be accomplished by drawing a shape, then erasing it, then drawing it again in a slightly altered position zErasing can be accomplished through careful use of XOR mode zTiming must be controlled so that the animation does not move too fast See Bouncing_Ball.java show it Bouncing_Ball.javashow it