<< Chapter < Page Chapter >> Page >

Nothing new here

There is nothing in Listing 6 that is new relative to what you learned in the previous module. Listing 6 starts by defining four GM2D02.Point objects that will be used to define the ends of the line segments. References to these Point objects are passed to the constructor for the GM2D02.Line class to instantiate the four Line objects.

Note that the locations of the points, and hence the positions of the lines were one pixel away from the edges of the canvas towards the center of thecanvas. These locations were chosen to ensure that the resulting visual manifestations of the lines would be visible on the canvas.

It is also extremely important to understand that the GM2D02.Point and GM2D02.Line objects instantiated in Listing 6 are not graphical objects. Rather, they are underlying data objects, which are suitable for use inmathematical operations.

Draw a visual manifestation of each line

Listing 7 calls the draw method of the GM2D02.Line class four times in succession for each off-screen image. Note that a reference tothe off-screen image is passed as a parameter to the draw method each time the method is called on one of the GM2D02.Line objects.

Listing 7 . Draw a visual manifestation of each line.
//Now draw a visual manifestation of each line // on g2Da.top.draw(g2Da); rightSide.draw(g2Da);bottom.draw(g2Da); leftSide.draw(g2Da);//Now draw a visual manifestation of each of the same// four lines on g2Db top.draw(g2Db);rightSide.draw(g2Db); bottom.draw(g2Db);leftSide.draw(g2Db);

As you will see shortly, each of these calls to the draw method causes an object of the standard Java Line2D.Double class to be rendered onto the specified off-screen image. When the off-screen image is ultimatelycopied to the canvas object by the overridden paint method, this produces a visual manifestation of the GM2D02.Line object.

Once again, however, it is very important to understand that the Line2D.Double object is a graphical object and the GM2D02.Line is an underlying data object. They are completely independent objects.

Also, it is important to understand that the Line2D.Double class is a member of the standard Java library,whereas the GM2D02.Line class is a member of my special game math library named GM2D02 .

The draw method of the GM2D02.Line class

Now it's time to explain one of the methods that was added to the original game-math library named GM2D01 to produce the new library named GM2D02 . At this point, I will start switching back and forth between code in the GM2D02 library and code in the program named PointLine03 .

Listing 8 shows the draw method that was called repeatedly on the GM2D02.Line objects in Listing 7 . This code belongs to the GM2D02 library.

Listing 8 . The draw method of the GM2D02.Line class.
public void draw(Graphics2D g2D){ Line2D.Double line = new Line2D.Double(getTail().getData(0), getTail().getData(1),getHead().getData(0), getHead().getData(1));g2D.draw(line); }//end draw

Construct and render a Line2D.Double object on the 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