<< Chapter < Page Chapter >> Page >

Listing 8 calls the getTail and getHead methods to get the x and y coordinate values (relative to the current coordinate frame) that define the ends of the line segment represented by the GM2D02.Line object on which the draw method was called.

These four values are passed as parameters to the constructor for the Java standard Line2D.Double class to define the endpoints of a Line2D.Double object.

Then the draw method of the Graphics2D class is called on the reference to the incoming graphics context parameter to cause the Line2D.Double object to be rendered onto that graphics context.

In this case, that graphics context is an off-screen graphics context.However, it could just as easily be an on-screen graphics context such as the graphics context that is received as a parameter to an overridden paint method.

Two different draw methods are involved:

The code in my custom draw method, which is a member of my custom GM2D02.Line class, turns around and calls a completely different method named draw , which is a member of the standard Java Graphics2D class.

Hallmarks of object-oriented programming

An object of the GM2D02.Line class knows how to produce a visual manifestation of itself onto a specified graphics context as illustrated by theborders surrounding the left and right images in Figure 1 .

This is one of the hallmarks of object-oriented programming. Objects know how to douseful things for themselves. Later on, we will see other hallmarks such as the ability of an object of the GM2D02.Vector class to add itself to another object of the same class and return a new vector object that is the sum of thetwo vectors.

Draw a visual manifestation of a GM2D02.Point object

Returning to the drawOffScreen method of the program named PointLine03 , Listing 9 begins by calling the translate method of the Graphics class on the off-screen image shown in the left side of Figure 1 for the purpose of changing the origin from the upper-left corner of the graphics context to thecenter of the graphics context.

Listing 9 . Draw a visual manifestation of a GM2D02.Point object.
g2Da.translate(osiWidth/2.0,osiHeight/2.0);GM2D02.Point origin = new GM2D02.Point( new GM2D02.ColMatrix(0.0,0.0));origin.draw(g2Da);

This is equivalent to changing the coordinate frame described by Kjell and discussed in the previous module.

Then the code in Listing 9 defines a GM2D02.Point object located at the origin of the new coordinate frame and calls the draw method on that object to produce the visual manifestation of the object in the center of theleft image in Figure 1 .

Once again, the GM2D02.Point object is an underlying data object. The visual manifestation will be produced by anobject of the Java standard class named Ellipse2D.Double , which will be a completely independent object.

The draw method of the GM2D02.Point class

Listing 10 shows the draw method that was added to the GM2D02.Point class to produce the updated game-math library. This method draws a smallcircle around the location of the point on the specified graphics context.

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