<< Chapter < Page Chapter >> Page >

Repaint the canvas

Finally, Listing 2 calls the repaint method to repaint the canvas. If you have studied the previous modules in this collection, you should know whatthis does and an explanation should not be necessary.

Beginning of the drawOffScreen method

Listing 3 shows the beginning of the method named drawOffScreen . The purpose of this method is to create the GM2D04.Point objects that define the vertices of the geometric object and to manipulate those points to producethe desired results.

Listing 3 . Beginning of the drawOffScreen method.
void drawOffScreen(Graphics2D g2D){ //Erase the off-screen image and draw new axes, but// don't change the coordinate frame. setCoordinateFrame(g2D,xAxisOffset,yAxisOffset,false);//Create a set of Point objects that specify// locations on the circumference of a circle and // save references to the Point objects in an array.for(int cnt = 0;cnt<numberPoints;cnt++){ points[cnt]= new GM2D04.Point( new GM2D04.ColMatrix(50*Math.cos((cnt*360/numberPoints) *Math.PI/180),50*Math.sin((cnt*360/numberPoints) *Math.PI/180)));if(drawPoints){//Draw points if true points[cnt].draw(g2D); }//end if}//end for loop

Erase the image and draw the axes

Listing 3 begins by calling the setCoordinateFrame method to erase the off-screen image and draw orthogonal axes, intersecting at the origin on theerased image. Note however that unlike the call to the setCoordinateFrame method that was made in the constructor in Listing 2 , the call to the method in Listing 3 passes false as the fourth parameter, thereby preventing the location of the origin from being modified.

In other words, in this case, the method is being called simply to erase the off-screen image and to draw axes on the clean off-screen image.

(In hindsight, it may have been better to break this method into two separate methods; one to translate the origin and a second to erase theoff-screen image and draw the axes.)

Create the points that define the vertices

Then Listing 3 executes a for loop that instantiates the set of mathematical GM2D04.Point objects that define the vertices of the geometric object and saves references to those objects in the array object that was instantiated in Listing 2 . Note that during the first pass through the constructor and the method named drawOffScreen , the length of the array object and the number of points instantiated are both specified by the initial value (6) of the instance variable named numberPoints (see Listing 1 ) .

Some knowledge of trigonometry is required

I told you in an earlier module "I will assume that you either already have, or can gain the required skills in geometry andtrigonometry on your own." I included a module titled GAME 2302-0320 Brief Trigonometry Tutorial in this collection to assist you in that effort.

In order to understand the code in the for loop in Listing 3 , you must have at least a rudimentary knowledge of trigonometry.

For now, suffice it to say that this code will instantiate a set of GM2D04.Point objects equally spaced around the circumference of a circle with a radius of 50 units centered on the origin.

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