<< 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:

  • Gets input values from the user input components shown at the bottom of Figure 1 .
  • Uses the input values to set the values of certain variables.
  • Instantiates new array objects of the correct length to contain the points that define the vertices of the geometric objects.
  • Calls the drawOffScreen method to generate the new point and line objects and display them on the canvas.

End of the program discussion

That ends the discussion of the program named VectorAdd05 .

The program named VectorAdd05a

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 .

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Game 2302 - mathematical applications for game development. OpenStax CNX. Jan 09, 2016 Download for free at https://legacy.cnx.org/content/col11450/1.33
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Game 2302 - mathematical applications for game development' conversation and receive update notifications?

Ask