Project 9: A graphing calculator (part 2)

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.

I. Introduction

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.

II. Displaying the window

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:

picture of the program

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: JTextFields 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.

III. Handling some action

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 JTextFields 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.

IV. Drawing the graph

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:

Finally, you will need to have your action listener call repaint() on your PaintPanel so that graph gets drawn.

V. Turn in

Take a little time to go back over your work so that you make sure everything works. In particular:

Turn in your .java source files with a command

$ /cslab/class/csci235/bin/handin project9 *.java

DUE: Wednesday, April 27, at 5:00 pm.


Thomas VanDrunen, Cary Gray
Last modified: Fri Apr 8 10:56:45 CDT 2016