Java GUI code- creating a circle

Java GUI code- creating a circle

My assignment is to write a program that prompts the user to enter the center and a point on the circle, which is (x,y)(x2,y2). The program should then output the circle's radius, circumference, and area. I did my code but I can't seem to figure out the mathematical code, can someone please help me?

  import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;

    public class CircleGUI extends JFrame {
        //variables
        double clx = 0.0, cly = 0.0, circumrx = 0.0, circumry = 0.0; //top-left and bottom-right coordinates
        ButtonHandler buttonHandler;

        //GUI components
        JLabel lClx, lCly, lCircumrx, lCircumry, lRadius, lArea, lCircumference;
        JTextField tClx, tCly, tCircumrx, tCircumry, tRadius, tArea, tCircumference;
        JButton bReset, bCompute;
        Container c;
        JPanel pNorth, pWest, pCenter, pSouth;

        //methods

        //constructor
        public CircleGUI() {
            //create GUI
            lClx = new JLabel("Center X");
            lCly = new JLabel("Center Y");
            lCircumrx = new JLabel("Circumference X");
            lCircumry = new JLabel("Circumference Y");
            lRadius = new JLabel("Radius");
            lArea = new JLabel("Area");
            lCircumference = new JLabel("Circumference");
            tClx = new JTextField();
            tCly = new JTextField();
            tCircumrx = new JTextField();
            tCircumry = new JTextField();
            tRadius = new JTextField();
            tArea = new JTextField();
            tCircumference= new JTextField();
            bReset = new JButton("Reset");
            bCompute = new JButton("Compute");
            pNorth = new JPanel();
            pWest = new JPanel();
            pCenter = new JPanel();
            pSouth = new JPanel();
            c = getContentPane();

            //add components
            c.setLayout(new BorderLayout());
            pNorth.setLayout(new GridLayout(2,4));
            pWest.setLayout(new GridLayout(4,2));
            pCenter.setLayout(new GridLayout(4,2));
            pSouth.setLayout(new GridLayout(1,2));

            c.add(pNorth, BorderLayout.NORTH);
            c.add(pWest, BorderLayout.WEST);
            c.add(pCenter, BorderLayout.CENTER);
            c.add(pSouth, BorderLayout.SOUTH);

            pNorth.add(lClx);
            pNorth.add(tClx);
            pNorth.add(lCly);
            pNorth.add(tCly);
            pNorth.add(lCircumrx);
            pNorth.add(tCircumrx);
            pNorth.add(lCircumry);
            pNorth.add(tCircumry);
            pWest.add(lRadius);
            pWest.add(lArea);
            pWest.add(lCircumference);
            pCenter.add(tRadius);
            pCenter.add(tArea);
            pCenter.add(tCircumference);
            pSouth.add(bReset);
            pSouth.add(bCompute);

            //register with buttons
            buttonHandler = new ButtonHandler();
            bReset.addActionListener(buttonHandler);
            bCompute.addActionListener(buttonHandler);

            //render the window
            setSize(400, 200);
            setLocation(100, 100);
            setResizable(false);
            setTitle("Circle GUI");
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setVisible(true);

        }

        //button handler class
        private class ButtonHandler implements ActionListener {
            public void actionPerformed(ActionEvent ae) {
                if (ae.getSource().equals(bCompute)) {
                    try {
                        clx = Double.parseDouble(tClx.getText());
                        cly = Double.parseDouble(tCly.getText());
                        circumrx = Double.parseDouble(tCircumrx.getText());
                        circumry = Double.parseDouble(tCircumry.getText());
                        Circle obj = new Circle(clx, cly, circumrx, circumry);
                        tRadius.setText(obj.getLength()+"");
                        tArea.setText(obj.getArea()+"");
                        tCircumference.setText(obj.getCircumference()+"");
                    }
                    catch (Exception e) {
                        JOptionPane.showMessageDialog(null, "Fix your input");
                        return;
                    }
                }
                else {
                    tClx.setText("");
                    tCly.setText("");
                    tCircumrx.setText("");
                    tCircumry.setText("");
                    tRadius.setText("");
                    tArea.setText("");
                    tCircumference.setText("");

                }
            }

        }

        /**
         * @param args
         */
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            CircleGUI newApp = new CircleGUI();

        }

    }
View Answers

March 2, 2011 at 12:52 PM

Java find radius, area and circumference of a circle

import java.awt.*;
    import java.text.*;
    import javax.swing.*;
    import java.awt.event.*;

    public class CircleGUI extends JFrame {
        double clx = 0.0, cly = 0.0, circumrx = 0.0, circumry = 0.0; 
        ButtonHandler buttonHandler;
        JLabel lClx, lCly, lCircumrx, lCircumry, lRadius, lArea, lCircumference;
        JTextField tClx, tCly, tCircumrx, tCircumry, tRadius, tArea, tCircumference;
        JButton bReset, bCompute;
        Container c;
        JPanel pNorth, pWest, pCenter, pSouth;
        public CircleGUI(){
            lClx = new JLabel("Center X");
            lCly = new JLabel("Center Y");
            lCircumrx = new JLabel("Circumference X");
            lCircumry = new JLabel("Circumference Y");
            lRadius = new JLabel("Radius");
            lArea = new JLabel("Area");
            lCircumference = new JLabel("Circumference");
            tClx = new JTextField();
            tCly = new JTextField();
            tCircumrx = new JTextField();
            tCircumry = new JTextField();
            tRadius = new JTextField();
            tArea = new JTextField();
            tCircumference= new JTextField();
            bReset = new JButton("Reset");
            bCompute = new JButton("Compute");
            pNorth = new JPanel();
            pWest = new JPanel();
            pCenter = new JPanel();
            pSouth = new JPanel();
            c = getContentPane();

            c.setLayout(new BorderLayout());
            pNorth.setLayout(new GridLayout(2,4));
            pWest.setLayout(new GridLayout(4,2));
            pCenter.setLayout(new GridLayout(4,2));
            pSouth.setLayout(new GridLayout(1,2));

            c.add(pNorth, BorderLayout.NORTH);
            c.add(pWest, BorderLayout.WEST);
            c.add(pCenter, BorderLayout.CENTER);
            c.add(pSouth, BorderLayout.SOUTH);

            pNorth.add(lClx);
            pNorth.add(tClx);
            pNorth.add(lCly);
            pNorth.add(tCly);
            pNorth.add(lCircumrx);
            pNorth.add(tCircumrx);
            pNorth.add(lCircumry);
            pNorth.add(tCircumry);
            pWest.add(lRadius);
            pWest.add(lArea);
            pWest.add(lCircumference);
            pCenter.add(tRadius);
            pCenter.add(tArea);
            pCenter.add(tCircumference);
            pSouth.add(bReset);
            pSouth.add(bCompute);

            buttonHandler = new ButtonHandler();
            bReset.addActionListener(buttonHandler);
            bCompute.addActionListener(buttonHandler);
            setSize(400, 200);
            setLocation(100, 100);
            setResizable(false);
            setTitle("Circle GUI");
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setVisible(true);
        }

March 2, 2011 at 12:52 PM

continue..

private class ButtonHandler implements ActionListener {
            public void actionPerformed(ActionEvent ae) {
                if (ae.getSource().equals(bCompute)) {
                    try {
                        DecimalFormat df=new DecimalFormat("##.##");
                        clx = Double.parseDouble(tClx.getText());
                        cly = Double.parseDouble(tCly.getText());
                        circumrx = Double.parseDouble(tCircumrx.getText());
                        circumry = Double.parseDouble(tCircumry.getText());
                        double p1=circumrx-clx;
                        double p2=circumry-cly;
                        double pp1=p1*p1;
                        double pp2=p2*p2;
                        double s=pp1+pp2;
                        double radius=Math.sqrt(s);
                        double area=3.14*radius*radius;
                        double circumference=2*3.14*radius;
                        tRadius.setText(df.format(radius));
                        tArea.setText(df.format(area));
                        tCircumference.setText(df.format(circumference));
                    }
                    catch (Exception e) {
                        JOptionPane.showMessageDialog(null, "Fix your input");
                        return;
                    }
                }
                else {
                    tClx.setText("");
                    tCly.setText("");
                    tCircumrx.setText("");
                    tCircumry.setText("");
                    tRadius.setText("");
                    tArea.setText("");
                    tCircumference.setText("");
                }
            }
        }
        public static void main(String[] args) {
            CircleGUI newApp = new CircleGUI();
        }
    }

Ads









Related Tutorials/Questions & Answers:
Java GUI code- creating a circle
Java GUI code- creating a circle  My assignment is to write a program that prompts the user to enter the center and a point on the circle, which..., 100); setResizable(false); setTitle("Circle GUI
Creating circle inside a circle
Creating circle inside a circle  hi can any one give code for following output by awt/swing creating circle inside a circle ,outside cirlce and intersect a circle
Advertisements
Java Circle to Circle collision detection
Java Circle to Circle collision detection  Java Circle to Circle collision detection
How to convert this Java code into GUI?
How to convert this Java code into GUI?   import java.util.Scanner; public class StudentMarks { double totalMarks; String grade; public void setTotalMarks(double totalMarks) { this.totalMarks = totalMarks
Convert the code to GUI
GUI code  GUI code
Catching Exceptions in GUI Code - Java Tutorials
.style1 { text-align: center; } Catching uncaught exception in GUI In this section, we will discuss how to catch uncaught exceptions in GUI. Lets see the given below code to identify the uncaught exception : import
Convert the code to GUI
Java Code to GUI   can any one convert My code to GUI code
Convert the code to GUI
GUI Example  GUI Example code to learn
Convert the code to GUI
Java GUI Class Example  Java GUI Class Example
Convert the code to GUI
GUI Java JSP application  GUI Java JSP application
Convert the code to GUI
Java and GUI application Example  Java and GUI application Example
Convert the code to GUI
How to Convert the code to GUI   How to convert a code into GUI
Convert the code to GUI
How to create GUI application in Java   How to create GUI application in Java
Convert the code to GUI
Convert the code   How to convert a code to GUI look alike
Convert the code to GUI
Is it possible to convert a code into GUI  Is it possible to convert a code into GUI
Convert the code to GUI
GUI Application example  GUI Application example
Convert the code to GUI
GUI Application Development   GUI Application Development
Convert the code to GUI
Write a GUI Application  best way to write a GUI based application
How do I handle the reaction of a circle and a semi-circle colliding in java?
How do I handle the reaction of a circle and a semi-circle colliding in java?  How do I handle the reaction of a circle and a semi-circle colliding in java
Convert the code to GUI ??
Convert the code to GUI ??  hi >> can anyone help me to conver this code to GUI ?? /** * @(#)RegistorClass.java * *. * @author...("*** Invalid operation code ***"); halt= true; // break
Convert the code to GUI
Convert the code to GUI   can any one convert My code to GUI code...: System.out.println("*** Invalid operation code... ??? System.out.println(); } } `print("code sample");` thanks
Convert the code to GUI
GUI example for beginners  GUI example for beginners  sory ,, I will posted my code again import java.util.Scanner; public class...; default: System.out.println("*** Invalid operation code
circle
circle  Program to test whether a given point lies inside a circle or not?   Hi Friend, Try the following code: import java.util.... of the circle : "); double r = input.nextDouble
HOW TO CONVERT THIS CODE INTO GUI
HOW TO CONVERT THIS CODE INTO GUI   System.out.println("\n\t UGANDA CHRISTIAN UNIVERSITY\n"); System.out.println("\n\tFACULTY OF SCIENCE AND TECHNOLOGY\n"); System.out.println("\n BACHELOR OF SCIENCE IN COMPUTER
convert this code to GUI
convert this code to GUI  hello.. this is my code.. import java.util.Scanner; public class StudentGrade { double totalMarks; String grade; public void setTotalMarks(double totalMarks) { this.totalMarks
convert this code to GUI
convert this code to GUI  hello.. this is my code.. import java.util.Scanner; public class StudentGrade { double totalMarks; String grade; public void setTotalMarks(double totalMarks) { this.totalMarks
convert this code to GUI
convert this code to GUI  hello.. this is my code.. import java.util.Scanner; public class StudentGrade { double totalMarks; String grade; public void setTotalMarks(double totalMarks) { this.totalMarks
convert this code to GUI
convert this code to GUI  import java.util.Scanner; public class StudentGrade { double totalMarks; String grade; public void setTotalMarks(double totalMarks) { this.totalMarks = totalMarks; //"this" keyword
convert this code to GUI
convert this code to GUI  import java.util.Scanner; public class StudentGrade { double totalMarks; String grade; public void setTotalMarks(double totalMarks) { this.totalMarks = totalMarks; //"this" keyword
convert this code to GUI
convert this code to GUI  import java.util.Scanner; public class StudentGrade { double totalMarks; String grade; public void setTotalMarks(double totalMarks) { this.totalMarks = totalMarks; //"this" keyword
convert this code to GUI
convert this code to GUI  import java.util.Scanner; public class StudentGrade { double totalMarks; String grade; public void setTotalMarks(double totalMarks) { this.totalMarks = totalMarks; //"this" keyword

Ads