A112 Assignment One

Graphic Composition Applet

Due midnight, Monday, October 30th.

In-lab Work

Your AI will provide you with a password that you will type into the Java source file you submit in lab. You will submit one file via the A112 Vincent submission system (linked from the A112 home page).

Part 1

First, open your student locker and open a Command prompt window. In this window, carefully type the following commands (ending each command, as always, with the enter key):

Desktop:\> M:
M:\> mkdir a112
M:\> cd a112
M:\a112> mkdir a1
M:\a112> cd a1
M:\a112\a1> notepad Hello.html

Here's what these commands are doing.

  1. The first cd (change directory) command moves the attention of MS-DOS to your student locker.
  2. The following mkdir (make directory) command creates a course directory named a112 in your student locker.
  3. Change to the new course directory.
  4. Make a subdirectory named a1 for this assignment. In this way create a new directory for each assignment and save all files for that assignment in its directory.
  5. Change to the assignment directory.
  6. Invoke notepad to create a file named Hello.html. Notepad is a very simple plain-text editor. Since .html and .java files should contain only plain-text (ASCII) characters, it is safest to edit them only with a plain-text editor.

Next enter the following two lines of HTML code into notepad, save the file, and exit notepad. (Cut and past from this page in your web browser is the easy way to enter text from assignments.)

<applet code="Hello.class" width=200 height=200>
</applet>

Now invoke notepad again and create a file named Hello.java. The capital H is important. Java file names are case sensitive. Enter the following text in this Java source code file:

// Hello.java: first applet for A112 by Chris Haynes

import java.applet.Applet;
import java.awt.*;

public class Hello extends Applet {

   public void paint(Graphics g) {

      g.drawString("Hello", 50, 50);

   }
}    

The first line, like all comment lines in this course, begins with two slashes. Every source code file in this course should begin with a comment containing the file name, a brief description of its purpose, and the author's name.

The four lines beginning with import or public, and the corresponding last two lines of closing braces, are applet "boiler plate". Applets in this course will always begin and end with those lines. The only thing that changes in them is the class name, in this case Hello.

When you have saved this file, enter in the command window the following command, which tells the java compiler, javac, to compile the Hello.java source file:

javac Hello.java

If you got any error messages, you didn't enter something properly. Double check your work. When it has compiled without error, you can view the applet with the command:

appletviewer Hello.html

An appletviewer window should pop up displaying the applet, which just says Hello. Congratulations: you have just run your first Java program.

Now edit the source file so the string in quotations in the drawString line so it says hello to you. For example, if you liked to be called Sue, it would say Hello, Sue. Also change the first line so it contains your full name, not Chris Haynes. Then save, compile, and view your new customized applet.

When your applet is working properly, or a few minutes before the end of the first lab period, which ever comes first, submit your Hello.java file using the Vincent submission system link on the course home page.

Part 2

Experiment the effects of making more or less random changes to the Hello.java file. What sorts of error messages do you get? Error messages will not always be comprehensible, but by experimenting in this way you can get familiar with a wide range of possible messages and what they mean. Be sure to undo each change you make before making the next change, as the results of multiple errors may be especially hard to understand. Here are some things you might try:

  1. remove one of the opening braces
  2. remove one of the closing braces
  3. put an extra closing brace at the end
  4. change the Hello name after class, without changing the name of the file
  5. change the G in Graphics to lower case.
  6. remove each of the public words individually
  7. remove one of the import statements
  8. remove the first and then (as a separate test) the second of the double quote characters
  9. change one of the numbers to -100 (the applet will still compile, but it won't look right)

Part 3

Using the methods in Part 1, create a Java source file with the following code and then compile it and view it. Since the class is named GraphicsDemo, the file must be named GraphicsDemo.java.

// GraphicsDemo.java: demonstrate graphics for A112 by Chris Haynes

import java.applet.Applet;
import java.awt.*;

public class GraphicsDemo extends Applet {

   public void paint(Graphics g) {

      // this message is sent to the applet object, not the graphics object
      setBackground(Color.white);

      // Standard colors: black, blue, cyan,, darkGray, lightGray, gray, green, 
      // magenta, orange, pink, red, white, and yellow

      // x and y coordinates are integer pixel values, with origin upper left,
      // except for drawString, for which the origin is lower left.

      // Draw a string of characters
      g.drawString("Java Graphics Demo", 50, 50); // string, x, y

      // Draw a red line.
      g.setColor(Color.red);
      // width and height are also integer pixel values.
      g.drawLine(0, 0, 100, 100); // x, y, width, height

      // green rectangles
      g.setColor(Color.green);
      g.drawRect(10, 100, 20, 30); // x, y, width, height
      g.fillRect(50, 100, 20, 40); // x, y, width, height

      // blue ovals
      g.setColor(Color.blue);
      g.drawOval(10, 150, 60, 40); // x, y, width, height
      g.fillOval(100, 150, 20, 40); // x, y, width, height
      g.fillOval(200, 150, 50, 50); // equal width and height for a circle

      // magenta arcs
      g.setColor(Color.magenta);
      // Arc method arguments: x, y, width, height, starting angle, arc angle
      // Angles are in degrees measured counter clockwise from the right.
      g.drawArc(10, 220, 60, 40, 0, 270); 
      g.fillArc(100, 220, 50, 50, 90, 270);

      // Primitive shapes can be combined to form many other shapes.
      // Next we draw a white (background color) arc over a magenta arc
      // to form an upside-down U shape.
      g.fillArc(200, 220, 60, 60, 0, 180);
      g.setColor(Color.white);
      g.fillArc(215, 235, 30, 30, 0, 180);

   } 
}

Also create a corresponding file named GraphicsDemo.html by editing the Hello.html file, changing the file name from Hello.class to GraphicsDemo.class and changing both the width and height dimensions to 300 pixels. Compile, view, and study this applet.

Now make your own version of this applet. As always when you've changed a file, substitute your name in the first (comment) line. Keep the same method calls, but change all the integer values and colors to experiment with their effects. When you have viewed and understand the results, submit your version of GraphicsDemo.java via the Vincent system.

Programming Assignment

Create an applet that satisfies the following requirements:

  1. Source file named MyGraphics.java.
  2. Dimensions (width by height) should be 400 by 600.
  3. Draws at least 20 visible graphic drawing elements. (Visible means someone who knew Java well could look at the applet and count them.)
  4. Uses at least three kinds of visible graphic drawing elements (such as lines, arcs, and rectangles).
  5. Uses at least three colors.
  6. Use comments to describe what you are doing (but not all the details of how you are doing it, unless this is not obvious from the code). Often it is best to group several statements together and describe what the group is doing with a comment just before the group. Separate groups with blank lines when necessary to set them off.

Creativity of design is not required in this assignment, but hey, why not let your imagination run wild. It's fun!

To view your applet, you will also need to create a MyGraphics.html file with the appropriate dimensions, but do not submit this file.

Submit your MyGraphics.java file under Assignment 1 on using Vincent submission system. Be sure to read and carefully follow the Vincent submission instructions on the course web page.