<< Chapter < Page | Chapter >> Page > |
This is a very simple method, which should not require further explanation. I am presenting it here mainly for the purpose of leadinginto a discussion of the vulnerabilities that this method could expose on Point and Vector objects instantiated from the original math class named GM2D01 ,
You may recall from the module titled Getting Started that the constructor for the Point class in the original library received and saved a reference to an object of the ColMatrix class that contained the two values required to define the Point . Now that it is possible to change the values stored in an object of the ColMatrix class, if such an object is used to instantiate an object of the Point class and then the values stored in the ColMatrix object are changed, the values that define the Point object will change accordingly. That is not good.
Updated constructor for the Point class in GM2D02
Continuing with code in the GM2D02 library, Listing 20 shows the updated constructor in the new version of the library.
Listing 20 . Updated constructor for the Point class in GM2D02. |
---|
public static class Point{
GM2D02.ColMatrix point;Point(GM2D02.ColMatrix point){//constructorthis.point =
new ColMatrix(point.getData(0),point.getData(1));}//end constructor |
The new version of the constructor for the Point class creates and saves a clone of the ColMatrix object used to define the point to prevent the point from being corrupted by a later change in the valuesstored in the original ColMatrix object through the use of its new setData method.
Updated constructor for the Vector class in GM2D02
Because the constructor for the Vector class in the original library was essentially the same as the constructor for the Point class, the addition of the setData method to the ColMatrix class created the same vulnerability for objects instantiated from the Vector class. Essentially the same correction was made for the constructor for the Vector class. You can view the new code in Listing 26 .
Additional changes to the library
In addition to the changes described above, the following changes were made to the game-math library to update it from version GM2D01 to GM2D02 . All of the changes were straightforward and shouldn't require an explanationbeyond the embedded comments. You can view the new code for all of these changes in Listing 26 .
The drawOffscreen method of the program named PointLine04
Most of the differences between the programs named PointLine03 and PointLine04 occur in the method named drawOffscreen . I will concentrate my discussion there.
Switching now to code in the program named PointLine04 , the drawOffscreen method begins in Listing 21 .
Listing 21 . The drawOffscreen method of the program named PointLine04. |
---|
void drawOffscreen(Graphics2D g2Da,Graphics2D g2Db){
//Draw a label on each off-screen image.g2Da.drawString("Off-screen image A",
osiWidth/8,osiHeight/8);
g2Db.drawString("Off-screen image B",osiWidth/8,
osiHeight/8); |
Notification Switch
Would you like to follow the 'Game 2302 - mathematical applications for game development' conversation and receive update notifications?