/* filename: circle.java description: given coordinates for two points on a cirlce, outputs attributes of circle author: Kalin Ringkvist, k.rainquist@comcast.net version: 1.00 2004-02-18 */ //import Java classes import javax.swing.*; import java.awt.*; import java.awt.event.*;//to detect the click on the button import java.text.*; //to format our output public class circle extends JFrame { //create instance variables private JLabel lblX1 = new JLabel("Location of first point (center of circle) on X axis (X1):"); private JLabel lblY1 = new JLabel("Location of first point (center of circle) on Y axis (Y1):"); private JLabel lblX2 = new JLabel("Location of second point (edge of circle) on X axis (X2):"); private JLabel lblY2 = new JLabel("Location of second point (edge of cirlce) on Y axis (Y2):"); private JTextField txbX1 = new JTextField(""); private JTextField txbY1 = new JTextField(""); private JTextField txbX2 = new JTextField(""); private JTextField txbY2 = new JTextField(""); private JButton butCalc = new JButton("Calculate"); private CalculateButtonHandler calcHandler; //create DecimalFormat to format final output (used in DecimalFormat num = new DecimalFormat("0.000"); public circle() { //get reference to JFrame. Container frame = getContentPane(); //create and apply layout to the frame frame.setLayout(new GridLayout(5,2)); //set result box as uneditable //txbResult.setEditable(false); //set the default operation to exit when the user closes the application setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //set the title of the frame setTitle("Calculate the attributes of a circle."); //show the frame show(); //put the objects on the form & align text frame.add(lblX1); //lblX1.setHorizontalAlignment(JTextField.CENTER); frame.add(txbX1); frame.add(lblY1); //lblY1.setHorizontalAlignment(JTextField.CENTER); frame.add(txbY1); frame.add(lblX2); //lblX2.setHorizontalAlignment(JTextField.CENTER); frame.add(txbX2); frame.add(lblY2); //lblY2.setHorizontalAlignment(JTextField.CENTER); frame.add(txbY2); frame.add(butCalc); //add action listener to butCalc to detect the click calcHandler = new CalculateButtonHandler(); butCalc.addActionListener(calcHandler); } public static void main(String[] args) { circle c = new circle(); //instantiate class c.pack(); //pack the frame around the objects } //private class handles the button click event private class CalculateButtonHandler implements ActionListener { public void actionPerformed(ActionEvent event) { //get the user input double dblX1 = Double.parseDouble(txbX1.getText()); double dblY1 = Double.parseDouble(txbY1.getText()); double dblX2 = Double.parseDouble(txbX2.getText()); double dblY2 = Double.parseDouble(txbY2.getText()); //call methods to calculate attributes double dblRadius = (radius(dblX1, dblY1, dblX2, dblY2)); double dblCircum = (circumference(dblRadius)); double dblArea = (area(dblRadius)); //format and combine attributes with labels and line feeds into a string variable String strResult = "Radius: " + num.format(dblRadius) + " units. \n Diameter: " + num.format(dblRadius * 2) + " units. \n Circumference: " + num.format(dblCircum) + " units. \n Area: " + num.format(dblArea) + " square units."; //Display that string variable in a message dialog JOptionPane.showMessageDialog(null, strResult, "Results", JOptionPane.INFORMATION_MESSAGE); } } //here begins the four required methods //calculates distance between the two points public static double distance(double X1, double Y1, double X2, double Y2) { return Math.sqrt((X2 - X1) * (X2 - X1) + (Y2 - Y1) * (Y2 - Y1)); } //This seems to be a pointless method, doing nothing but calling another method public static double radius(double X1, double Y1, double X2, double Y2) { return distance(X1, Y1, X2, Y2); } //calculates circumference of the circle that's represented by the two points public static double circumference(double radius) { //doubled pi and multiplied by the radius return 6.2832 * radius; } //calculates the area of the circle public static double area(double radius) { //radius squared multiplied by pi return radius * radius * 3.1416; } }