Assigned: Wed. 02 Oct 2002
NOTE CHANGE IN DUE DATE: Mon 14 Oct 2002
There has been a fairly major change in what is required in this lab. Originally, this lab had two main parts:
CHOOSE one or the other of these parts to complete for the lab. If you choose OPTION II, there is code on the L drive that will do the first part for you (see below for how to copy it over).
If you choose this option for the lab, you will write code that will create a colored circle under user direction.
This lab requires working with user events (Class Event) in Java. While you can set up your own listener for the mouse events, it is probably easier to work off of the events that are automatically listened for by the Applet class.
Note that all Java defined classes are on the web site Java api. While some of the class methods will be covered here, complete listings are on this web site.
We have already done labs where we overloaded the init() and paint() methods of the Applet class. This lab we will overload some more of this class' methods. The specific methods of interest may include:
Notice that each of these methods is passed the message of a class Event in addition to a location (x and y coordinates). Like Applet, Event is a Java defined class that is explained in detail on the sun web site (above). In particular, notice that an Event object can tell you things about when it happened, like if the Control key was down when the event happened (method controlDown).
Here is some example code that demonstrates how you could go about doing this part of the lab:
File 1: Applet1.java
import java.applet.*;
import java.awt.*;
public class Applet1 extends Applet {
public int last_x = 0;
public int last_y = 0;
// Start up the applet
public void init() { setSize(300,150); }
// What to do when the mouse is put down
public boolean mouseDrag(Event e, int x, int y) {
Graphics g = getGraphics();
MyLine line1 = new MyLine(x, y);
line1.addLine(g, last_x, last_y, x, y);
last_x = x;
last_y = y;
return true;
}
}
File 2: MyLine.java
import java.applet.*;
import java.awt.*;
public class MyLine {
// Constructor make a MyLine
public MyLine() {}
// Method addLine adds to MyLine
public boolean addLine(Graphics line_graphics, int start_x, int start_y,
int end_x, int end_y); {
line_graphics.drawLine(start_x,start_y,end_x,end_y);
}
}
If you choose this option for the lab, you take the DragApplet project from the L drive, put it onto your own drive and build on it. The changes that you will need to make to the DragApplet project include:
If you choose OPTION II, start by copying the code to the first part (creating a circle under user direction) over to your space:
In addition to drawing circles, this lab requires making testing variables and then changing your actions based on the results of those tests.
This means that you must be able to:
Working off of the code from the L drive, these changes should be made in the method "handleControlDownClick" method in the DragApplet class definition (at the bottom, in the "MY CODE" section).
As with all Java programs, you will need a "runner" program to run the functionality of the other classes by creating the objects and running their appropriate methods. There will also be other files (classes) that do the bulk of the work.
Recall that each class has its own file. The name of the class and the file MUST be the same, just as the classes constructor must have the same name as the class. (The file, of course, has a ".java" extention after its name.)
OPTION I of this lab: Will create one circle under user control (using the mouse).
OPTION II of this lab: Will work off of code that creates a circle using the mouse. Added on to this will be code that will change the color of the circle based on where the user does a mouse clicks with the control key pressed. So a circle is created when the mouse is pressed and dragged. The size of the circle is determined by the position of the mouse when the user releases it. Circles, when created, have a default color that is NOT red (of your choice). If there is a control mouse click (control key and mouse click) inside of the circle, its color changes to red. If there is a control mouse click (control key and mouse click) outside of the circle, then its color changes back to the default color.
CONTRARY TO PREVIOUS INSTRUCTIONS, DO NOT EXTEND ANY NEW CLASSES (I think that we already have enough class in this group...)
The files for this lab will be the same as the files for Lab4, with a few additions and modifications in order to handle the new functionality.
A circle must be created dynamically under user control (mouse control).
If you are starting off of the "mouse creation" code from drive L, your circle must be created in some default color of your choice that is not red, then change to red if the user "control-clicks" in the circle, then back to the default color if the user "control-clicks" outside of the circle.