<< Chapter < Page Chapter >> Page >

The overridden paint method will be called when the JFrame and the Canvas appear on the screen or when the repaint method is called on the Canvas object.

The method is also called when some screen activity requires the JFrame to be redrawn (such as minimizing and then restoring the JFrame ) .

The purpose of the paint method in this program is to call the drawImage method twice in succession to draw the two off-screen images on the screen in the side-by-side format shown in Figure 1 .

The first parameter to the drawImage method is the off-screen image that is to be drawn on the canvas. The second and third parameters specifythe location where the upper-left corner of the image will be drawn.

The fourth parameter is a reference to an ImageObserver object, which is essentially a dummy image observer in this case, because an actual imageobserver isn't needed. (To learn more about image observers, see The AWT Package, Graphics- Introduction to Images .)

End of the program

Listing 18 also signals the end of the GUI class and the end of the program named PointLine03 ..

The program named PointLine04

A complete listing of this program is provided in Listing 28 . Just like the previous program named PointLine03 , this program produces the screen output shown in Figure 1 . However, it produces that output in a significantly different way.

This program emphasizes the differences between graphics objects and underlying data objects. It also illustrates the use of the new setData methods of the Point class and the Vector class, along with the new setTail and setHead methods of the Line class. These methods were added to the game-math library when it was updated to the GM2D02 version.

Dealing with some vulnerabilities

The addition of the new set methods exposed some vulnerabilities in the original version of the game-math library. I will explain how the codein the library was modified to deal with that issue. In particular, constructors were updated to ensure that existing points, vectors, and lines arenot corrupted by using the new set methods to change the values in the ColMatrix and/or Point objects originally used to construct the points, vectors, and lines. The updated constructors create and saveclones of the ColMatrix and/or Point objects originally used to define the Point , Vector , and/or Line objects.

Will explain the code in fragments

As before, I will explain the code in fragments. Some of the code in this program is identical to or very similar to code that I have alreadyexplained in the program named PointLine03 . I won't repeat that explanation. Instead, I will concentrate on the differences between thetwo programs.

The setData method of the ColMatrix class

The new setData method of the ColMatrix class is shown in Listing 19 . Note that this code is part of the GM2D02 library. At this point, I will begin switching back and forth between the library and the programnamed PointLine04 .

Listing 19 . The setData method of the ColMatrix class.
public void setData(int index,double data){ if((index<0) || (index>1)){ throw new IndexOutOfBoundsException();}else{ this.data[index]= data; }//end else}//end setData method

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