Jump to content

Input from user made into coordinates to make a circle

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
6 replies to this topic

#1
JaseR33

JaseR33

    Newbie

  • Members
  • PipPip
  • 10 posts
Hey guys. I'm very new to Java programming and I'm trying to get the user output to draw a circle of their dimentions in my JFrame. I just have no idea how to take their input and put it as my coordinates to draw my circle. I have a picture and i want the user to add a sun to the picture using and X, Y, and area value.
Any help would be massive. I need the code, what it might look like, if that makes sense. I have a look through the forums, but cant seem to find what I'm looking for.
Here's what I have prompted, and the sun which I am drawing... discard the current coordinates obviously. I think I need to use like a maths equasion to convert area to radius, then add the two coordinates to the code?

System.out.println ("Enter an X, Y and area coordinate for the sun: ");

Oval sun = new Oval(100, 100, 50, 50);
sun.setBackground(Color.yellow);

win.add(sun, 0);
win.repaint();

#2
JaseR33

JaseR33

    Newbie

  • Members
  • PipPip
  • 10 posts
I should have also said, I'm using BlueJ.

#3
josep

josep

    Learning Programmer

  • Members
  • PipPipPip
  • 56 posts
can u check java2s.com You will find many examples.

Good luck!

#4
JaseR33

JaseR33

    Newbie

  • Members
  • PipPip
  • 10 posts

josep said:

can u check java2s.com You will find many examples.

Good luck!

Wow, that site is very bad to navigate. It hurts my poor eyes! Thanks for the reply though.

Anyone else?

#5
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
Use a scanner to get the 2 values after having asked the question:
int x, y;
Scanner scanner = new Scanner(System.in);

System.out.println ("Enter X: ");
x = scanner.nextInt();
System.out.println ("Enter y: ");
y = scanner.nextInt();

Oval sun = new Oval(x, y, 50, 50);
sun.setBackground(Color.yellow);

win.add(sun, 0);
win.repaint();


#6
JaseR33

JaseR33

    Newbie

  • Members
  • PipPip
  • 10 posts

oxano said:

Use a scanner to get the 2 values after having asked the question:

int x, y;

Scanner scanner = new Scanner(System.in);


System.out.println ("Enter X: ");

x = scanner.nextInt();

System.out.println ("Enter y: ");

y = scanner.nextInt();


Oval sun = new Oval(x, y, 50, 50);

sun.setBackground(Color.yellow);


win.add(sun, 0);

win.repaint();


Thanks oxano. You are a genius! I knew it would be something simple. So the area is the next trick, because when I put in -

System.out.println ("Enter an area for the sun: ");
a = scanner.nextInt();

Oval sun = new Oval(x, y, a, a);
sun.setBackground(Color.yellow);

That works, but its technically not the area, its height and width, so I'm assuming I need to have a maths calculation to convert area to radius. I have no idea how to do that haha.

Edited by JaseR33, 07 August 2010 - 08:55 PM.


#7
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
Well the area of a circle is measured with: pi*r² . Where 2r = diameter of the circle.
--> pi*r² = A
--> pi*(d/2)² = A
--> pi /4 *d² = A
--> d² = A*4 / pi
--> d = V(A*4/pi)

V( ) = square root.

In Java you can access the square root function as well as pi using the Math class. pi = Math.PI // d=Math.sqrt(A*4/Math.PI)