<< Chapter < Page Chapter >> Page >

Listing 10 . Source code for the program named DotProd2D02.

/*DotProd2D02.java Copyright 2008, R.G.BaldwinRevised 03/07/08 This program allows the user to experiment with the dotproduct and the angle between a pair of GM02.Vector2D objects.Study Kjell through Chapter 9, The Angle Between Two Vectors.A GUI is provided that allows the user to enter four double values that define each of two GM02.Vector2Dobjects. The GUI also provides an OK button as well as two text fields used for display of computed results.In addition, the GUI provides a 2D drawing area. When the user clicks the OK button, the program draws thetwo vectors, one in black and the other in magenta, on the output screen with the tail of each vector located atthe origin in 2D space. The program also displays the values of the dot productof the two vectors and the angle between the two vectors in degrees.Tested using JDK 1.6 under WinXP. *********************************************************/import java.awt.*; import javax.swing.*;import java.awt.event.*; class DotProd2D02{public static void main(String[] args){GUI guiObj = new GUI(); }//end main}//end controlling class DotProd2D02 //======================================================//class GUI extends JFrame implements ActionListener{ //Specify the horizontal and vertical size of a JFrame// object. int hSize = 400;int vSize = 400; Image osi;//an off-screen imageint osiWidth;//off-screen image width int osiHeight;//off-screen image heightMyCanvas myCanvas;//a subclass of Canvas Graphics2D g2D;//off-screen graphics context.//User input components. JTextField vectorAx = new JTextField("50.0");JTextField vectorAy = new JTextField("100.0"); JTextField vectorBx = new JTextField("-100.0");JTextField vectorBy = new JTextField("-50.0"); JTextField dotProduct = new JTextField();JTextField angleDisplay = new JTextField(); JButton button = new JButton("OK");//----------------------------------------------------//GUI(){//constructor//Set JFrame size, title, and close operation. setSize(hSize,vSize);setTitle("Copyright 2008,R.G.Baldwin"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//Instantiate a JPanel that will house the user input// components and set its layout manager. JPanel controlPanel = new JPanel();controlPanel.setLayout(new GridLayout(0,4)); //Add the user input components and appropriate labels// to the control panel. controlPanel.add(new JLabel(" VectorAx = "));controlPanel.add(vectorAx); controlPanel.add(new JLabel(" VectorAy = "));controlPanel.add(vectorAy);controlPanel.add(new JLabel(" VectorBx = ")); controlPanel.add(vectorBx);controlPanel.add(new JLabel(" VectorBy = "));controlPanel.add(vectorBy);controlPanel.add(new JLabel(" Dot Prod = ")); controlPanel.add(dotProduct);controlPanel.add(new JLabel(" Angle (deg) = "));controlPanel.add(angleDisplay); controlPanel.add(button);//Add the control panel to the SOUTH position in the // JFrame.this.getContentPane().add( BorderLayout.SOUTH,controlPanel);//Create a new drawing canvas and add it to the// CENTER of the JFrame above the control panel. myCanvas = new MyCanvas();this.getContentPane().add( BorderLayout.CENTER,myCanvas);//This object must be visible before you can get an // off-screen image. It must also be visible before// you can compute the size of the canvas. setVisible(true);//Make the size of the off-screen image match the// size of the canvas. osiWidth = myCanvas.getWidth();osiHeight = myCanvas.getHeight();//Create an off-screen image and get a graphics // context on it.osi = createImage(osiWidth,osiHeight); g2D = (Graphics2D)(osi.getGraphics());//Translate the origin to the center.GM02.translate(g2D,0.5*osiWidth,-0.5*osiHeight);//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//----------------------------------------------------////This method is used to draw orthogonal 2D axes on the // off-screen image that intersect at the origin.private void setCoordinateFrame(Graphics2D g2D){ //Erase the screeng2D.setColor(Color.WHITE); GM02.fillRect(g2D,-osiWidth/2,osiHeight/2,osiWidth,osiHeight); //Draw x-axis in REDg2D.setColor(Color.RED); GM02.Point2D pointA = new GM02.Point2D(new GM02.ColMatrix2D(-osiWidth/2,0)); GM02.Point2D pointB = new GM02.Point2D(new GM02.ColMatrix2D(osiWidth/2,0)); new GM02.Line2D(pointA,pointB).draw(g2D);//Draw y-axis in GREEN g2D.setColor(Color.GREEN);pointA = new GM02.Point2D( new GM02.ColMatrix2D(0,-osiHeight/2));pointB = new GM02.Point2D( new GM02.ColMatrix2D(0,osiHeight/2));new GM02.Line2D(pointA,pointB).draw(g2D); }//end setCoordinateFrame method//----------------------------------------------------////This method is called to respond to a click on the // button.public void actionPerformed(ActionEvent e){//Erase the off-screen image and draw the axes. setCoordinateFrame(g2D);//Create two ColMatrix2D objects based on the user// input values. GM02.ColMatrix2D matrixA = new GM02.ColMatrix2D(Double.parseDouble(vectorAx.getText()), Double.parseDouble(vectorAy.getText()));GM02.ColMatrix2D matrixB = new GM02.ColMatrix2D(Double.parseDouble(vectorBx.getText()), Double.parseDouble(vectorBy.getText()));//Use the ColMatrix2D objects to create two Vector2D// objects. GM02.Vector2D vecA = new GM02.Vector2D(matrixA);GM02.Vector2D vecB = new GM02.Vector2D(matrixB);//Draw the two vectors with their tails at the origin. g2D.setColor(Color.BLACK);vecA.draw( g2D,new GM02.Point2D(new GM02.ColMatrix2D(0,0)));g2D.setColor(Color.MAGENTA); vecB.draw(g2D,new GM02.Point2D(new GM02.ColMatrix2D(0,0))); //Compute the dot product of the two vectors.double dotProd = vecA.dot(vecB);//Eliminate exponential notation in the display. if(Math.abs(dotProd)<0.001){ dotProd = 0.0;}//end if//Convert to four decimal digits and display. dotProd =((int)(10000*dotProd))/10000.0;dotProduct.setText("" + dotProd); //Compute the angle between the two vectors.double angle = vecA.angle(vecB);//Eliminate exponential notation in the display. if(Math.abs(angle)<0.001){ angle = 0.0;}//end if//Convert to four decimal digits and display. angle =((int)(10000*angle))/10000.0;angleDisplay.setText("" + angle); myCanvas.repaint();//Copy off-screen image to canvas.}//end actionPerformed //====================================================////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 appear on the// screen or when the repaint method is called on the // Canvas object.//The purpose of this method is to display the // off-screen image on the screen.public void paint(Graphics g){ g.drawImage(osi,0,0,this);}//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