<< Chapter < Page Chapter >> Page >

Listing 12 . Source code for the program named DotProd3D02.

/*DotProd3D02.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.Vector3D objects.Study Kjell through Chapter 10, Angle between 3D Vectors. A GUI is provided that allows the user to enter sixdouble values that define each of two GM02.Vector3D objects. The GUI also provides an OK button as well astwo text fields used for display of computed results. In addition, the GUI provides a 3D drawing area.When the user clicks the OK button, the program draws the two vectors, one black and the other in magenta, on theoutput screen with the tail of each vector located at the origin in 3D space.The program also displays the values of the dot product of the two vectors and the angle between the two vectorsin degrees. Tested using JDK 1.6 under WinXP.*********************************************************/ import java.awt.*;import javax.swing.*; import java.awt.event.*;class DotProd3D02{ public static void main(String[]args){ GUI guiObj = new GUI();}//end main }//end controlling class DotProd3D02//======================================================// 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 image int osiWidth;//off-screen image widthint osiHeight;//off-screen image height MyCanvas myCanvas;//a subclass of CanvasGraphics2D g2D;//off-screen graphics context. //User input components.JTextField vecAx = new JTextField("50.0"); JTextField vecAy = new JTextField("100.0");JTextField vecAz = new JTextField("0.0"); JTextField vecBx = new JTextField("-100.0");JTextField vecBy = new JTextField("-50.0"); JTextField vecBz = new JTextField("0.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,6));//Add the user input components and appropriate labels // to the control panel.controlPanel.add(new JLabel(" VecAx = ")); controlPanel.add(vecAx);controlPanel.add(new JLabel(" VecAy = ")); controlPanel.add(vecAy);controlPanel.add(new JLabel(" VecAz = "));controlPanel.add(vecAz);controlPanel.add(new JLabel(" VecBx = ")); controlPanel.add(vecBx);controlPanel.add(new JLabel(" VecrBy = "));controlPanel.add(vecBy);controlPanel.add(new JLabel(" VecBz = ")); controlPanel.add(vecBz);controlPanel.add(new JLabel(" Dot Prod = "));controlPanel.add(dotProduct);controlPanel.add(new JLabel(" Ang(deg)")); controlPanel.add(angleDisplay);controlPanel.add(new JLabel(""));//spacercontrolPanel.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 3D axes on the// off-screen image that intersect at the origin. private void setCoordinateFrame(Graphics2D g2D){//Erase the screen g2D.setColor(Color.WHITE);GM02.fillRect(g2D,-osiWidth/2,osiHeight/2, osiWidth,osiHeight);//Draw x-axis in RED g2D.setColor(Color.RED);GM02.Point3D pointA = new GM02.Point3D( new GM02.ColMatrix3D(-osiWidth/2,0,0));GM02.Point3D pointB = new GM02.Point3D( new GM02.ColMatrix3D(osiWidth/2,0,0));new GM02.Line3D(pointA,pointB).draw(g2D);//Draw y-axis in GREEN g2D.setColor(Color.GREEN);pointA = new GM02.Point3D( new GM02.ColMatrix3D(0,-osiHeight/2,0));pointB = new GM02.Point3D( new GM02.ColMatrix3D(0,osiHeight/2,0));new GM02.Line3D(pointA,pointB).draw(g2D);//Draw z-axis in BLUE. Make its length the same as the // length of the x-axis.g2D.setColor(Color.BLUE); pointA = new GM02.Point3D(new GM02.ColMatrix3D(0,0,-osiWidth/2)); pointB = new GM02.Point3D(new GM02.ColMatrix3D(0,0,osiWidth/2)); new GM02.Line3D(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 ColMatrix3D objects based on the user // input values.GM02.ColMatrix3D matrixA = new GM02.ColMatrix3D( Double.parseDouble(vecAx.getText()),Double.parseDouble(vecAy.getText()), Double.parseDouble(vecAz.getText()));GM02.ColMatrix3D matrixB = new GM02.ColMatrix3D(Double.parseDouble(vecBx.getText()), Double.parseDouble(vecBy.getText()),Double.parseDouble(vecBz.getText()));//Use the ColMatrix3D objects to create two Vector3D // objects.GM02.Vector3D vecA = new GM02.Vector3D(matrixA); GM02.Vector3D vecB = new GM02.Vector3D(matrixB);//Draw the two vectors with their tails at the origin.g2D.setColor(Color.BLACK); vecA.draw(g2D,new GM02.Point3D(new GM02.ColMatrix3D(0,0,0))); g2D.setColor(Color.MAGENTA);vecB.draw(g2D,new GM02.Point3D( new GM02.ColMatrix3D(0,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