Science and Computers -- PHY307/607
Lab 6. -
Creating your own Mapping Applet
More simple Java
Basics
- Login in to SUnix and move to your PHY307 directory
(type cd public_html/PHY307 at the SUnix prompt). Create a new
directory with the command mkdir Simple. Change its
permissions with command chmod 755 Simple.
- Go to the Labs section of the PHY307 page and download the two
Java source codes Simple.java and Map.java to
the new Simple directory.
- Compile them with the command javac *.java. Change
permissions on the .class files with the usual
chmod 644 *.class.
- Create a page to launch this new applet from using pico:
pico Simple.html. Put the following line in this
file
< applet code=Simple.class width=250 height=250 > < /applet >
- Change its permissions chmod 644 Simple.html.
- Point Netscape at web.syr.edu/~your SUnixuserid/PHY307/Simple/Simple.html.
What do you see ?
OK, lets see how Java does this. Use pico to edit the file Simple.java (i.e pico Simple.java). The first line of the code should
read
import java.awt.*;
This line allows you to access Java's graphical capabilities.
The next line reads
public class Simple extends java.applet.Applet{
This line tells Java that this object will be an applet (strictly will
inherit all the usual characteristics of the applet class) and will
be called Simple (note: the name of the file Simple.java always
corresponds to the name in the class definition - here Simple).
Note that this applet has only 2 user-defined methods - the
init() method and the paint() method.
The init() method
When you first load the
applet the init() method is executed. In this case it has only
one real function -- to create another object called Map and to
set a couple of data items in Map
(the setA() and setX()
method calls). Map is of course, just an object designed to
hold the data and methods appropriate to a description of the
logistic map. It has thus two data items (the value of x and
a) and a number of methods to set and get the values of these. It also
has an iterate() method to update the value of x
using the logistic function.
Notice how one calls these methods eg. mymap.setA(2.5);.
The
variable mymap is a reference to the newly created
Map object and the dot operator allows you to access the
method setA() which is defined within the class Map. This method has an argument (2.5 in this case) which is the
value you want to set the parameter a to.
The paint() method
This method contains only the single line
g.drawString("x is "+mymap.getX(),100,125);
g.drawString is a method which is used to print text and/or
numbers to the screen. In this case it prints the
words `x is' together with the numerical value of x.
This is gotten using the method call mymap.getX(). Where is it
printed ? Imagine the screen is like an xy-plot. The x-axis
runs between 0 and 250 (the width of the applet as specified in
the applet tag) and the y-axis similarly. The (x,y) coordinates of
the beginning of the word are just the last two arguments to
drawString() -- here (100,125).
Finally end your editing session of Simple.java by quitting pico without
saving anything.
The Map object
OK, type pico Map.java to take a look at this Java code now.
You should see the first line is just a class definition
public class Map{
(Map is not
a graphical or applet type object so you don't need the extends java.applet.Applet stuff).
Then there are a couple of lines which define x,a to
be decimal numbers and private to this class.
Then follow a set of methods which we have already
described to set and get the values of x,a and to
do an update of x according to the logistic
function.
Making some improvements
- Edit Simple.java to add the following
lines inside the paint() method (i.e between its opening
and closing curly braces and before the call to g.drawString())
g.setColor(Color.yellow);
g.fillRect(25,25,200,200);
g.setColor(Color.red);
This changes the color to yellow and calls another method fillRect() to draw a rectangle of size 200 by 200 with its bottom lefthand corner
at coordinates (25,25). It then changes the color to red in preparation
for the call to drawString(). Exit pico saving your
changes.
- Recompile the Java codes (javac *.java) and reload the
applet (holding down the shift key while hitting Reload
forces Netscape to reload Java class
files). What do you see ?
- OK, lets do something else now. Edit Simple.java and add
the line
mymap.iterate();
just before the drawString()method call. Exit, recompile and reload the applet. What do you now see ?
You should see that the value of x has now changed as it should and
the new value has been written to the screen.
- It would be nice to have a button on the screen which when you click
it automatically iterates the function and writes the new x value to
the screen. Lets do this now.
- First we need to define a button type. At the top of Simple.java
underneath the line private Map mymap add the line
private Button mybutton;
.
- Inside the init() method add lines to create the
button and add it to the screen:
mybutton = new Button("Iterate");
add(mybutton);
- We're almost there. Finally we must add code that registers when
the button is clicked and calls iterate. To do that we must add
a new method action() to the Simple object/class.
Directly after the paint() method (and before the final
closing curly brace of the class definition) add the code
public boolean action(Event evt, Object arg){
if(evt.target instanceof Button){
mymap.iterate();
repaint();
return true;
}
return false;
}
- This catches clicks of the button,
issues the method call mymap.iterate() and then calls
repaint() which basically just calls the paint()
method again. Finally, delete the original mymap.iterate() call
you had put into the paint() method.
- Exit pico, recompile and reload. What happens on clicking the button ?
To get credit for this lab you need to
- Email me the url of your applet and (as an attachment) all your
source .java files. If you use the email client pine you attach files
using the ctrl-J key sequence once you have positioned the
cursor over the attachment header line.
The deadline to get these things done is next Thursday.
Back to the PHY307 Homepage
This page maintained by Simon Catterall, last updated 6 October, 1998.