<< Chapter < Page Chapter >> Page >

What does the actionPerformed method do?

There is nothing complicated about the code in Listing 12 . This code performs the following actions as indicated by the embedded comments:

  • Call the setCoordinateFrame method to reset the coordinate frame to world coordinates.
  • Get user input values and use them to compute and save new offset values that will be used to define the origin of the new coordinate frame.
  • Call the drawOffScreen method to erase the current image from the off-screen image and draw a new image on it using the new coordinate frame.Note that the drawOffScreen method will call the setCoordinateFrame method to establish the new coordinate frame before drawing the new image on the off-screen image.
  • Call the repaint method to cause the off-screen image to be copied to the canvas and displayed on the computer screen.

The setCoordinateFrame method

This method, which is shown in Listing 13 , is used to set the coordinate frame of the off-screen image by setting the origin to the specified offsetvalues relative to origin of the world coordinate frame.

Listing 13 . The setCoordinateFrame method.
private void setCoordinateFrame( Graphics2D g2D,double xOff,double yOff){//Paint the background white g2D.setColor(Color.WHITE);g2D.fillRect(0,0,osiWidth,osiHeight); //Translate the origin by the specified amountg2D.translate((int)xOff,(int)yOff); //Draw new X and Y-axes in BLACKg2D.setColor(Color.BLACK); g2D.drawLine(-(int)xOff,0,(int)(osiWidth-xOff),0);g2D.drawLine(0,-(int)yOff,0,(int)((osiHeight-yOff))); }//end setCoordinateFrame method

What does the setCoordinateFrame method do?

As mentioned earlier, the origin of the world coordinate frame is the upper-left corner of the off-screen image. The setCoordinateFrame method assumes that the current coordinate frame coincides with the world coordinateframe when the method is called. This is because the method changes the coordinate frame relative to the current coordinate frame.

The method begins by painting the background white erasing anything already there. (Note that this step erases the entire image only when the coordinate frame coincides with the world coordinate frame when the method is called.)

Then the method establishes the new coordinate frame by translating the origin of the off-screen image using the X and Y offset values received asincoming parameters.

Finally, the method draws black orthogonal axes intersecting at the origin of the new coordinate frame on the off-screen image.

Beginning of the drawOffScreen method

The code for the method named drawOffScreen begins in Listing 14 . This method is called once in the constructor for the GUI class (see Listing 21 ) , and again each time the user clicks the Replot button shown in Figure 2 .

The purpose of this method is to create some Vector objects and to cause visual manifestations of the Vector objects to be drawn onto an off-screen image.

Listing 14 . Beginning of the drawOffScreen method.
void drawOffScreen(Graphics2D g2D){ setCoordinateFrame(g2D,xOffset,yOffset);

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