<< Chapter < Page | Chapter >> Page > |
The code in Listing 29 also defines the constructor, whose only purpose is to save an integer identifying the position of this object in the vertical stack of MyCanvas objects.
The beginning of the overridden paint method for the MyCanvas class is shown in Listing 30 .
Listing 30. Beginning of the overridden paint method. |
---|
public void paint(Graphics g){
//Calculate the scale factorsxScale = width/(xMax-xMin);
yScale = height/(yMax-yMin);//Set the origin based on the
// minimum values in x and yg.translate((int)((0-xMin)*xScale),
(int)((0-yMin)*yScale));drawAxes(g);//Draw the axes
g.setColor(Color.BLACK); |
The code in Listing 30 :
The plotting process consists of drawing straight line segment between pairs ofpoints. For each line segment, one of the points is defined by a pair of old coordinate values. The other point is defined by a pair of new coordinate values.
The code in Listing 31 initializes the beginning point for the plot. The initial value for the x-coordinate is the left edge of the plotting area.
Listing 31. Get old coordinate values. |
---|
//Get initial data values
double xVal = xMin;int oldX = getTheX(xVal);
int oldY = 0;//Use the Canvas obj number to
// determine which method to// call to get the value for y.
switch(cnt){case 0 :
oldY = getTheY(data.f1(xVal));break;
case 1 :oldY = getTheY(data.f2(xVal));
break;case 2 :
oldY = getTheY(data.f3(xVal));break;
case 3 :oldY = getTheY(data.f4(xVal));
break;case 4 :
oldY = getTheY(data.f5(xVal));}//end switch |
The initial value for the y-coordinate depends on which function is being plotted on the MyCanvas object. Recall that each MyCanvas object contains an instance variable that identifies its position in the vertical stack of MyCanvas objects. The switch statement in Listing 31 uses that information to call one of the five methods named f1 through f5 . This gets the correct value for the y-coordinate based on the value of the x-coordinatefor each MyCanvas object.
The methods named getTheX and getTheY called by the code in Listing 31 convert the coordinate values from type double to integer values in pixels.
The method named getTheY also changes the sign on the data so that positive y-values go up the screen rather than down the screen.
(By default, positive vertical coordinate values go down the screen from top to bottom in Java.)
The remainder of the overridden paint method is shown in Listing 32 .
Plot the points. |
---|
//Now loop and plot the points
while(xVal<xMax){
int yVal = 0;//Get next data value. Use the
// Canvas obj number to// determine which method to
// invoke to get the value for y.switch(cnt){
case 0 :yVal =
getTheY(data.f1(xVal));break;
case 1 :yVal =
getTheY(data.f2(xVal));break;
case 2 :yVal =
getTheY(data.f3(xVal));break;
case 3 :yVal =
getTheY(data.f4(xVal));break;
case 4 :yVal =
getTheY(data.f5(xVal));}//end switch1
//Convert the x-value to an int// and draw the next line segment
int x = getTheX(xVal);g.drawLine(oldX,oldY,x,yVal);
//Increment along the x-axisxVal += xCalcInc;
//Save end point to use as start// point for next line segment.
oldX = x;oldY = yVal;
}//end while loop}//end overridden paint method |
Notification Switch
Would you like to follow the 'Digital signal processing - dsp' conversation and receive update notifications?