<< Chapter < Page Chapter >> Page >

Complete program listings

Complete listings of the programs discussed above are provided in Listing 14 , Listing 15 , and Listing 16 below.

Listing 14 . Source code for the program named PointLine01.

/*PointLine01.java Copyright 2008, R.G.BaldwinThis program illustrates the implementation of the concept of a point and a line segment in Java.Four points (locations in space) are defined by using the coordinates of the four points as the x and y parametersto the constructor for the Point2D.Double class. This results in four objects of the Point2D.Double class.Two line segments are defined by using pairs of points as parameters to the Line2D.Double class. This resultsin two objects of the Line2D.Double class. The draw method belonging to an object of the Graphics2Dclass is used to draw the two line segments on a Canvas object for which the origin has been translated to thecenter of the Canvas. The coordinate values of the points and the selection ofpoint-pairs to specify the ends of the line segments is such that the final rendering is a pair of orthogonallines that intersect at the origin. Tested using JDK 1.6 under WinXP.*********************************************************/ import java.awt.geom.*;import java.awt.*; import javax.swing.*;class PointLine01{ public static void main(String[]args){ GUI guiObj = new GUI();}//end main }//end controlling class PointLine01//======================================================// 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 titlesetSize(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 //----------------------------------------------------////This is an inner class of the GUI class. class MyCanvas extends Canvas{//Override the paint() method. This method will be // called when the JFrame and the Canvas in its// contentPane are displayed on the screen. public void paint(Graphics g){//Downcast the Graphics object to a Graphics2D // object. The Graphics2D class provides// capabilities that don't exist in the Graphics // class.Graphics2D g2 = (Graphics2D)g;//By default, the origin is at the upper-left corner // of the canvas. This statement translates the// origin to the center of the canvas. g2.translate(this.getWidth()/2.0,this.getHeight()/2.0);//Define two points. Point2D pointA =new Point2D.Double(-this.getWidth()/2.5,0.0); Point2D pointB =new Point2D.Double(this.getWidth()/2.5,0.0); //Use the points to construct an object that// represents a line segment that connects the two // points. The values of the points causes this// line segment to be horizontal. Line2D.Double horizLine =new Line2D.Double(pointA,pointB);//Use the same procedure to construct an object that// represents a vertical line segment. Point2D pointC =new Point2D.Double(0.0,-this.getHeight()/2.5); Point2D pointD =new Point2D.Double(0.0,this.getHeight()/2.5); Line2D.Double vertLine =new Line2D.Double(pointC,pointD);//Draw the horizontal and vertical line segments on // the canvas.g2.draw(horizLine); g2.draw(vertLine);}//end overridden paint()}//end inner class MyCanvas}//end class GUI

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