Due midnight, Monday, October 30th.
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).
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.
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.
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:
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.
Create an applet that satisfies the following requirements:
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.