110 likes | 267 Views
2D Drawing in Java. Coordinate System. +x. (0,0). +y. Graphics class. The Graphics class Contains methods and current state to allow us to draw 2D shapes State variables (are set and used until changed) Color * Font * Clipping area Coordinate system Methods
E N D
Coordinate System +x (0,0) +y
Graphics class • The Graphics class • Contains methods and current state to allow us to draw 2D shapes • State variables (are set and used until changed) • Color * • Font * • Clipping area • Coordinate system • Methods • See java documentation for Graphics class for a complete list
Graphics methods • Color -- a class with the following public properties: • blac,k BLACK • red, RED • pink, PINK • orange, ORANGE • yellow, YELLOW • green, GREEN • magenta, MAGENTA • cyan, CYAN • blue, BLUE
Graphics methods • public abstract void drawLine(int x1, int y1, int x2, int y2) • Draws a line, using the current color, between the points (x1, y1) and (x2, y2) in this graphics context's coordinate system. • Parameters: • x1 - the first point's x coordinate. • y1 - the first point's y coordinate. • x2 - the second point's x coordinate. • y2 - the second point's y coordinate.
Graphics methods • public void drawRect(int x, int y, int width, int height) • Draws the outline of the specified rectangle. • The left and right edges of the rectangle are at x and x + width. • The top and bottom edges are at y and y + height. • The rectangle is drawn using the graphics context's current color. • Parameters: • x- the x coordinate of the rectangle to be drawn. • y - the y coordinate of the rectangle to be drawn. • width - the width of the rectangle to be drawn. • height - the height of the rectangle to be drawn.
Graphics methods • public abstract void drawOval(int x, int y, int width, int height) • Draws the outline of an oval. • The result is a circle or ellipse that fits within the rectangle specified by the x, y, width, and height arguments. • The oval covers an area that is width + 1 pixels wide and height + 1 pixels tall. • Parameters: • x - the x coordinate of the upper left corner of the oval to be drawn. • y - the y coordinate of the upper left corner of the oval to be drawn. • width - the width of the oval to be drawn. • height - the height of the oval to be drawn.
Graphics methods • public abstract void drawArc(int x, int y, int width, int height, intstartAngle, intarcAngle) • Draws the outline of a circular or elliptical arc covering the specified rectangle. • The resulting arc begins at startAngle and extends for arcAngle degrees, using the current color. • Angles are interpreted such that 0 degrees is at the 3 o'clock position. A positive value indicates a counter-clockwise rotation while a negative value indicates a clockwise rotation. • The center of the arc is the center of the rectangle whose origin is (x, y) and whose size is specified by the width and height arguments. • The resulting arc covers an area width + 1 pixels wide by height + 1 pixels tall.
drawArc continued • public abstract void drawArc(int x, int y, int width, int height, intstartAngle, intarcAngle) • Parameters: • x - the x coordinate of the upper-left corner of the arc to be drawn. • y - the y coordinate of the upper-left corner of the arc to be drawn. • width - the width of the arc to be drawn. • height - the height of the arc to be drawn. • startAngle - the beginning angle. • arcAngle - the angular extent of the arc, relative to the start angle.
Graphics methods • public abstract void drawPolyline(int[] xPoints, int[] yPoints, intnPoints) • Draws a sequence of connected lines defined by arrays of x and y coordinates. • Each pair of (x, y) coordinates defines a point. • The figure is not closed if the first point differs from the last point. • Parameters: • xPoints - an array of x points • yPoints - an array of ypointsn • Points - the total number of points
Graphics methods • To draw filled shapes rather than outlines: • public void fillRect(int x, int y, int width, int height) • public abstract void fillOval(int x, int y, int width, int height) • public abstract void fillArc(int x, int y, int width, int height, intstartAngle, intarcAngle) • public abstract void fillPolyline(int[] xPoints, int[] yPoints, intnPoints)