This is a continuation of the calculator program you started in project 8. You will now be adding a graphical user interface plus the graphing capability.
You will be continuing to work on your program from project 8; so go to that directory, and make sure that everything is committed to your Mercurial repository.
You should be able to compile and run the Interpreter
program. Nothing that you do in this part should interfere with that
working. You will need to change the node and interpreter classes
only if you discover a mistake that you need to fix.
There are two main parts for you to do: designing the window for the GUI and providing the graphing functionality. In addition, this program provides a good exercise in handling exceptions; you should know enough to do that already. By the time you complete lab 16 on April 12, you should know everything you need to complete the first part. We will cover the material you need to do the graphing in class and lab on April 13 and 15.
In this part, you will design and implement
the window that will be shown to the user.
The class GraphCalc
has a main()
method which is the main method of the whole program,
and its primary purpose is to build the window.
As a reminder, Dr. VanDrunen's solution to this project looks like this:
You are not required to mimic this window exactly, but your program should have a window with the basic features/elements found here.
I am providing for you a class called
PaintPanel
which will make doing graphics
easier.
It is a component which you can include in the window, and it will
reserve the space to draw your graph. More information about this
class appears below, in the section about drawing the graph.
Your task is to finish the main()
method of GraphCalc
.
I have included the parts that need to be somewhere in the
window: JTextField
s for the function and
the x and y min and max, a button, and a PaintPanel
.
You may decide to rearrange these.
At any rate, you need to add them to the window,
plus any panels, layout managers, etc.
Don't worry about the fact that the window doesn't
do anything.
In this part, your only concern is the window's appearance.
You can run GraphCalc
to see what it looks like, and
close the window to get the program to exit.
Be sure to commit when you have this done.
Before you get to drawing the graph, you could add a class
implementing ActionListener
as the
listener for the button. That listener will need to get all of the
text from the JTextField
s in the window and verify that
they make some degree of sense.
For right now, you could simply print a message to the terminal to indicate that the button was pressed and whether the fields were good. Or you could get a little fancier and print out the function's value at, say, the minimum x.
Doing that robustly will almost certainly require that you catch some exceptions. If you get this done correctly, your program should never crash, no matter what the user does in the input fields.
If you'd like to get just a tiny bit fancier, you could add another label to your window, and display your message there instead of (or in addition to) printing it on terminal.
Finally, you need to make a graph appear on
the PaintPanel
whenever the button is pressed (and all of
the fields are valid). That will require making a class that
implements the Painter
interface to do the actual
drawing.
Note that one way to do this is to have the same class that implements
ActionListener
also implement Painter
.
If your actionPerformed()
has stashed all of the (parsed)
input in instance variables, those are exactly what the
paint()
method will need to do its job.
Your paint()
method will need to:
Graphics
class is in the upper lefthand corner.
This means that the y value -9 would correspond to
coordinate 190.
y
axis; if 0 is within the y range, then draw the x
axis.
Finally, you will need to have your action listener call
repaint()
on your PaintPanel
so that graph
gets drawn.
Take a little time to go back over your work so that you make sure everything works. In particular:
Painter.java
or PaintPanel.java
.
java Interpreter
to
evaluate an expression.
GraphCalc.java
that indicates the status of your GUI.
.java
files.
Turn in your .java
source files with a command
$ /cslab/class/csci235/bin/handin project9 *.java
DUE: Wednesday, April 27, at 5:00 pm.