Assignment 1: Self Portrait
Fall 2007, David Matuszek

Purposes of this assignment:

General idea of the assignment:

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. This should only take about half an hour. After this, I will get you started on the self portrait assignment; do as much as you can in the lab, then finish on your own after the lab.

If you missed lab, do this:

Since we haven't yet had a chance to actually talk about Java in class, you will just let Eclipse create a simple program for you, and add one line to it. One thing you need to know in advance is that capitalization matters. Capitalize everything in the program exactly as shown.

  1. Start Eclipse. This should bring you to a "Welcome to Eclipse" screen; if it does not, go to the menu item Help --> Welcome.
  2. Click on the Tutorials circle. This is the one with the green checkmark in it.
  3. Click on "Create a Hello World application." Don't click on the "Create a Hello World SWT application."
  4. Follow the Tutorial. Click on the Build a simple Java Application icon and follow the instructions. The one line you need to type in is:
         System.out.println("Hello world!");
    Be sure to get the capitalization right!

This is much less than we will do in lab, but it should get you started.

Your self portrait

Create a project named MyPortraitProject, and create an applet class called MyPortrait in the default (blank) package. Use these exact names, with the correct capitalization. Eclipse will complain about using the default package, but ignore that. Use the following code to get started:

import java.awt.*;
import javax.swing.*;


public class MyPortrait extends JApplet {


    public void paint(Graphics g) {
        // Replace this line with your code
    }
}

Add code to this applet 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 applet.

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 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 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 the commands you can use in the applet. (If you want a more detailed explanation, take your browser to http://java.sun.com/javase/6/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 MyPortrait.java.

Most class assignments will be done by teams, but this one you should do 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:

September 13, before midnight. Turn in your program electronically, using Blackboard. (See my Instructions for Using Zip Files and Blackboard). Only assignments submitted via Blackboard will be accepted--do not send your program by email. The usual late penalty of 5 points per day (out of 100 points) will apply.