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

                14.13 GUI Track and Problem Solving: Line Plot Example Revisited (Optional) 585
   y direction
x direction
 Figure 14.18 Sample output for the LinePlotGUI program
of the horizontal pixel positions for each of the points. The yPixels parameter holds an array of the vertical pixel positions for each of the points. For example, xPixels[0] and yPixels[0] hold pixel positions for the first point, xPixels[1] and yPixels[1] hold pixel positions for the second point, and so on. Figure 14.19’s drawPolyline method call displays a line that connects four points in the shape of an N.
          Apago PDF Enhancer
  public void paintComponent(Graphics g)
50 100
   {
int[] xPixels = {50, 50, 100, 100};
int[] yPixels = {140, 40, 140, 40};
g.drawPolyline(xPixels, yPixels, 4);
50
100
150
  } // end paintComponent
  Figure 14.19 An example drawPolyline method call that displays a line in the shape of an N Algorithm Development
Once you learn about the drawPolyline method, the LinePlotGUI program’s basic algorithm becomes clear: Fill up the xPixels and yPixels arrays with pixel values for a sequence of points. Then use those arrays to call drawPolyline.
Before fleshing out that basic algorithm with more details, you need to be aware of an important as- sumption. The plotted line’s points are evenly spaced along the x axis. More specifically, the points occur at positions x􏰀0, x􏰀1, x􏰀2, and so on. So when the program prompts the user for a point, only a y value is needed, not an x value (because x’s value is already known: x􏰀0, x􏰀1, x􏰀2, and so on).
















































































   617   618   619   620   621