<< Chapter < Page Chapter >> Page >

Also, according to the Sun documentation, the method returns "an off-screen drawable image, which can be used for double buffering. The returnvalue may be null if the component is not displayable." The returned value is type Image .

The getGraphics method

There are eleven different versions of the getGraphics method in the JDK 1.7 standard library. The version of the method used in this programis defined in the Image class. This method is called on the Image object returned by the createImage method in Listing 3 .

According to the Sun documentation, this method creates a graphics context for drawing to an off-screen image. You will note that the reference tothe graphics context was cast to type Graphics2D and saved in a reference variable of the type Graphics2D . I explained the relationship between the Graphics class and the Graphics2D class in an earlier module.

At this point, we have two off-screen image objects of type Image and we have a graphics context on each of them. This makes it possible to usethe methods of the Graphics class and/or the methods of the Graphics2D class to draw on either of the off-screen images.

Remaining code in the constructor

Listing 4 shows the remainder of the constructor for the GUI class.

Listing 4 . Remaining code in the constructor.
drawOffscreen(g2Da,g2Db); myCanvas.repaint();}//end constructor

Listing 4 begins by calling the method named drawOffscreen to draw some points, lines, and vectors on the two off-screen images. Notethat references to each of the two graphics contexts are passed as parameters to the drawOffscreen method.

Then Listing 4 calls the repaint method belonging to the MyCanvas object to cause the overridden paint method belonging to that object to be executed. Later, we will see that the overridden paint method copies the two off-screen images to the canvas in the side-by-side format shownin Figure 1 causing the contents of those off-screen images to be displayed in a JFrame object.

Beginning of the drawOffscreen method

The beginning of the drawOffscreen method is shown in Listing 5 . The purpose of this method is to define points, lines, and vectors as underlyingdata objects and then to cause a visual manifestation of some of the points, lines, and vectors to be drawn onto the two off-screen images.

Listing 5 . Beginning of the drawOffscreen method.
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);

Before getting into the underlying data objects, however, Listing 5 calls the drawString method belonging to the Graphics class to draw the two text strings shown in Figure 1 on the two off-screen images.

Instantiate four Line objects that will be used to draw borders

Listing 6 instantiates four GM2D02.Line objects that will ultimately be used to draw the borders around the left and right images shown in Figure 1 .

Listing 6 . Define four lines that will be used to draw borders.
//First define four points that will be used to define // the ends of the four lines.GM2D02.Point upperLeftPoint = new GM2D02.Point( new GM2D02.ColMatrix(1.0,1.0));GM2D02.Point upperRightPoint = new GM2D02.Point( new GM2D02.ColMatrix(osiWidth-1,1.0));GM2D02.Point lowerRightPoint = new GM2D02.Point( new GM2D02.ColMatrix(osiWidth-1,osiHeight-1));GM2D02.Point lowerLeftPoint = new GM2D02.Point( new GM2D02.ColMatrix(1.0,osiHeight-1));//Now define the four lines based on the endpoints.GM2D02.Line top = new GM2D02.Line(upperLeftPoint, upperRightPoint);GM2D02.Line rightSide = new GM2D02.Line( upperRightPoint,lowerRightPoint); GM2D02.Line bottom = new GM2D02.Line(lowerLeftPoint,lowerRightPoint); GM2D02.Line leftSide = new GM2D02.Line(upperLeftPoint,lowerLeftPoint);

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