<< Chapter < Page | Chapter >> Page > |
Listing 9 . Source code for the program named DotProd2D01.
/*DotProd2D01.java
Copyright 2008, R.G.BaldwinRevised 03/04/08
Study Kjell through Chapter 8 - Length, Orthogonality, andthe Column Matrix Dot product.
The purpose of this program is to confirm proper operationof the ColMatrix2D.dot method.
The program creates a GUI that allows the user to enterthe first and second values for a pair of ColMatrix2D
objects along with a button labeled OK.When the user clicks the OK button, the dot product
between the two ColMatrix2D objects is computed. Theresulting value is converted to four decimal digits and
displayed in a text field on the GUI.Tested using JDK 1.6 under WinXP.
*********************************************************/import java.awt.*;
import javax.swing.*;import java.awt.event.*;
class DotProd2D01{public static void main(String[]args){
GUI guiObj = new GUI();}//end main
}//end controlling class DotProd2D01//======================================================//
class GUI extends JFrame implements ActionListener{//Specify the horizontal and vertical size of a JFrame
// object.int hSize = 400;
int vSize = 150;JTextField colMatA0 = new JTextField("0.707");
JTextField colMatA1 = new JTextField("0.707");JTextField colMatB0 = new JTextField("-0.707");
JTextField colMatB1 = new JTextField("-0.707");JTextField dotProduct = 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(" colMatA0 = "));
controlPanel.add(colMatA0);controlPanel.add(new JLabel(" colMatA1 = "));
controlPanel.add(colMatA1);controlPanel.add(new JLabel(" colMatB0 = "));controlPanel.add(colMatB0);controlPanel.add(new JLabel(" colMatB1 = "));
controlPanel.add(colMatB1);controlPanel.add(new JLabel(" Dot Prod = "));controlPanel.add(dotProduct);
controlPanel.add(new JLabel(""));//spacercontrolPanel.add(button);
//Add the control panel to the CENTER position in the// JFrame.
this.getContentPane().add(BorderLayout.CENTER,controlPanel);
setVisible(true);//Register this object as an action listener on the
// button.button.addActionListener(this);
}//end constructor//----------------------------------------------------//
//This method is called to respond to a click on the// button.
public void actionPerformed(ActionEvent e){//Create two ColMatrix2D objects.
GM02.ColMatrix2D matrixA = new GM02.ColMatrix2D(Double.parseDouble(colMatA0.getText()),
Double.parseDouble(colMatA1.getText()));GM02.ColMatrix2D matrixB = new GM02.ColMatrix2D(Double.parseDouble(colMatB0.getText()),
Double.parseDouble(colMatB1.getText()));//Compute the dot product.double dotProd = matrixA.dot(matrixB);//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);
}//end actionPerformed//====================================================//
}//end class GUI
Notification Switch
Would you like to follow the 'Game 2302 - mathematical applications for game development' conversation and receive update notifications?