Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



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

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


Cathy Dinh
Java GUI code- creating a circle
2 Answer(s)      3 years and 10 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();

        }

    }

Advertisement
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 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 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
Java Circle to Circle collision detection
Java Circle to Circle collision detection  Java Circle to Circle collision detection
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
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
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
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
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
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
GUI code  GUI code
Convert the code to GUI
Java Code to GUI   can any one convert My code to GUI 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
Convert the code to GUI
GUI Example  GUI Example code to learn
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 GUI Class Example  Java GUI Class Example
Convert the code to GUI
How to Convert the code to GUI   How to convert a code into GUI
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
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
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
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
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
Java GUI
Java GUI  1) Using Java GUI, create a rectangular box that changes color each time a user click a change color button. 2) Modify Question 1 to include a new button named insert image, that allow user to insert a bitmap image
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
Advertisements
 

 

 

DMCA.com