package euclid;
import java.math.BigInteger;
import javax.swing.JOptionPane;
/**
*
* @author Shane
*/
public class Euclidgui extends javax.swing.JFrame {
/**
* Creates new form Euclidgui
*/
public Euclidgui() {
initComponents();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jFormattedTextField1 = new javax.swing.JFormattedTextField();
jInternalFrame1 = new javax.swing.JInternalFrame();
jTextField1 = new javax.swing.JTextField();
input2Text = new javax.swing.JTextField();
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
input1Text = new javax.swing.JTextField();
computeGcdButton = new javax.swing.JButton();
ResetButton = new javax.swing.JButton();
lcmButton = new javax.swing.JButton();
answerLabel = new javax.swing.JLabel();
jScrollPane1 = new javax.swing.JScrollPane();
gcdTextAnswer = new javax.swing.JTextPane();
jLabel3 = new javax.swing.JLabel();
jFormattedTextField1.setText("jFormattedTextField1");
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jInternalFrame1.setTitle("Greatest Common Divisor");
jInternalFrame1.setVisible(true);
jTextField1.setText("Greatest Common Divisor Program");
jLabel1.setText("Enter X:");
jLabel2.setText("Enter Y:");
computeGcdButton.setText("Compute gcd(x, y)");
computeGcdButton.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
computeGcdButtonMouseClicked(evt);
}
});
ResetButton.setText("Reset");
ResetButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
ResetButtonActionPerformed(evt);
}
});
lcmButton.setText("Compute lcm(x, y)");
lcmButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
lcmButtonActionPerformed(evt);
}
});
answerLabel.setText("gcd(x, y)");
jScrollPane1.setViewportView(gcdTextAnswer);
jLabel3.setText("lcm(x, y)");
javax.swing.GroupLayout jInternalFrame1Layout = new javax.swing.GroupLayout(jInternalFrame1.getContentPane());
jInternalFrame1.getContentPane().setLayout(jInternalFrame1Layout);
jInternalFrame1Layout.setHorizontalGroup(
jInternalFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jInternalFrame1Layout.createSequentialGroup()
.addGroup(jInternalFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jInternalFrame1Layout.createSequentialGroup()
.addGap(133, 133, 133)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(jInternalFrame1Layout.createSequentialGroup()
.addGap(38, 38, 38)
.addGroup(jInternalFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addGroup(jInternalFrame1Layout.createSequentialGroup()
.addGroup(jInternalFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(computeGcdButton)
.addGroup(jInternalFrame1Layout.createSequentialGroup()
.addComponent(jLabel1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(input1Text, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGap(42, 42, 42)
.addComponent(jLabel2)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(input2Text, javax.swing.GroupLayout.PREFERRED_SIZE, 121, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(jInternalFrame1Layout.createSequentialGroup()
.addComponent(answerLabel)
.addGap(18, 18, 18)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 203, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(jLabel3))
.addGroup(jInternalFrame1Layout.createSequentialGroup()
.addComponent(lcmButton)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(ResetButton)
.addGap(23, 23, 23)))))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
jInternalFrame1Layout.setVerticalGroup(
jInternalFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jInternalFrame1Layout.createSequentialGroup()
.addContainerGap()
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addGroup(jInternalFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(input2Text, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel1)
.addComponent(jLabel2)
.addComponent(input1Text, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 31, Short.MAX_VALUE)
.addGroup(jInternalFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jInternalFrame1Layout.createSequentialGroup()
.addComponent(jLabel3)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED))
.addGroup(jInternalFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jInternalFrame1Layout.createSequentialGroup()
.addComponent(answerLabel)
.addGap(54, 54, 54))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jInternalFrame1Layout.createSequentialGroup()
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 160, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18))))
.addComponent(computeGcdButton)
.addGap(18, 18, 18)
.addGroup(jInternalFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(lcmButton)
.addComponent(ResetButton))
.addGap(30, 30, 30))
);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jInternalFrame1)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jInternalFrame1)
);
pack();
}// </editor-fold>
private void computeGcdButtonMouseClicked(java.awt.event.MouseEvent evt) {
BigInteger input1 = BigInteger.ZERO;
BigInteger input2 = BigInteger.ZERO;
try{
input1 = new BigInteger(input1Text.getText());
}catch(Exception e){
JOptionPane.showMessageDialog(this, "Bad X Input", "Error", JOptionPane.ERROR_MESSAGE);
}
try{
input2 = new BigInteger(input2Text.getText());
}catch(Exception e){
JOptionPane.showMessageDialog(this, "Bad Y Input", "Error", JOptionPane.ERROR_MESSAGE);
}
BigInteger gcd = Euclid.gcd(input1, input2);
gcdTextAnswer.setText(gcd.toString());
}
private void lcmButtonActionPerformed(java.awt.event.ActionEvent evt) {
BigInteger input1 = BigInteger.ZERO;
BigInteger input2 = BigInteger.ZERO;
try{
input1 = new BigInteger(input1Text.getText());
}catch(Exception e){
JOptionPane.showMessageDialog(this, "Bad X Input", "Error", JOptionPane.ERROR_MESSAGE);
}
try{
input2 = new BigInteger(input2Text.getText());
}catch(Exception e){
JOptionPane.showMessageDialog(this, "Bad Y Input", "Error", JOptionPane.ERROR_MESSAGE);
}
BigInteger lcm = Euclid.lcm(input1, input2);
gcdTextAnswer.setText(lcm.toString());
}
private void ResetButtonActionPerformed(java.awt.event.ActionEvent evt) {
input1Text.setText("");
input2Text.setText("");
gcdTextAnswer.setText("");
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(Euclidgui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Euclidgui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Euclidgui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Euclidgui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Euclidgui().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton ResetButton;
private javax.swing.JLabel answerLabel;
private javax.swing.JButton computeGcdButton;
private javax.swing.JTextPane gcdTextAnswer;
private javax.swing.JTextField input1Text;
private javax.swing.JTextField input2Text;
private javax.swing.JFormattedTextField jFormattedTextField1;
private javax.swing.JInternalFrame jInternalFrame1;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTextField jTextField1;
private javax.swing.JButton lcmButton;
// End of variables declaration
}