| CIT
591 Assignment 1: Simple Drawing Fall 2005, David Matuszek |
There are three parts to this assignment.
Since we haven't yet had a chance to actually talk about Java in class, this is a sort of tutorial on BlueJ, where I give you the code to enter. One thing you need to know in advance is that capitalization matters. Capitalize everything in the program exactly as shown.
To begin:
(your name)
on the fifth line with your name (omit the parentheses). On line 29, near
the bottom of the program, replace // put your code here
with the following line:System.out.println("Hello, World!");Hello, World!While there is a Save command on the BlueJ menu, you don't need to use it. Your project is automatically saved for you, each time you compile.
An applet is a program that runs only in a Web page. Fortunately, BlueJ will create the Web page for you, if you like. This applet will just create and display a simple drawing.
import java.awt.*;
import javax.swing.*;
public class Drawing extends JApplet {
public void paint(Graphics g) {
g.setColor(Color.BLACK);
g.drawRect(10, 10, 80, 80); // x, y, width, height
g.setColor(Color.BLUE);
g.fillRect(110, 10, 80, 80);
g.setColor(Color.DARK_GRAY);
g.drawOval(210, 10, 80, 80);
g.setColor(Color.CYAN);
g.fillOval(310, 10, 80, 80);
g.setColor(Color.LIGHT_GRAY);
// startAngle, degrees
g.drawArc(10, 110, 80, 80, 0, 45);
g.setColor(Color.MAGENTA);
g.fillArc(110, 110, 80, 80, 45, 90);
g.setColor(Color.ORANGE);
// x, y, width, height, arcWidth, arcHeight
g.drawRoundRect(210, 110, 80, 80, 40, 40);
g.setColor(Color.PINK);
g.fillRoundRect(310, 110, 80, 80, 20, 60);
g.setColor(Color.RED);
// startX, startY, endX, endY
g.drawLine(10, 210, 90, 290);
g.drawString("ABC123", 110, 210 + 50);
// Now for something more complicated
g.setColor(Color.BLACK);
g.drawPolygon(new int[] { 250, 290, 210 },
new int[] { 210, 290, 290 }, 3);
g.setColor(Color.YELLOW);
g.fillPolygon(new int[] { 320, 350, 380, 310, 390 },
new int[] { 290, 210, 290, 240, 240 }, 5);
}
}
|
300 for Height and 400
for Width, and click Ok. If all goes well, you should get
a variety of figures, in a Create a third project named MyDrawingProject, and create an applet
class called MyDrawing. Use the following code to get started:
import java.awt.*; import javax.swing.*; public class MyDrawing extends JApplet { public void paint(Graphics g) { // Replace this line with your code } }
Try to make sense of the above applet, then create a drawing of your own. Draw something recognizable (a person, a house, a car, etc.), not something abstract. It will help if you understand the x-y coordinate system (described briefly below), but you don't have to understand everything; you should be able to do this assignment by imitating my applet, copying lines and changing numbers as necessary.
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.)
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 (as in the previous applet), then the bottom right corner is X=400, y=300.
Most of the drawing commands take place within a rectangle defined by (x, y, width, height), where x is the number of pixels in from the left edge and y is the number of pixels down from the top edge. Values outside the applet window are legal, but are not drawn on the screen.
Here is a brief explanation of the commands I used in my drawing applet. (If you want a more detailed explanation, take your browser to http://java.sun.com/j2se/1.5.0/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.)
|
MyDrawing.
For this assignment, you only need to turn in the one file, which should be named
MyDrawing.java. Turn in your program electronically, using Blackboard.
(See my Instructions
for Using Zip Files and Blackboard).
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 be willing to help your classmates if they ask for help, but basically you should do the work yourself.