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();
Input from user made into coordinates to make a circle
Started by JaseR33, Aug 05 2010 11:24 PM
6 replies to this topic
#1
Posted 05 August 2010 - 11:24 PM
|
|
|
#2
Posted 05 August 2010 - 11:26 PM
I should have also said, I'm using BlueJ.
#3
Posted 06 August 2010 - 01:14 AM
can u check java2s.com You will find many examples.
Good luck!
Good luck!
#4
Posted 06 August 2010 - 02:49 AM
josep said:
can u check java2s.com You will find many examples.
Good luck!
Good luck!
Wow, that site is very bad to navigate. It hurts my poor eyes! Thanks for the reply though.
Anyone else?
#5
Posted 07 August 2010 - 11:55 AM
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
Posted 07 August 2010 - 06:51 PM
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
Posted 08 August 2010 - 12:02 AM
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)
--> 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)


Sign In
Create Account


Back to top









