- Login to SUnix and go to your PHY307 directory (type
cd public_html/PHY307). Create a new directory with the
command mkdir Simple2. Change its permissions with
the command chmod 755 Simple2. Now execute the command
cp Simple/*.java Simple2. This copies all the .java files
in your old subdirectory Simple to the new subdirectory Simple2.
- Change to this new directory by typing cd Simple2. Rename
the file Simple.java by typing mv Simple.java Simple2.java.
Create a
new .html file by typing pico Simple2.html. Inside this
put the line of code
< applet code=Simple2.class height=300 width=300 > < /applet >
- You now have set up everything so that you can create a new version
of the Simple Applet which will be called Simple2 and will
(hopefully) illustrate new features of Java.
- Make sure you are in the subdirectory Simple2 (if you type
pwd you should get a line which ends in Simple2). If you
are in public_html just type cd PHY307/Simple2 to enter the
Simple2 subdirectory.
- Edit Simple2.java (type pico Simple2.java). First change
the name Simple on the second line to Simple2.
- Under the line private Button mybutton; add the
line
private int counter=0;
This defines a new variable
which will count the number of
times the iterate button has been clicked.
- To store the coordinates of the points produced by the map we
need to use some arrays or lists. A list called x is
denoted x[]. The first element of that list is denoted x[1],
the second x[2] etc.
- Directly under the previous line defining the counter
variable add the line
private int x[],y[];
- This line defines the lists/arrays of integers x,y. To
create them we need to use the new operator. Inside the init()
method (after the add(mybutton) line) type the code
x = new int[100];
y = new int[100];
This reserves space for 100 elements in each list or array.
Also in init() replace the number 2.5 in the method call
setA(2.5) by 3.75.
- OK, look at the paint() method. Delete the line involving the
method drawString() (since we will now be drawing points to the
screen rather than printing them out). In its place add the
line
for(int i=0;i < counter-1;i++)
g.drawLine(x[i],y[i],x[i+1],y[i+1]);
This draws lines between successive pairs of (x,y) coordinate points which are
stored in the arrays x[] and y[]. The for loop
runs progressively through all values of the integer variable i
drawing a new bit of line between successive points indexed by i
until you cover all pairs of points (limit is counter). The code
fragment i++ adds 1 to i.
- Finally replace the arguments to the fillRect() method
to 50,50,200,200.
- Next go to the method action. After the line mymap.iterate()
add the line addPoint();. This adds a new point after
each iteration to the list of points to be plotted.
- Finally we have to define the contents of this new method
addPoint(). After the
closing curly brace of the paint() method add the following
code
private void addPoint(){
double xscale=10.0;
double yscale=200.0;
int xoffset=50;
int yoffset=250;
int ix,iy;
ix=xoffset+(int)(xscale*counter);
iy=yoffset-(int)(yscale*mymap.getX());
x[counter]=ix;
y[counter]=iy;
counter++;
if (counter==21) counter=0;
}
How does this work ? First, the pair of numbers (x,n) (the value of x and
the number of iterations) must be converted to pixel values on the
computer screen (i.e integers which run from 1 through 300 - the size of
the applet). This is what is accomplished by the first 2 lines. Notice that
we have defined x and y offsets on the screen and x and y scaling factors
which do the conversion to screen coordinates. Then
these new pixel coordinates are added to the arrays x[],y[]. Finally
the repaint(); command calls the paint() method and causes
the points to be plotted to the screen. The final if statement
just makes sure that you can only plot up to 21 iterations after which it
starts again.
- Make all these changes, recompile the code (javac *.java),
change the file permissions (chmod 644 *.class and
chmod 644 Simple2.html) and load the applet (point Netscape at
web.syr.edu/yourSUnixuserid/PHY307/Simple2/Simple2.html). Does it work ?