For this program, I'm not giving you a lot of directions or hints
in the specifications, which means that I want you to look at this
applet and figure out how to do things.
However, example4.java
is quite similar to what you need to do for this program.
In fact, a lot of homework4.java is already written for you in example4.java.
So before starting on this program,
I would spend a couple of hours getting familiar with example4.java.
You don't have to write your program the way I've written example4.java,
but as always, feel free to use parts of the example in writing the code for the homework.
The basic features of the applet are:
When the mouse enters one of the arrows, you scroll through the map
in the direction of the arrow (this is done with animation).
When you enter a building number into the JTextField, the map is
centered at that building, and a little box is drawn around the building.
EXTRA-CREDIT: You can click the JButton and the entire image of the map is
displayed inside of a JFrame (if you don't do the extra credit, the JButton should
still included--it just won't do anything when clicked).
The basic class organization of my program is that the JApplet contains
a ControlPanel in the SOUTH region and a TopPanel in the CENTER region of the JApplet.
The TopPanel contains an ImagePanel in its center and ArrowPanels all around the
ImagePanel. The way I made mine look this way was to have in the TopPanel:
in the NORTH region of the TopPanel
a JPanel with ArrowPanels in this JPanels WEST, CENTER, and
EAST regions;
in the CENTER region of the TopPanel a JPanel with ArrowPanels in
the WEST and EAST regions, and with the ImagePanel (where the image is
actually displayed) in the CENTER region;
in the SOUTH region of the TopPanel
a JPanel with ArrowPanels in this JPanels WEST, CENTER, and EAST regions.
The size of your applet in the HTML file should be approximately the
size of mine (my applet is 500 wide x 300 high).
Control Panel
You should have the JComponents seen in the applet above.
I used a FlowLayout where the components are centered,
and put in a horizontal gap between components.
The map should be centered at the building number given in the JTextField.
You should go to this building if the JButton is clicked or if the user hits
enter in the JTextField (note that doing both of these things generates an
ActionEvent). Try this in the applet above.
If a bad building number is given (whether it is non-integer or an integer
that is < 0 or > 115), the following should occur (try this in the applet above):
the text of the JTextField should be removed (which can be done by setting
the text to an empty string, for example, textField.setText(""); ).
the focus should be given to the JTextField (with code such as
textField.requestFocus();), so that the user knows to re-enter a number;
since there is no building currently selected, the box that had been
around a building is no longer shown.
To check that it is an integer or not, use a try-catch block.
See the code in homework3.java for an example of this
(in my solution to homework3.java,
see where we want to get the RGB values, in which case we
want to make sure what is entered is an integer between 0 and 255).
Each of the JComponents (including the JPanel where the image
is drawn) should have a toolTipText set similar
to what mine does above.
Program example4.java does some things that are similar to what
is done in this program.
TopPanel/ImagePanel
Here is the image.
You can simply right click on it and then do
"Save Image As..." to save it to your own account
in order to work with it. Let me know if you have any trouble
doing this. You can see example4.java for how to load it
into your program.
The code for the ArrowPanel class can be found in example4.java.
You can use it exactly as given there.
You can also find the ArrowPanel class
here.
When the mouse enters an ArrowPanel, that ArrowPanel should
be highlighted and the map should move in that direction.
When the mouse exits an ArrowPanel, that ArrowPanel should
be unhighlighted (returned to its original color), and the
map should stop moving in that direction.
If we get to an edge of the map, we should stop moving in
that direction. If we are scrolling diagonally and we reach
the edge of the image in one direction but not the other, we should
stop moving in the direction where the edge is reached but we should
continue moving in the direction where no edge is yet reached.
If we click on any point in the map, the map should now
be centered at that point (unless the point is near an edge
of the image, in which we case we try to center the map as close
as we can to the point clicked).
The way that we move to a particular building in the image
is by moving to a particular location (x-y-coordinates, given in pixels).
Here
are the x-y-coordinates that correspond to each building number.
So for example, if building 1 is wanted, you should center the image
at location (848, 140).
You can simply cut and paste these numbers into your own program.
(I actually had to spend the time to figure out the x-y-coordinates
of each building.) You'll notice that on the map there are also
buildings 117 and 118, but no 116, which is why I stopped at 115.
Once a building has been highlighted, it should stay highlighted,
even when you start to move through the image.
The only way for a building to become unhighlighted is if another
building becomes highlighted or if the user enters a bad building number,
in which case no building is highlighted.
Be sure to experiment with the applet above to see
how your program should be.
Program example4.java does some things that are similar to what is
done in this program.
EXTRA-CREDIT: JFrame for Entire Image
This part is worth up to 10 extra credit points. Note: you can get
at most 100% on the total homework grade for this class.
When the JButton "View Entire Image" is clicked, a JFrame
appears with the entire image inside of it.
You can get information on the JFrame on the web (javax.swing.JFrame)
and/or by looking at examples in the book which use the JFrame
(look in the index for instances of JFrame).
Actually, the JFrame contains a Scrollbar, which contains the image.
We haven't covered the Scrollbar class. You can get the basics on it
by looking at its documentation on the web (java.awt.Scrollbar).
Be sure that your JFrame can be closed (when the user clicks on the
top right hand corner of the JFrame, a WindowEvent is generated, and
the result should be the closing of the window, which in this case
is the JFrame. The has several examples of this sort of thing.
The files to submit
homework4.java
homework4.class
homework4.html
At the top of your .java file, be sure to include your
name,
login ID,
student number,
assignment number, and the
honesty pledge.
Clarifications and answers to specfic questions from students