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

Questions & Answers

Explain the following terms . (1) Abiotic factors in an ecosystem
Nomai Reply
Abiotic factors are non living components of ecosystem.These include physical and chemical elements like temperature,light,water,soil,air quality and oxygen etc
Qasim
what is biology
daniel Reply
what is diffusion
Emmanuel Reply
passive process of transport of low-molecular weight material according to its concentration gradient
AI-Robot
what is production?
Catherine
Pathogens and diseases
how did the oxygen help a human being
Achol Reply
how did the nutrition help the plants
Achol Reply
Biology is a branch of Natural science which deals/About living Organism.
Ahmedin Reply
what is phylogeny
Odigie Reply
evolutionary history and relationship of an organism or group of organisms
AI-Robot
ok
Deng
what is biology
Hajah Reply
cell is the smallest unit of the humanity biologically
Abraham
what is biology
Victoria Reply
what is biology
Abraham
HOW CAN MAN ORGAN FUNCTION
Alfred Reply
the diagram of the digestive system
Assiatu Reply
allimentary cannel
Ogenrwot
How does twins formed
William Reply
They formed in two ways first when one sperm and one egg are splited by mitosis or two sperm and two eggs join together
Oluwatobi
what is respiration
Deborah
what is genetics
Josephine Reply
Genetics is the study of heredity
Misack
how does twins formed?
Misack
What is manual
Hassan Reply
Got questions? Join the online conversation and get instant answers!
Jobilize.com Reply

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