I am a beginner programmer. How would you refactor this code to abide by good coding practices? As it stands the program has 4 classes and its aim is to compute the area and perimeter of a specific shape by inputting its length from a JSlider. The MyFrame
, MyShape
, MyTriangle
, MyCircle
and MySquare
. The initComponents()
method is autogenerated by NetBeans autobuilder so I didn't include it below. Also the program is fully functionable as of now, just want to know how to better refactor the code, splitting it in different classes etc..
package mainpackage;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DecimalFormat;
import javax.swing.JFrame;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class MyFrame extends JFrame
{
MyShape myShape;
int value;
public MyFrame()
{
// Create frame, panels and items within
initComponents();
shapeSlider.setEnabled(false);
// Create instances of shapes when chosen in Menu
initShapes();
// Adds Changelistener to slider and updates dimensionOutputField
linkEventHandler();
exitButton2.addActionListener(new CloseListener());
}
private void initShapes()
{
ShapeHandler shapeHandler = new ShapeHandler();
squareMenuItem.addActionListener(shapeHandler);
circleMenuItem.addActionListener(shapeHandler);
triangleMenuItem.addActionListener(shapeHandler);
}
public class ShapeHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == squareMenuItem)
{
myShape = new MySquare();
} else if (e.getSource() == circleMenuItem)
{
myShape = new MyCircle();
} else if (e.getSource() == triangleMenuItem)
{
myShape = new MyTriangle();
}
shapeSlider.setEnabled(true);
}
}
public class MyChangeAction implements ChangeListener
{
public void stateChanged(ChangeEvent e)
{
int currentValue = shapeSlider.getValue() ;
dimensionOutputField.setText(String.valueOf(currentValue));
DecimalFormat numberFormat = new DecimalFormat("#.00");
double bLength = myShape.calculatePerimeter(currentValue);
String bLengthString = numberFormat.format(bLength);
bLengthOutputField.setText(bLengthString);
double bArea = myShape.calculateArea(currentValue);
String areaString = numberFormat.format(bArea);
areaOutputField1.setText(areaString);
}
}
private void linkEventHandler()
{
MyChangeAction sliderAction = new MyChangeAction() ;
shapeSlider.addChangeListener(sliderAction);
}
private class CloseListener implements ActionListener
{
@Override
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
}
private void exitButtonActionPerformed(java.awt.event.ActionEvent evt) {
}
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(MyFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex)
{
java.util.logging.Logger.getLogger(MyFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex)
{
java.util.logging.Logger.getLogger(MyFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex)
{
java.util.logging.Logger.getLogger(MyFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable()
{
public void run() {
new MyFrame().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JPanel MyControlPanel;
private javax.swing.JLabel areaLabel1;
private javax.swing.JTextField areaOutputField1;
private javax.swing.JLabel bLengthLabel;
private javax.swing.JTextField bLengthOutputField;
private javax.swing.JMenuItem circleMenuItem;
private javax.swing.JLabel dimensionLabel;
private javax.swing.JTextField dimensionOutputField;
private javax.swing.JButton exitButton2;
private javax.swing.JPanel mainPanel2;
private javax.swing.JLabel mainTitle;
private javax.swing.JMenu shapeMenu;
private javax.swing.JMenuBar shapeMenuBar;
private javax.swing.JSlider shapeSlider;
private javax.swing.JMenuItem squareMenuItem;
private javax.swing.JMenuItem triangleMenuItem;
private org.jdesktop.beansbinding.BindingGroup bindingGroup;
// End of variables declaration
}