<< Chapter < Page | Chapter >> Page > |
Drawing the points
Once again, an if statement is embedded in the code in Listing 6 to determine whether or not to draw the new points on the off-screen image as thenew GM2D04.Point objects are instantiated and saved in the array. If the points are drawn, they will be drawn in RED due to the code in Listing 5 .
Drawing the lines
The code in Listing 7 is essentially the same as the code in Listing 4 . This code tests the variable named drawLines to determine whether or not to draw lines connecting the new points in the current drawing color (RED) .
Listing 7 . Draw the lines if drawLines is true. |
---|
if(drawLines){//Instantiate and draw lines if true.
for(int cnt = 0;cnt<numberPoints-1;cnt++){
line = new GM2D04.Line(newPoints[cnt],
newPoints[cnt+1]);
line.draw(g2D);}//end for loop
//Draw the remaining line required to close the// polygon.
line = new GM2D04.Line(newPoints[numberPoints-1],
newPoints[0]);
line.draw(g2D);}//end if
}//end drawOffScreen |
End of the method
Listing 7 also signals the end of the method named drawOffScreen .
The actionPerformed method
As mentioned earlier, the actionPerformed method, shown in Listing 8 , is called to respond to each click on the Replot button shown in Figure 1 .
Listing 8 . The actionPerformed method. |
---|
public void actionPerformed(ActionEvent e){
//Get user input values and use them to modify several// values that control the translation and the
// drawing.numberPoints = Integer.parseInt(
numberPointsField.getText());vector.setData(
0,Double.parseDouble(vectorX.getText()));vector.setData(
1,Double.parseDouble(vectorY.getText()));if(drawPointsBox.getState()){
drawPoints = true;}else{
drawPoints = false;}//end elseif(drawLinesBox.getState()){
drawLines = true;}else{
drawLines = false;}//end else//Instantiate two new array objects with a length
// that matches the new value for numberPoints.points = new GM2D04.Point[numberPoints];newPoints = new GM2D04.Point[numberPoints];//Draw a new off-screen image based on user inputs.
drawOffScreen(g2D);myCanvas.repaint();//Copy off-screen image to canvas.
}//end actionPerformed |
Behavior of the actionPerformed method
Basically this method:
End of the program discussion
That ends the discussion of the program named VectorAdd05 .
I promised you earlier that I would explain a different approach to modifying the vertices of the geometric object in order to translate it. That approach isused in the program named VVectorAdd05a , which you will find in Listing 19 .
Notification Switch
Would you like to follow the 'Game 2302 - mathematical applications for game development' conversation and receive update notifications?