CIT 590/591 Assignment 7: Self Portrait
Fall 2012, David Matuszek

Purposes of this assignment

General idea of the assignment

This is an individual assignment. You won't have a partner.

Use Eclipse to create a self portrait. You can do this with the Java commands listed below, modifying the numbers as necessary.

In today's lab I will step you through using Eclipse to create a couple of simple programs, following this outline. This should take about half the period. After this, I will get you started on the self portrait assignment.

Your self portrait

Create a project named SelfPortrait. Within that project, create a package called selfPortrait. Within that package, create a class called SelfPortrait. Use these exact names, paying attention to the capitalization. Use the following code to get started:

package selfPortrait;

import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JFrame;

/**
 * @author Put your name here
 */
public class SelfPortrait extends JFrame {

    public static void main(String[] args) {
        new SelfPortrait().run();
    }

    private void run() {
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(300, 300);
        setVisible(true);
    }
    
    public void paint(Graphics g) {
        g.drawString("Put your name here", 100, 280);
        g.setColor(Color.RED);
g.fillRect(10, 27, 280, 10);
g.setColor(Color.ORANGE);
g.fillOval(100, 100, 100, 120); } }

This program is just to get you started; change it as much as you like. Your goal is to draw a picture of yourself. Try to make it recognizable if at all possible (not just a smiley face, smiley ) You will not be graded on artistic merit, but you may lose points if it looks like you didn't even make an effort. Use a drawString command to put your name at the bottom of the window.

The variable g is the "Graphics" that you are drawing on, and is a part of every drawing command.

The named colors you can use are RED, ORANGE, YELLOW, GREEN, CYAN, BLUE, PINK, MAGENTA, BLACK, DARK_GRAY, GRAY, LIGHT_GRAY, and WHITE. You can create other colors, but these are the only ones that have predefined names. For more colors, see http://houseof3d.com/pete/applets/tools/colors/.

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 window. If the 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 the commands you can use in your program. (If you want a more detailed explanation, take your browser to http://docs.oracle.com/javase/7/docs/api/, click on java.awt in the upper left pane, then click on Graphics 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.

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. Amounts are integer value between 0 and 255.

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.

Turn in

For this assignment, you only need to turn in the one file, which should be named SelfPortrait.java.

Do this assignment by yourself. You can get help from your classmates, and you should help your classmates if they ask you to, but basically you should do the work yourself.

Due date

You program is due by 6am, November 2.