Java Drawing Commands
Fall 2014, David Matuszek

You can draw (paint) on any Java Container (JFrame, JPanel, JApplet). To do so:

  1. Extend the container with a class of your own.
  2. In your class, override the inherited method public void paint(Graphics g). The variable g is the "Graphics" that you are drawing on, and is a part of every drawing command. This method (or methods called from this method) should draw everything that is to appear in the component. Begin by erasing everything left over from the previous drawing, with a pair of commands such as
    g.setColor(Color.WHITE);
    g.fillRect(0, 0, getWidth(), getHeight());
    
  3. Do not call the paint method! Instead, to repaint the Container, call the inherited method repaint(), which will call paint with the needed Graphics parameter.

Java uses an X-Y coordinate system, where the units are pixels (picture elements, usually about 72 pixels per inch). X=0, Y=0 is the top left corner of the applet window. If the applet window is 400 pixels wide and 300 pixels high, then the bottom right corner is X=399, Y=299. It's okay to draw things larger than the window, but only the part visible "through the window" will be visible on the screen.

Most of the drawing commands take place within a rectangle defined by (x, y, width, height), where x and y are the coordinates of the top left corner of the rectangle. In algebra, increasing values of y are "higher" in a graph; but in Java, increasing y moves you down on the screen.

Here is a brief explanation of some of the commands you can use in the paint method. If you want a more detailed explanation, take your browser to https://docs.oracle.com/javase/7/docs/api/, click on java.awt in the upper left pane, then click on Graphics (under Classes) in the lower left pane. The large pane on the right will show detailed explanations of these and other commands.

g.setColor(color);
Use the specified color for subsequent drawing commands. The named colors you can use are RED, ORANGE, YELLOW, GREEN, CYAN, BLUE, PINK, MAGENTA, BLACK, DARK_GRAY, GRAY, LIGHT_GRAY, and WHITE.

g.setColor(new Color(red, green, blue));
Create a color using the given amounts of red, green, and blue, and use the specified color for subsequent drawing commands. There are numerous web sites that can help you choose a color, for example, http://www.w3schools.com/tags/ref_colorpicker.asp.

g.drawRect(x, y, width, height);
Draw the outline of a rectangle starting at x, y, with the given width and height.

g.fillRect(x, y, width, height);
Draw a solid rectangle or square starting at x, y, with the given width and height.

g.drawOval(x, y, width, height);
Draw the outline of an ellipse or circle included in the rectangle starting at x, y, with the given width and height.

g.fillOval(x, y, width, height);
Draw a solid ellipse or circle included in the rectangle starting at x, y, with the given width and height.

g.drawArc(x, y, width, height, startAngle, degrees);
Draw an arc of a circle whose center is the center of the circle included in the rectangle starting at x, y, with the given width and height. The arc starts at startAngle (0 is the 3 o'clock position) and continues counterclockwise for the specified number of degrees.

g.fillArc(x
, y, width, height, startAngle, degrees);
Draw a sector (wedge) of a circle whose center is the center of the circle included in the rectangle starting at x, y, with the given width and height. The sector starts at startAngle (0 is the 3 o'clock position) and continues counterclockwise for the specified number of degrees.

g.drawRoundRect(x, y, width, height, arcWidth, arcHeight);
Draw the outline of a rectangle with rounded corners starting at x, y, with the given width and height. The amount of "roundedness" is given by arcWidth and arcHeight.

g.fillRoundRect(x, y, width, height, arcWidth, arcHeight);
Draw a solid rectangle with rounded corners starting at x, y, with the given width and height. The amount of "roundedness" is given by arcWidth and arcHeight.

g.drawLine(x1, y1, x2, y2);
Draw a line from the point (x1, y1) to the point (x2, y2).

g.drawString("Some quoted string", x, y)
;
Draws the characters inside quotes starting at the position (x, y). Unlike the other methods, the point (x, y) is the bottom left corner of the first letter.

g.drawPolygon(xArray, yArray, n);
Draws the outline of a polygon. The sequence of points is given by the n corresponding values in xArray and yArray. For example, the command
    g.drawPolygon(new int[] { 250, 290, 210 },
                  new int[] { 210, 290, 290 }, 3);

will draw a triangle using the 3 points (250, 210), (290, 290), and (210, 290).

g.fillPolygon(xArray, yArray, n);
Draws a solid polygon. The sequence of points is given by the n corresponding values in xArray and yArray.When the edges of the polygon cross, some areas may be considered to be "outside" the polygon, hence not colored in.

Due date

Turn your assignment in to Canvas before 6am Monday, December 1.