<< Chapter < Page Chapter >> Page >

As before, the purpose of the drawOffscreen method is to define points, lines, and vectors and then to cause a visual manifestation of some of the points, lines, and vectors to bedrawn onto two separate off-screen images. Unlike before, however, this version of the method uses a minimum number of underlying data objects toproduce the output, thereby emphasizing the differences between graphics objects and underlying data objects.

The code in Listing 21 is essentially the same as before and doesn't require any explanation.

Borders

As before, and as shown in Figure 1 , this method draws borders on each of the off-screen images by drawing lines parallel to the edges of the off-screenimages. Each line is offset by one pixel toward the center of the off-screen image.

Instantiate a point at the upper-left corner

Listing 22 instantiates and saves an underlying data object of type Point containing coordinate values for the upper left corner. This object willbe used as one endpoint for a moveable Line object from which the top and left borders will be drawn.

Listing 22 . Instantiate a point at the upper-left corner.
GM2D02.Point upperLeftPoint = new GM2D02.Point( new GM2D02.ColMatrix(1.0,1.0));

Instantiate a moveable Point object

Listing 23 instantiates a Point object that will be used as an endpoint for a moveable line in all four locations.

Listing 23 . Instantiate a moveable Point object.
GM2D02.Point aPoint = new GM2D02.Point( new GM2D02.ColMatrix(osiWidth-1,upperLeftPoint.getData(1)));

The location of this point will be modified several times during the drawing of the border by calling the setData method on the point. The initial location of this point is the upper-right corner. (Recall that the origin is at the upper-left corner of both off-screen images atthis point in the program.) The call to the Point constructor causes the y-coordinate to be the same as the y-coordinate forthe Point object instantiated in Listing 22 .

Instantiate a moveable Line object

Listing 24 instantiates a Line object that will be modified several times in succession by calling its setHead and setTail methods to place it in four different locations.

Listing 24 . Instantiate a moveable Line object.
GM2D02.Line theLine = new GM2D02.Line( upperLeftPoint,aPoint);theLine.draw(g2Da); theLine.draw(g2Db);

This Line object will be used to draw the top, right, bottom, and left lines that make up the border. It is used in Listing 24 to cause a graphical Line2D.Double object to be instantiated and rendered as the line at the top of both of the off-screen images.

Relocate the Line to three more locations

Listing 25 calls various methods of the Point and Line classes (including setData , setTail , and setHead ) to relocate the GM2D02.Line object to three more locations, each parallel to an edge of an off-screen image.

Listing 25 . Relocate the Line to three more locations.
//Draw right border. //Save the previous head as the new tail.theLine.setTail(theLine.getHead()); //Modify the location of aPoint. There's no need to// change the x-coordinate. Just change the // y-coordinate to move the point down the screen in// the positive y direction to the lower-right corner. aPoint.setData(1,osiHeight-1);//Use the new location of aPoint as the new head. theLine.setHead(aPoint);theLine.draw(g2Da); theLine.draw(g2Db);//Draw bottom border. There's no need to change the// y-coordinate in the new point, which is located // at the lower-left corner.theLine.setTail(theLine.getHead()); aPoint.setData(0,1.0);theLine.setHead(aPoint); theLine.draw(g2Da);theLine.draw(g2Db); //Draw left border to close the rectangular border.theLine.setTail(theLine.getHead()); theLine.setHead(upperLeftPoint);theLine.draw(g2Da); theLine.draw(g2Db);

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