<< Chapter < Page Chapter >> Page >

( Line2D.Double is a class in the standard Java library.)

Testing:

All of the programs in this module were tested using JDK 1.7 running under Windows XP.

The draw method

The draw method belonging to an object of the Graphics2D class is used to draw the two line segments on a Canvas object for which the origin has been translated to the center of the Canvas . The result is shown in Figure 1 .

( Graphics2D and Canvas are classes in the standard Java library.)

Figure 1 Graphics illustration of four points and two lines.

Missing image.

The coordinate values of the points and the selection of point-pairs to specify the ends of the line segments is such that the final rendering is a pairof orthogonal lines that intersect at the origin.

(You could think of these lines as the axes in a Cartesian coordinate system.)

Will explain in fragments

I will present and explain this program in fragments. A complete listing of the program is provided in Listing 14 near the end of the module.

(Use the code in Listing 14 and the instructions provided above to write, compile, and run the program. This will be a good way for you to confirm that Java is properly installed on your computer and that you areable to follow the instructions to produce the output shown in Figure 1 .)

The first code fragment is shown in Listing 1 .

Listing 1 . The controlling class named PointLine01.
class PointLine01{ public static void main(String[]args){ GUI guiObj = new GUI();}//end main }//end controlling class PointLine01

Listing 1 shows the definition of the controlling class, including the main method for the program named PointLine01 .

The main method simply instantiates a new object of a class named GUI and saves that object's reference in the variable named guiObj.

(GUI is not a class in the standard Java library. It is defined below.)

The GUI object produces the screen image shown in Figure 1 .

The class named GUI

Listing 2 shows the beginning of the class named GUI , including the constructor for the class.

Listing 2 . Beginning of the class named GUI.
class GUI extends JFrame{ //Specify the horizontal and vertical size of a JFrame// object. int hSize = 200;int vSize = 200;GUI(){//constructor//Set JFrame size and title setSize(hSize,vSize);setTitle("R.G.Baldwin");//Create a new drawing canvas and add it to the // center of the JFrame.MyCanvas myCanvas = new MyCanvas(); this.getContentPane().add(myCanvas);setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);setVisible(true); }//end constructor

Listing 2 begins by declaring and initializing a pair of instance variables that will be used to specify the size of the JFrame shown in Figure 1 .

(A JFrame object in Java might be called a window in other programming environments.)

The constructor for the class named GUI

The constructor begins by using the two instance variables to set the size of the JFrame and by setting the title for the JFrame to R.G.Baldwin .

Create a new drawing canvas and add it to the center of the JFrame

Then the constructor instantiates a Canvas object and adds it to the JFrame . Without getting into the details as to why, I will simply tell you that the Canvas object fills the entire client area of the frame, which is the area inside of the four borders.

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