<< Chapter < Page Chapter >> Page >

The code in Listing 1 declares a large number of instance variables,initializing some of them. Those variables shouldn't require an explanation beyond the embedded comments. I show them here solely to make it easy foryou to refer to them later when I discuss them.

Extends JFrame and implements ActionListener

This class extends the JFrame class and implements the ActionListener interface. As you will see later, implementing the ActionListener interface requires that the class contains a concrete definition of the actionPerformed method. It also makes an object of the GUI class eligible for being registered as a listener object on the Replot button shown in Figure 1 .

Abbreviated constructor for the GUI class

An Abbreviated listing of the constructor for the GUI class is shown in Listing 2 . Much of the code was deleted from Listing 2 for brevity. You can view the code that was deleted in Listing 18 .

Listing 2 . Abbreviated constructor for the GUI class.
GUI(){//constructor //Instantiate the array objects that will be used to// store the points that define the vertices of the // geometric object.points = new GM2D04.Point[numberPoints];newPoints = new GM2D04.Point[numberPoints];//Code that creates the user interface was deleted// for brevity.//Code dealing with the canvas and the off-screen image // was deleted for brevity.//Erase the off-screen image and draw the axes setCoordinateFrame(g2D,xAxisOffset,yAxisOffset,true);//Create the Point objects that define the geometric // object and manipulate them to produce the desired// results. drawOffScreen(g2D);//Register this object as an action listener on the// button. button.addActionListener(this);//Cause the overridden paint method belonging to // myCanvas to be executed.myCanvas.repaint();}//end constructor

Arrays for storage of vertices

The constructor begins by instantiating two array objects that will be used to store references to the GM2D04.Point objects that define the vertices of the geometric object.

Set the coordinate frame

Further down in Listing 2 , the constructor calls the method named setCoordinateFrame to erase the off-screen image, draw orthogonal axes on it, and translate the originto a new location. The code in the setCoordinateFrame method is straightforward and shouldn't require an explanation beyond the embeddedcomments. You can view that code in Listing 18 .

The fourth parameter to the setCoordinateFrame method is used to determine whether or not to translate the origin by the amount given by thesecond and third parameters. If true, the origin is translated. If false, the origin is not translated.

The method named drawOffScreen

After setting the coordinate frame, Listing 2 calls the method named drawOffScreen . I will discuss that method in some detail shortly.

Register an ActionListener object

Next, the code in Listing 2 registers the object of the GUI class as an action listener on the Replot button shown in Figure 1 . This causes the method named actionPerformed to be executed whenever the user clicks the Replot button. I will also discuss the actionPerformed method shortly.

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