Home Answers Viewqa Java-Beginners Java GUI code- creating a circle

 
 


Cathy Dinh
Java GUI code- creating a circle
2 Answer(s)      2 years and 3 months ago
Posted in : Java Beginners

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();
        }
    }









Related Pages:
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
Java gui program for drawing rectangle and circle
Java gui program for drawing rectangle and circle   how to write java gui program for drawing rectangle and circle? there shoud be circle.... and the program must also show message dialog "this is a red circle" when click
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 Create Circle In Java
How to Create Circle In Java       Introduction: This is a simple program of java awt. In this section, you will learn how to create Circle Diagram. The java circle is the most
Circle question - Java Beginners
the following code: import java.util.*; class Circle{ static double pi=3.14...Circle question  Hey You helped me with one this question already but the code you gave doesnt seem to work any more ideas? Write an application
area of a circle - Java Beginners
, Try the following code: class Circle { public double radius; double...area of a circle  Write a class circle which consists of functions getdata() and area(), and also write a main program to create a circle object
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
Java Code to GUI   can any one convert My code to GUI code
Convert the code to GUI
How to create GUI application in Java   How to create GUI application in Java
Java GUI Program - Java Beginners
Java GUI Program  How is the following program supposed to be coded? Write an application that prompts the user to enter the radius of a circle... calculates and displays the circumference and area of the circle to the nearest 2
Java GUI code
Java GUI code  Write a GUI program to compute the amount of a certificate of deposit on maturity. The sample data follows: Amount deposited..., computer 80000.00 (1+7.75 /100)^15   Hi Friend, Try the following code
java gui
java gui   friends... good day.. i have doubt in java gui. ? i created 1 java gui application. That has two text fields jtext1,jtext2. case: user... must go to the next text field(jtext2). How can i arrange this. which code can i
Method which returns area of circle - Java Beginners
Method which returns area of circle  Need simple Java Method example that returns area of circle  Java Example CodeWith the help of given Java code you can return the area of a circle and give the radius
Creating a Frame
, calender, combobox checkbox and many more for creating GUI in Java based... in Java. Some of the features of Swing are: * Swing GUI Components* Java 2D API... of example code: What is java swing?     
Rental Code GUI - Java Beginners
Rental Code GUI  dear sir... i would like to ask some code of java GUI form that ask the user will choose the menu to input Disk #: type: title: record company: price: director: no. of copies
Rationale for GUI tutorial decisions
Table of Contents Rationale for GUI tutorial decisions Java offers many... that some Java textbooks are written without any GUI coverage, and others cover..., the percentage of the code that is related to the GUI becomes smaller
GUI
GUI  How to GUI in Net-beans ... ??   Please visit the following link: http://www.roseindia.net/java/java-tips/background/30java_tools/netbeans.shtml
Convert this code to GUI - Java Beginners
Convert this code to GUI  I have written this.i need to convert the following code to GUI:- import java.awt.*; import java.applet.*; import...); } }  hi friend, We have convert your code into GUI
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
Flex Circle drawing example
Flex circle drawing example   ... and the button operator. The circle is not a default feature, the instructions for drawing a circle is defined inside the Sprite class of flash.display
Write a program to calculate area and perimeter of a circle
for calculating the radius and perimeter of the circle. Here is the code... Write a program to calculate area and perimeter of a circle... and perimeter of a circle. First of all name a class as "CircleArea"
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
GUI code  GUI code
urgent help needed in JDBC AND JAVA GUI - JDBC
want any one to help me convert from scanner to java GUI for this code, and connect it to mysql database thanks the code is //1st class for creating...urgent help needed in JDBC AND JAVA GUI  my application allows
GUI 2 - Java Beginners
GUI 2  How can I modify this code? Please give me a GUI...;GUI Example");pack();show();}public void actionPerformed(ActionEvent event...); }}//newly added code//KeyEvent function public void numberTextField_KeyTyped
By using Applet display a circle in a rectangle
By using Applet display a circle in a rectangle  Write a java applet to display a circle in a rectangle
GUI - Java Beginners
GUI testing  GUI testing in software testing  HiNow, use the code and get the answer.import javax.swing.*;public class DemoTryCatchDialog...;GUI Example");pack();show();}public void actionPerformed(ActionEvent event
Convert the code to GUI
GUI Example  GUI Example code to learn
Java GUI - Applet
Java GUI  HELLO, i am working on java chat server, i add JFrame and make GUI structure by draging buttons and labels, now i want to insert image on left corner JLable. how thats possible?  Hi friend, Code
Convert the code to GUI
GUI Application example  GUI Application example
Convert the code to GUI
How to Convert the code to GUI   How to convert a code into GUI
GUI Alternatives
, it isn't difficult to build a Graphical User Interface (GUI) in Java, but it is hard... to use the Java GUI libraries to build your GUI. There are alternatives... your interface be in Java? You can use existing GUI technologies like Flash
GUI - Java Beginners
. Send me the code please.  Hi Friend, Please visit the following links: http://www.roseindia.net/tutorial/java/swing/navigatedata.html http://www.roseindia.net/java/example/java/swing/add_edit_and_delete_employee_inf.shtml
GUI Tips
Java NotesGUI Tips [Beginning of list of GUI tips -- needs much more... (eg, actionPerformed) may be mixed in with the GUI construction code.... Events and communication After the GUI is constructed, the program stops execution
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
GUI - Java Beginners
GUI  whats wrong with my code failed to compile // Department interface //import java.sql.*; import javax.swing.*; import java.awt.event.*; import java.awt.*; class EmployeeInformation extends JFrame implements
Gui - Java Beginners
Gui  How can i set a background color in my code and also put the Gender panel in the middle or center of the Personal details and The Buttons the adding an image on the label Inpatients maintainance module and also increasing
GUI - Java Beginners
GUI  whats wrong with my code? import javax.swing.*; import java.awt.event.*; import java.awt.*; class prog3 extends JFrame { JLabel lbl,lbl1,lbl2,lbl3,lbl4,lbl5,lbl6,lbl7,lb18,lbl9,lbl10,lbl11,lbl12; JTextField
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
Java GUI IndexOf - Java Beginners
Java GUI IndexOf  Hello and thank you for having this great site. Here is my problem. Write a Java GUI application called Index.java that inputs... Friend, Try the following code: import java.awt.*; import javax.swing.
plz help me to create gui using Java netbeans
plz help me to create gui using Java netbeans  Hi, I am unable to fetch a particular data from DB.I am using netbeans for creating GUI. If I want..., Try the following code: import java.awt.*; import java.sql.*; import
Gui plz help
Gui plz help   Create a Java application that would allow a person... far. so basically what i did is i used the java palletes to make a application... handling code here: try{ int num = Integer.parseInt(jLabel4.getText
Writing a GUI program - Java Beginners
where and what i should put into the overall code. Also I can't quite figure out how to write the code for the GUI. Could anyone please help?  Hi Friend...Writing a GUI program  Hello everyone! I'm trying to write a program
creating java classes
creating java classes  This program uses a class named DrivingLicense to keep track of two driving licenses, including the driver?s name, and the number of speeding tickets they have received. You may not modify the DLTest code
bank account gui
; Transaction>(); I already done with the GUI i just need the code to make the button...."; } } For more information, visit the following link: http://www.roseindia.net/tutorial/java

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.