Graphics & Java 2D Drawing Two Dimensional Shapes Controlling Fonts Controlling Colors
Java’s Coordinate System The upper-left coordinate (0,0) is behind the title bar of the window. Drawing coordinates should be adjusted to draw inside the borders of a window Java’s Container Class (superclass of all windows in Java) has method getInsets() that determine the drawing area for a window.
Color Control Constants ( ORANGE, PINK, CYAN, MAGENTA, YELLOW, BLACK, WHITE, GRAY, RED, GREEN, BLUE ) Example –g.setColor(Color.MAGENTA); RGB (Red Green Blue Values) –0 to 255 ( 0 no color, 255 full color) –0.0 to 1.0 Example –g.setColor(new Color(255, 0,0)); –g.setColor (new Color (0.0f, 1.0f, 0.0f)
Font Control Java Font Styles (PLAIN, BOLD, ITALIC) Java Fonts –Serif (Times) –Monospaced (Courier) –SanSerif (Helvetica) Examples: –g.setFont(new Font (“Serif”, Font.BOLD, 12)); –g.setFont(new Font(“Monospaced”, Font.ITALIC + Font.BOLD, 24)
Drawing Lines, Rectangles, Ovals Basic Drawing Methods –drawLine (x1, y1, x2, y2) –drawRect (x, y, width, height) –drawOval (x, y, width, height) (x,y) is top-left corner of bounding box of oval Fill Shapes –Call setColor method before filling shapes –fillRect (x, y, width,height) –fillOval(x, y, width, height)
Drawing Polygons and Polylines Polygons -Closed multisided shapes consisting of straight line segments Polylines – a sequence of connected points.
Drawing Polygons and Polylines Graphics Methods for drawing Polygons drawPolygon( int xValues[], int yValues[], int numPoints) drawPolyline (int xValues[], int yValues[], int numPoints) fillPolygon (int xValues[], int yValues[], int numPoints) Example: int xValues[] = {20,30,40}; int yValues[] = {50,50, 60}; g.drawPolygon (xValues, yValues, 3); g.drawPolyline (xValeus, yValues, 3);
Drawing Polygons and Polylines Graphics Methods for drawing Polygons drawPolygon(Polygon p) fillPolygon (Polygon p) Polygon Constructors and Methods Polygon() – creates a new polygon that does not contain any points addPoint (int x, int y) – adds point to polygon Polygon (int xValues[], int yValues[], int numPoints) int xValues[] = {20,30,40}; int yValues[] = {50,50, 60}; Polygon p = new Polygon (xValues, yValues, 3); p.addPoint(30,80); g.fillPolygon (p);