Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: making a program that draws two circles and calculates the area and circumference

  1. #1
    Junior Member
    Join Date
    Feb 2012
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default making a program that draws two circles and calculates the area and circumference

    I have a program that I need to finish before tomorrow and here are the instructions:

    To draw two circles plus a line connecting their centers and then print a few facts about the circles. The (x, y) coordinates and radii of the circles must be accepted from the user.
    The applet’s algorithm is as follows:
    1. Ask the user for the x-coordinate for the location of a circle.
    2. Ask the user for the y-coordinate for the location of a circle.
    3. Ask the user for the x-coordinate for the location of another circle.
    4. Ask the user for the y-coordinate for the location of another circle.
    5. Ask the user for the radius of a circle.
    6. Ask the user for the radius of another circle.
    7. Draw both circles using the x, y coordinates and radii given by the user.
    8. Draw a line connecting both centers.
    9. Write the following facts about the circles:
    a. Each circle’s area
    b. Each circle’s circumference
    c. Whether or not the circles touch each other.


    Requirements:
    •This program must extend a JFrame.
    •Set the size of the program to 500x500 using the setSize(500, 500); function.
    •Remember that when you draw the circles, you must use the drawOval() function, but it will NOT use the center of the circle as the x,y coordinate, it will use the upper-left corner of the shape. So you must adjust the ends of the line segment so that it connects the circles by their centers (add the radius to the x and y coordinates of the circles to do this).
    •Use the drawString() function to display the messages about the circles. The numbers for the area and circumference should be formatted using %1.3f. This can be done by using the String class’s format() function for example:
    drawstring(String.format(“area = %1.3f”, circle1Area));
    •Your program class should contain private double variables to store the circle data.
    •Use Math.PI to get the value of Pi (3.14159…) for calculating the area and circumference.
    •To find out if the circles overlap, you can use the Distance Formula (look it up) to find the distance between their centers, then compare that distance to the sum of the two circles’ radii. If the distance is less than or equal to the sum of the radii, they overlap.


    The problems I'm having is basically Steps 7-9 and A, B and C. I don't know how I'm supposed to do the string formatting, drawing the circles, drawing a line connecting their centers and how to calculate the area and circumference. So when I run this, it does the first 6 steps and then the window shows up blank because I don't have the rest of the information. I have included a picture of what its supposed to look like, sorry that the circles got cut off, but that's what it kind of is supposed show. You can kind of see that the line connects the centers.



    Image.jpgHere is the code I have so far:

    import javax.swing.JFrame;
    import java.awt.Graphics;
    import javax.swing.JOptionPane;
    import java.lang.Math;
    import java.util.Scanner;
    /**
     *
     * @author Nate
     */
    public class Program1 extends JFrame {
        private static final int FRAME_SIZE = 500;
        private static final double PI =3.14159;
        private static final double X1 = 100;
        private static final double X2 = 100;
        private static final double Y1 = 150;
        private static final double Y2 = 200;
        private static final double R1 = 50;
        private static final double R2 = 60;
     
     
        public void paintComponent(Graphics canvas)
    {
    super.paintComponents(canvas);
        }
        @Override
        public void paint(Graphics g) {
    super.paint(g);
    Graphics canvas = (Graphics)g;
        }
     
        public static void main(String[] args) {
     
            String circle1XcoordinateString =
             JOptionPane.showInputDialog("Enter the X-Coordinate of circle #1");
            int circle1X = Integer.parseInt(circle1XcoordinateString);
     
            String circle2XcoordinateString =
             JOptionPane.showInputDialog("Enter the X-Coordinate of circle #2");
            int circle2X = Integer.parseInt(circle2XcoordinateString);
     
            String circle1YcoordinateString =
             JOptionPane.showInputDialog("Enter the Y-Coordinate of circle #1");
            int circle1Y = Integer.parseInt(circle1YcoordinateString);
     
            String circle2YcoordinateString =
             JOptionPane.showInputDialog("Enter the Y-Coordinate of circle #2");
            int circle2Y = Integer.parseInt(circle2YcoordinateString);
     
            String radius1String =
             JOptionPane.showInputDialog("Enter the radius of circle #1");
            int radius1 = Integer.parseInt(radius1String);
     
            String radius2String =
             JOptionPane.showInputDialog("Enter the radius of circle #2"); 
            int radius2 = Integer.parseInt(radius2String);
     
            JFrame guiWindow = new JFrame();
            guiWindow.setVisible(true);
            guiWindow.setSize(500, 500);
            guiWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
     
      }
        }
    Attached Images Attached Images
    Last edited by Nate08; February 10th, 2012 at 02:01 AM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: making a program that draws two circles and calculates the area and circumference

    The JFrame class does not have a paintComponent method that you can override, so that part of your code will not be called by the JVM to do anything. You override the paintComponent method for Swing classes.

    You need to look at the API doc for the Graphics class to see what methods you can use to draw a circle. I think the one you want draws an oval with the width and height the same. Add a call to this method to your paint method and see what is drawn on the screen. Be sure to set the Graphics color to a contrasting color so your drawing will be visible.

Similar Threads

  1. Making a program that calculates how many days you lived.
    By shifat96 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 10th, 2012, 12:34 AM
  2. Help with JFrame program that calculates average
    By ePerKar3 in forum AWT / Java Swing
    Replies: 3
    Last Post: November 4th, 2011, 08:48 AM
  3. So I'm making this program...
    By SkyAphid in forum The Cafe
    Replies: 2
    Last Post: August 29th, 2011, 05:55 PM
  4. Basic program which gets cost, adds tax, gets payment then calculates change.
    By bibboorton in forum What's Wrong With My Code?
    Replies: 6
    Last Post: August 25th, 2010, 10:31 AM
  5. Need help making program
    By ixjaybeexi in forum Collections and Generics
    Replies: 5
    Last Post: December 6th, 2009, 11:36 PM