Page 622 - Introduction to Programming with Java: A Problem Solving Approach
P. 622
588 Chapter 14 Exception Handling
//*************************************************************
// This method prompts the user for y coordinates for points
// at positions x=0, x=1, etc.
public void readYCoordinates()
{
String yStr; // user's entry for a point's y coordinate
numOfPoints = getIntFromUser("Enter number of points: ");⎫ ⎬
maxY = getIntFromUser("Enter maximum point value: "); ⎭ yCoords = new double[numOfpoints];
for (int i=0; i<=maxX; i++)
}
// end readYCoordinates
{
}
// end for
yStr = JOptionPane.showInputDialog(
"At x = " + i + ", what is y's value?\n" +
"Enter an integer between 0 and " +
maxY + " inclusive:");
try
{
}
}
yCoords[i] = Integer.parseInt(yStr);
if (yCoords[i] < 0 || yCoords[i] > maxY)
{
}
JOptionPanAe.pshaowgMeossagPeDiFalogE(nnulhl,ancer
"Invalid entry. Value must be between 0 and " + maxY);
i--;
catch (NumberFormatException e)
{
JOptionPane.showMessageDialog(null,
"Invalid entry. Must enter an integer.");
i--;
These initializations use a helper method.
Figure 14.20b
LinePlotGUI class—part B
constructor. That’s a good idea for several reasons. One, it furthers the goal of modularization in that separate tasks are performed in separate modules. Two, it improves program speed. The LinePlotPanel constructor executes only one time, when the LinePlotPanel object is instantiated in main. The paintComponent method executes every time the user does something to alter the program’s win- dow. There’s no need to redo the drawing calculations every time that happens, so moving that code to the LinePlotPanel constructor works well.
For a line plot with a small number of points it would be no big deal if we’d made the mistake of put- ting all the drawing calculations code in the paintComponent method. But if there are many points, the