Page 620 - Introduction to Programming with Java: A Problem Solving Approach
P. 620

                586 Chapter 14 Exception Handling
Here’s a high-level description of the algorithm:
1. Prompt the user for the number of points and the maximum y value (maxY).
2. For each point, prompt the user for a y coordinate value and store the value in
yCoords.
3. Determinethenumberofhorizontalpixelsbetweenadjacentpoints(pixelInterval).
4. Fill up the xPixels array:
xPixels[i] ← i * pixelInterval
5. Fill up the yPixels array by scaling the values in the yCoords array:
yPixels[i] ← (yCoords[i]/maxY) * height in pixels of plotted line’s border
6. Call drawRect to display a border for the plotted line.
7. Call drawPolyline to display the plotted line.
Program Structure
Now that you have a high-level description of the algorithm, you might be tempted to immediately translate
it into source code. First, you should determine where the different parts of the program should go. As is
customary for a non-trivial program like this one, you should implement the solution with two classes—a
LinePlotGUI class for driving the program and a LinePlotPanel class for drawing the pictures.
Clearly, the drawRect and drawPolyline method calls should go in the LinePlotPanel class.,
   Organize your thoughts as an algorithm.
          since they involve drawing. But what about the code that prompts the user for y coordinate values, and what
Apago PDF Enhancer
about the code that calculates the xPixels and yPixels values? It’s important to have the code in the right class to ensure that each class has a clearly defined role. That helps with program development, read- ability, and maintainability. The LinePlotGUI class drives the program and input plays a big role in that effort. Therefore, you should put the user-prompt code in the LinePlotGUI class. The LinePlotPanel class draws the pictures, and calculations play a big role in that effort. Therefore, you should put the drawing- calculations code in the LinePlotPanel class.
Modularity
Now it’s time to look at the LinePlotGUI program’s source code. Note the modular nature of the LinePlotGUI class in Figures 14.20a, 14.20b, and 14.20c. The JFrame calls (setSize, setTitle, setDefaultCloseOperation) are in their own module, the LinePlotGUI constructor. The user- prompt code is in its own module, the readYCoordinates method. The readYCoordinates method prompts the user for the number of points. It also prompts the user for the maximum value on the y axis. The two inputs both need to undergo the same type of input validation. To avoid redundant code, the input validation code is in a common helping method, getIntFromUser. The readYCoordinates method also prompts the user for the y value of each point, which can be anywhere in the range between zero and the maximum y value.
Note the paintComponent method in Figure 14.21b. The paintComponent method is called automatically by the JVM when the program starts up and whenever a user does something to alter the program’s window (e.g., when the user resizes the window, or moves another window off of the window). The paintComponent method is in charge of drawing the window’s pictures. You could put the drawing- calculations code and the drawing graphics code together in the paintComponent method, but as shown in Figures 14.21a and 14.21b, the drawing-calculations code is in its own module, the LinePlotPanel










































































   618   619   620   621   622