Computer Graphics (CMSC 427) - Spring 2000

                                                Prof. Ben Bederson

                                                Homework One: Learning Visual J++

                                                Due: Tuesday, Feb 8th, 2000

 

The purpose of this homework is to introduce you to Java, and to Visual J++, the software development environment we will be using.   In addition, this will get you started thinking about generating graphics.  Note that the Java language is well-defined by Sun and one of its well-known features is that the same "class" files will run on many different computers and operating systems.  Visual J++, however, defines many potentially useful "language extensions" which take advantage of the Windows platform - but do not work on any other platform.  Since the point of this course is graphics, we will not take a position one way or the other on whether to use "pure Java", or the Microsoft language extensions.  It is up to you.

There are two principle versions of Java: "Java 1" and "Java 2".  Each major version has several minor versions.  For this class, we will be using "Java 1" which is the standard version of Java used on the Web (and by Visual J++).  If you look at the Microsoft site, you will not see mention of Java 2.  If you look at the Sun site, you will see that Java 1 is well supported, but they are pushing hard to move towards Java 2.  Java 2 does not change the language, but introduces a lot of new functionality through new libraries.

Step 1: Go to the CS lab we will be using for the course in 4140 A.V. Williams building and log in regular WAM account.  If you do not have a WAM account, you must get one immediately.  Start up Visual J++ from the Windows Program menu and go through the first 3 chapters of the Learn Visual J++ Now book. Type in the examples and explore the environment - try different things out.

Step 2:   Start reading the Sun Java tutorials.  Try taking some of their examples and getting them to work in Visual J++.  Be sure to only read the sections of the tutorial for Java 1.  You'll notice that these tutorials come with instructions for using Sun's compiler, usually called the JDK (the Java Development Kit).  However, the source code will work on either system even if the details of editing compiling and running are different. 

Step 3: Create a new empty Visual J++ project and write a program (i.e., set of classes) that draws a line between any two points using only the graphics primitive that draws a single dot.  You may start with the sample code below which creates a simple window that has event handlers to click.  The requirements for this homework are that:

In addition, your program must follow standard high-quality programming practices including:

Step 4: Turn in your homework on time (as specified in the syllabus).  To turn in your program you must use the submit program on the WAM machines to be described in class.


The following is a sample bit of Java code that you may find useful for this homework assignment.

/**
 * A demonstration application to be used as a start for homework #1
 * for CMSC 427 - Spring 2000.
 * 
 * @author Ben Bederson
 * @date January 25, 2000
 */
import java.awt.*;
import java.awt.event.*;

/**
 * The LineDrawing class is a placeholder to actually draw lines. It simply
 * creates a window with a drawing panel.
 */
public class LineDrawing extends Frame
{
    public LineDrawing() {
	setBounds(100, 100, 400, 400);    // Define the window dimensions
	Panel panel = new DrawingPanel(); // Create the drawing panel
	panel.setBounds(0, 0, 400, 400);  // Set the drawing panel size
	add(panel);                       // Add the drawing panel to the window
	setVisible(true);                 // Make the whole thing visible

		// We must define this event handler
		// to capture clicks on the "X" to
		// exit the program.
	addWindowListener(new WindowAdapter() {
	    public void windowClosing(WindowEvent e) {
		System.exit(0);
	    }
	});
    }

    /**
     * This gets called when the application starts up. The first thing
     * we do is create an instance of the main class.
     */
    public static void main(String[] args)
    {
	new LineDrawing();
    }
}

/**
 * This is the drawing panel that we use to draw on. We must create this class
 * extending Panel so that we can override the crucial 'paint' method which
 * gets called whenever the system determines that Panel needs to be redrawn.
 * We also define simple event handlers to determine when the user clicks
 * on the drawing panel.
 */
class DrawingPanel extends Panel
{
   private int x1, y1, x2, y2; // Start and end point of line

    /**
     * Simple constructor that adds a mouse listener to this panel.
     */
    public DrawingPanel() {
	MouseListener mouseListener = new DrawingMouseListener(this);
	addMouseListener(mouseListener);
    }

    /**
     * This method gets called whenever the panel needs to get repainted. 
     * Putting in drawing methods here, such as g.drawLine() will cause
     * those things to be drawn on the panel. For instance, if you add
     * g.drawLine(0, 0, 100, 100) the canvas will appear with a diagonal line.
     */
    public void paint(Graphics g) {
		// NOTE: This is the code that must be replaced with your own
		// line rasterizing algorithm.
	g.drawLine(x1, y1, x2, y2); // Draw the line connecting two points
    }

    /**
     * Specify the first point of the line to be drawn in this panel.
     */
    public void setPoint1(int x, int y) {
	x1 = x;
	y1 = y;
	repaint();
    }

    /**
     * Specify the secon point of the line to be drawn in this panel
     */
    public void setPoint2(int x, int y) {
	x2 = x;
	y2 = y;
	repaint();
    }
}

/**
 * A mouse listener for a DrawingPanel that passes the first and second
 * point of a line back to the panel with alternating mouse clicks.
 */
class DrawingMouseListener extends MouseAdapter {
    private DrawingPanel panel; // The panel to draw on
    private boolean firstPress = true; // True when expecting first mouse press

    /**
     * Constructor that receives the panel to draw on.
     */
    public DrawingMouseListener(DrawingPanel panel) {
	this.panel = panel;
    }

    /**
     * Mouse Press event listener that sets both points of the line on 
     * the first mouse click, and then sets the second point on the
     * second click - effectively letting the user draw a line one
     * with each two clicks.
     */
    public void mousePressed(MouseEvent e) {
	if (firstPress) {
	    panel.setPoint1(e.getX(), e.getY());
	    panel.setPoint2(e.getX(), e.getY());
	    firstPress = false;
	} else {
	    panel.setPoint2(e.getX(), e.getY());
	    firstPress = true;
	}
    }
}