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 12 of 12

Thread: need help finishing up this project but I don't know the code...please help

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

    Default need help finishing up this project but I don't know the code...please help

    here is the code I have so far:

    import java.awt.Graphics;
    import javax.swing.JFrame;
    import javax.swing.JOptionPane;
    import java.util.Scanner;
     
    public class Program1 extends JFrame {
        private int x1=100,y1=100,x2=150,y2=200,r1=50,r2=60;
        boolean check = false;
     
        public Program1()
    {
        String input = JOptionPane.showInputDialog("Enter the X-Coordinate of circle #1");
        x1 = Integer.parseInt(input);
        input = JOptionPane.showInputDialog("Enter the Y-Coordinate of circle #1");
        y1 = Integer.parseInt(input);
        input = JOptionPane.showInputDialog("Enter the X-Coordinate of circle #2");
        x2 = Integer.parseInt(input);
        input = JOptionPane.showInputDialog("Enter the Y-Coordinate of circle #2");
        y2 = Integer.parseInt(input);
        input = JOptionPane.showInputDialog("Enter the radius of circle #1");
        r1 = Integer.parseInt(input);
        input = JOptionPane.showInputDialog("Enter the radius of circle #2");
        r2 = Integer.parseInt(input);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
        @Override
        public void paint(Graphics g)
    {
        super.paint(g);	
        g.drawOval(x1-r1, y1-r1, r1*2, r1*2);
        g.drawOval(x2-r2, y2-r2, r2*2, r2*2);
        g.drawLine(x1, y1, x2, y2);
     
        double areaCircle1 = Math.PI*r1*r1;
        double circumferenceCircle1 = 2*Math.PI*r1;
        double areaCircle2 = Math.PI*r2*r2;
        double circumferenceCircle2 = 2*Math.PI*r2;
        g.drawString(String.format("Area = %1.3f", areaCircle1), x1, y1);	
        g.drawString(String.format("Circumference = %1.3f", circumferenceCircle1), x1, y1+10);
        g.drawString(String.format("Area = %1.3f", areaCircle2), x2, y2);
        g.drawString(String.format("Circumference = %1.3f", circumferenceCircle2), x2, y2+10);
     
        double distance = Math.sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));
     
     
        public static void main(String[] args) {
        Scanner keyboard = new Scanner(System.in);
     
            {
        Program1 frame = new Program1();
        frame.setSize(500, 500);
        frame.setVisible(true);
    }
    }


    Image.jpg

    The problem I am having is when I run the program, I need it to put the stats at the top of the window and not in the center of the circles like the image I included says. Can someone post the code for what I need to change please?

    [If what I'm asking is confusing, I'll try to re-word it. If you run the program and see that it puts the stats in the center of the circles, I need to know what to change to get it to put the stats at the top of the window, but keep the circles the same like the picture I've included shows.]
    Last edited by Nate08; February 10th, 2012 at 02:17 PM. Reason: Clearification


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: need help finishing up this project but I don't know the code...please help

    I need it to put the stats at the top of the window and not in the center of the circles
    Are you talking about the values of the x,y variables when you call drawString()?

    One way to see what the x,y values should be is to take a piece of grid paper, draw on it with a pencil where you want the Strings and circles to be and then see what the x,y values should be.

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

    Default Re: need help finishing up this project but I don't know the code...please help

    Quote Originally Posted by Norm View Post
    Are you talking about the values of the x,y variables when you call drawString()?

    One way to see what the x,y values should be is to take a piece of grid paper, draw on it with a pencil where you want the Strings and circles to be and then see what the x,y values should be.
    If you click on the image I included, it shows the words "They do not overlap" and "Circle 1's stats:" and the rest of that all at the top of the window. That's what I need. But as of right now, it is being placed in the center of the circles.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: need help finishing up this project but I don't know the code...please help

    Do you understand what I wrote in my last post? If not please ask about what you do not understand. I thought I explained exactly what was wrong and how you could fix it.

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

    Default Re: need help finishing up this project but I don't know the code...please help

    Quote Originally Posted by Norm View Post
    Do you understand what I wrote in my last post? If not please ask about what you do not understand. I thought I explained exactly what was wrong and how you could fix it.
    No sorry, I don't understand because this is my first assignment and I don't know much about Java programming yet. But yes, I'm talking about when you call drawString() and type in the messages, I need them at the top left hand corner of the window that pops up and they are going to the center of the circles right now.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: need help finishing up this project but I don't know the code...please help

    The x,y values in the drawString method give the location to draw the String.
    Where do you want to draw the Strings? Change the x,y in the drawString method to be where you want them to be drawn on the screen.

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

    Default Re: need help finishing up this project but I don't know the code...please help

    Quote Originally Posted by Norm View Post
    The x,y values in the drawString method give the location to draw the String.
    Where do you want to draw the Strings? Change the x,y in the drawString method to be where you want them to be drawn on the screen.
    Alright, I kind of figured out what you meant. I changed the values of the x and y, but I still have one more problem. I need each stat on a separate line, so that its on 3 lines. I know about the \n escape character but I'm not sure if that's for sure what I need to do or also even how to use it in my code?

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: need help finishing up this project but I don't know the code...please help

    \n has no meaning for the drawString method. You have to position each line yourself.

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

    Default Re: need help finishing up this project but I don't know the code...please help

    Alright, and another thing I noticed. As I'm re-positioning the words, the circles aren't moving and so some of the words are on the circle?

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: need help finishing up this project but I don't know the code...please help

    The shapes and Strings will draw where you draw them. The x,y values control that. If you want some shape in a different location, you need to change its x,y.
    The location of everything is up to you.

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

    Default Re: need help finishing up this project but I don't know the code...please help

    Ok, I moved the text to where I wanted it. Now where in my code do I change the numbers to move the circles because the words are overlapping the circles.

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: need help finishing up this project but I don't know the code...please help

    where in my code do I change the numbers to move the circles
    Look at the code that is drawing the circles. Change the variables it uses to control where the circles are drawn.
    Look at the API doc for the Graphics class to see how to use the various drawing methods.
    Java Platform SE 6

    Find Graphics in the lower left, click on the link and the doc will be in the main window.

Similar Threads

  1. Need help finishing code. List Building...
    By Margaret_Girl87 in forum Object Oriented Programming
    Replies: 1
    Last Post: November 30th, 2011, 06:09 PM
  2. Problem with Java code for Project
    By JavaAsh in forum What's Wrong With My Code?
    Replies: 7
    Last Post: October 19th, 2011, 03:52 AM
  3. [SOLVED] How do I run test code in different file under a project
    By lostbit in forum Java Theory & Questions
    Replies: 1
    Last Post: September 28th, 2011, 11:31 AM
  4. Beginner. Need Help finishing a simple Shoot em up game ASAP!!!!
    By cbock55 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 2nd, 2011, 08:13 AM
  5. Add code to an existing project
    By atul.mathur31 in forum Java IDEs
    Replies: 1
    Last Post: January 5th, 2011, 06:50 PM