GRAPHICS AND JAVA 2D

 Color and Font Control    Drawing Lines, Rectangles, and Ovals    Drawing Arcs, Polygons, and Polylines

In Java's Coordinate system, the origin (0,0) is the top left hand corner.  Since this is behind the title bar of the window, the coordinates should be selected so that the entire drawing is visible.  Every point is defined by its coordinate.  The x-coordinate is the horizontal distance to the right from the origin, and the y-coordinate s the vertical distance below the origin.  Their units are measured in pixels, which is the monitor's smallest unit of resolution.

A Java's graphics context enables drawing on the screen.  A graphics object manages a graphics context by controlling how information is drawn.  Graphics objects contain methods for drawing, font manipulation, color manipulation, etc.  In applets we use Graphics object g to manage the applet's graphics context.  The graphics class is an abstract class (i.e., Graphics objects cannot be instantiated or created), which makes Java portable.  Drawings are different on each platform, one class cannot implement drawing capabilities on all systems.  There are different ways to draw rectangles on Microsoft Windows, UNIX, or Macintosh..  When Java is implemented on each platform, a derived class of Graphics is created that actually implements all the drawing capabilities.  Its implementation is hidden, but the interface enables us to write programs in a platform-independent manner.

Class Component is the superclass for many of the classes in the jawa.awt package.  Component method paint takes a Graphics object as an argument.  This object is passed to the paint method by the system when a paint operation is required for a Component.  The header for the paint method is:

        public void paint( Graphics g )

The graphics object g receives a reference to an object of the system's derived Graphics class.

COLOR CONTROL

Class color defines methods and constants for manipulating colors in a Java Program.  Every color is created from a red, a green, and a blue component called RGB values.  They can be integers in the range 0 to 255, or floating point values 0.0 to 1.0.  R defines the amount of red, G the amount of green, and B the amount of blue.  The larger the value, the greater the amount of that particular color.  The user can choose from 256 x 256 x 256 (or 16, 777,216 colors).  For instance orange has a red value of 255, a green value of 200, and a blue value 0f 0.  Black is 0, 0, 0 while white is 255, 255, 255. Gray is 128, 128, 128.  If the values increase, gray becomes lighter, while if they decrease, gray becomes darker.

FONT CONTROL

Most font methods and font constants are part of class Font.  Class Font's constructor takes three arguments - the font name, font style, and font size.  Some font names are Monospaced, SansSerif, and Serif.  Some font styles are font.PLAIN, Font.ITALIC, and Font.BOLD, and they can be used in combination e.g. Font.ITALIC + Font.BOLD.  The font size is measured in points which is 1/72 of an inch.  (72 points = 1 inch, 36 points = 1/2 inch, the common 12 points = 1/6 inch)

Font methods are available to test the style of the current font.  The isplain method returns true if the current style is plain.  Other information about font's metrics are height, descent (the amount a character dips below the base line, ascent (the amount rises above the base line) and leading (the interline space - the difference between the descent of one line of text and the line  of text below it)

DRAWING LINES, RECTANGLES, AND OVALS

public void drawLine(  int x1, int y1, int x2, int y2 ) draws a line between points (x1, y1) and (x2, y2)

public void drawRect(  int x, int y, int width, int height ) draws a rectangle of specified width and height.  The top left hand corner has the coordinates (x, y)

public void fillRect(  int x, int y, int width, int height ) draws a solid rectangle of specified width and height.  The top left hand corner has the coordinates (x, y)

public void clearRect(  int x, int y, int width, int height ) draws a solid rectangle of specified width and height in the current background colorThe top left hand corner has the coordinates (x, y)

public void drawRoundRect(  int x, int 1, int width, int height, int arcWidth, int arcHeight ) draws a rectangle with rounded corners in the current color  of specified width and height.  The top left hand corner has the coordinates (x, y) The arcWidth and arcHeight determine the rounding of corners.

public void fillRoundRect(  int x, int 1, int width, int height, int arcWidth, int arcHeight ) draws a solid rectangle with rounded corners in the current color  of specified width and height.  The top left hand corner has the coordinates (x, y) The arcWidth and arcHeight determine the rounding of corners.

public void draw3DRect(  int x, int y, int width, int height, boolean b ) draws a three-dimensional rectangle in the current color of specified width and height.  The top left hand corner has the coordinates (x, y)  The rectangle appears raised when b is true, and lowered when b is false.

public void fill3DRect(  int x, int y, int width, int height, boolean b ) draws a filled three-dimensional rectangle in the current color of specified width and height.  The top left hand corner has the coordinates (x, y)  The rectangle appears raised when b is true, and lowered when b is false.

public void drawOval(  int x, int y, int width, int height ) draws an oval in the current color of specified width and height.  The bounding rectangle's top left hand corner has the coordinates (x, y).  The oval touches all four sids of the bounding rectangle at the center of each side.

public void fillOval(  int x, int y, int width, int height ) draws a filled  oval in the current color of specified width and height.  The bounding rectangle's top left hand corner has the coordinates (x, y).  The oval touches all four sids of the bounding rectangle at the center of each side.

To draw squares, draw rectangles with the same width and height.  To draw circles, draw ovals with the same width and height.  Note hat width and height values must not be negative..

DRAWING ARCS

An arc is a portion of an oval.  Arc angles are measured in degrees.  Arcs sweep from a starting angle the number of degrees specified by the arc angle.  Arcs that sweep in a counterclockwise direction are measured in positive degrees.  Arcs that sweep in a clockwise direction are measured in negative degrees.  A bounding rectangle for the oval is specified.

public void drawArc(  int x, int y, int width, int height, int startAngle, int arcAngle ) draws an arc in the current color of specified width and height.  The bounding rectangle's top left hand corner has the coordinates (x, y).  The arc segment is drawn starting at startAngle and sweeps arcAngle degrees.

public void fillArc(  int x, int y, int width, int height, int startAngle, int arcAngle ) draws a solid arc (i. e. a sector) in the current color of specified width and height.  The bounding rectangle's top left hand corner has the coordinates (x, y).  The arc segment is drawn starting at startAngle and sweeps arcAngle degrees.

DRAWING POLYGONS AND POLYLINES

Polygons are multisided shapes.  Polylines are a series of connected lines.

public void drawPolygon(  int xPoints[], int yPoints[], int points ) draws a polygon.  The x-coordinate of each point is specified in the xPoints array and the y-coordinate of each point is specified in the yPoints array  The last argument specifies the number of points.  This method draws a closed polygon even if the last point is different from the first point.

public void drawPolyLine(  int xPoints[], int yPoints[], int points ) draws a series of connected lines.  The x-coordinate of each point is specified in the xPoints array and the y-coordinate of each point is specified in the yPoints array  The last argument specifies the number of points.  If the last point is different from the first point, the polyline is not closed.

public void drawPolygon(  Polygon p ) draws the specified closed polygon.

public void fillPolygon(  int xPoints[], int yPoints[], int points ) draws a solid polygon.  The x-coordinate of each point is specified in the xPoints array and the y-coordinate of each point is specified in the yPoints array  The last argument specifies the number of points.  This method draws a closed polygon even if the last point is different from the first point.

public void fillPolygon(  Polygon p ) draws the specified solid closed polygon.