This page is optimized for mobile devices, if you would prefer the desktop version just click here

1.11 Game 2302-0130: putting the game-math library to work  (Page 10/20)

(Each time you click the Replot button, the object can be seen to move from the initial location, not the current location, to the new location.)

As with the program named VectorAdd05 , this program uses the addVectorToPoint method of the GM2D04.Point class to translate a geometric object from one location in space to a different location in space. Inthis program, however, the translation is performed in 100 incremental steps to produce an animation. As shown in Figure 5 , the animated geometric object is drawn in red to make it visually distinct from the original object. The originalobject is not erased from the display.

The polygon

The program initially constructs and draws a black polygon in the upper-left corner of the canvas as shown in Figure 4 . (The polygon is a hexagon in Figure 4 .) The six points that define the vertices of the hexagon lie on a circle with a radius of 50 units. The points at the vertices and the lines thatconnect the points are drawn initially.

Also as shown in Figure 4 , a GUI is provided that allows the user to specify the following items and click a Replot button to cause the animation to begin:

  • The number of points that define the geometric object (polygon) .
  • X-component of the displacement vector.
  • Y-component of the displacement vector.
  • A checkbox to specify whether points are to be drawn.
  • A checkbox to specify whether lines are to be drawn.

As in the previous program, changing the number of points causes the number of vertices that describe the geometric object to change, allowing you to createtriangles, rectangles, pentagons, hexagons, circles, etc.

Changing the components of the displacement vector causes the geometric object to be translated to a different location. Checking and unchecking thecheckboxes causes the points and/or the lines to either be drawn or not drawn.

Animation performance

On the computer that I am now using, which is not a new one (September 2012), the animation becomes slightly jerky at about 3500 points when both the points and the lines are drawn.

Will explain in fragments

I will explain this program in fragments and will avoid repeating explanations that I have previously given. A complete listing of this program isprovided in Listing 20 near the end of the module.

Much of the code in this program is very similar to code that I explained earlier. The thing that is new about this program is the animation aspect of theprogram. Therefore, I will concentrate mainly on the animation code in my explanation of the program.

The class named MyCanvas, the update method, and the paint method

Before getting into the details of the program, I want to explain the inner workings of the class named MyCanvas that I defined for this program. This class, which is an inner class of the GUI class, is shown in its entirety in Listing 10 .

Listing 10 . The class named MyCanvas, the update method, and the paint method.
class MyCanvas extends Canvas{ public void update(Graphics g){paint(g);//Call the overridden paint method. }//end overridden update()//--------------------------------------------------// public void paint(Graphics g){g.drawImage(osi,0,0,this); }//end overridden paint()}//end inner class MyCanvas
<< Chapter < Page Page > Chapter >>

Read also:

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.
Jobilize.com uses cookies to ensure that you get the best experience. By continuing to use Jobilize.com web-site, you agree to the Terms of Use and Privacy Policy.