Getting the radius for a circle from user input

For my last and not so hard project i have to ask the user to input the radii of two circles, draw them, and then say if they intersect (if the user inputs a negative radius say they don't). The problem I'm having is that if i try to take r1 which is my user inputted number and apply it to the width/height of the circle it says i cannot use a double for g.drawOval(int,int,int,int);

my code

import java.awt.*;

import java.applet.*;

import java.io.*;

publicclass PROJECTextends Applet{

publicvoid init(){

BufferedReader in =new BufferedReader(new InputStreamReader(System.in));

String input;

try{

System.out.println("Type the radius of the first cricle.");

input = in.readLine();

r1 = getNumber(input);

System.out.println("Type the radius of the second cricle.");

input = in.readLine();

r2 = getNumber(input);

}catch(IOException e){}

}

publicdouble getNumber(String input){

double d;

try{

d = Double.parseDouble(input);

}catch(NumberFormatException nfe){

d = Double.NaN;

}

return d;

}

publicvoid paint(Graphics g){

Color bg =new Color(240, 0, 100);

Color ivory =new Color(240, 240, 240);

setBackground(bg);

g.drawString("Welcome to Java!!", 50, 60 );

g.setColor(ivory);

g.fillOval(20,50,r1,r1);

}

double r1;

double r2;

}

the error--

--Configuration: PROJECT - JDK version 1.5.0_07 <Default> - <Default>--

C:\Program Files\Xinox Software\JCreatorV3LE\MyProjects\LastProj\PROJECT\src\PROJECT.java:52: fillOval(int,int,int,int) in java.awt.Graphics cannot be applied to (int,int,double,double)

g.fillOval(20,50,r1,r1);

^

1 error

Process completed.

[3133 byte] By [FrancoNa] at [2007-10-3 3:18:23]
# 1
Cast to int:g.fillOval(20,50,(int)r1,(int)r1);or set up r1 as an int to begin with.
MLRona at 2007-7-14 21:10:07 > top of Java-index,Java Essentials,Java Programming...
# 2
Thank you very much.
FrancoNa at 2007-7-14 21:10:07 > top of Java-index,Java Essentials,Java Programming...
# 3

To find out if the circles are intersecting or not, i have to find this distance and then compare that with to the radii. Now I'm having trouble with this formula, I know it's easy to write but I just don't knwo what to write. I'm thinking ill have to use (r1+r2) somewhere in there. This is what i have started with.

if(distance<=(r1+r2)) {

System.out.println("They intersect");

}

That boviously doesn't give me the right answer, any ideas?

FrancoNa at 2007-7-14 21:10:07 > top of Java-index,Java Essentials,Java Programming...
# 4
If distance is the distance between the two centre-points, then this should be correct. Why do you say it is obviously wrong?Brian
brian@cubik.caa at 2007-7-14 21:10:07 > top of Java-index,Java Essentials,Java Programming...
# 5
The distiance is between the two circles not the centers.
FrancoNa at 2007-7-14 21:10:07 > top of Java-index,Java Essentials,Java Programming...
# 6
You know thecentre because that is what gdraw uses. The distance between the centres must be less than r1 +r2 and distnce is the square root of (r1*r1 + r2*r2).Does that asnwer your question?
nerak99a at 2007-7-14 21:10:07 > top of Java-index,Java Essentials,Java Programming...
# 7
To know if two circles intersect, you need to know both their radii and their centers. Then, you need the distance formula between two points. Yes, if (distanceBetweenCenters <= (r1+r2)), then they intersect. The key is the formula for the distance between two points.
MLRona at 2007-7-14 21:10:07 > top of Java-index,Java Essentials,Java Programming...
# 8
That doesn't work, if i do that then two cricles with the radii of 100 do not intersect but the program says it doesnt because the distance between is 141.Circle one's center is 100,200 and circle two's center is 200,100 by the way.
FrancoNa at 2007-7-14 21:10:07 > top of Java-index,Java Essentials,Java Programming...
# 9
buddy its time to get some paper and a pencil and draw it out. Its not to difficult.you know the x,y coordinates of the center of the circles, so its like finding the distance between two points, you should be able to figure that out.
sophisticatedda at 2007-7-14 21:10:07 > top of Java-index,Java Essentials,Java Programming...
# 10

First of all, it's not about the distance formula,from one point to another, if it was I could finish this in two seconds. Second of all, as I put in a higher number, the circles move more and more down-right, why is that? this is my code

import java.awt.*;

import java.applet.*;

import java.io.*;

public class PROJECT extends Applet {

public void init() {

BufferedReader in = new BufferedReader(new InputStreamReader(System.in));

String input;

try{

System.out.println("Type the radius of the first cricle.");

input = in.readLine();

r1 = getNumber(input);

System.out.println("Type the radius of the second cricle.");

input = in.readLine();

r2 = getNumber(input);

}catch(IOException e){}

}

public double getNumber(String input){

double d;

try{

d = Double.parseDouble(input);

} catch(NumberFormatException nfe){

d = Double.NaN;

}

return d;

}

public void paint(Graphics g) {

Color myColor = new Color(240, 50, 100);

Color ivory = new Color(240, 240, 240);

int x1 = 100;

int x2 = 200;

int y1 = 200;

int y2 = 100;

setBackground(myColor);

g.setColor(ivory);

g.fillOval(100,200,(int)r1,(int)r1);

g.fillOval(200,100,(int)r2,(int)r2);

///DISTANCE

int xs = x2-x1;

int xsq = xs*xs;

int ys = y2-y1;

int ysq = ys*ys;

double distance = Math.sqrt(xsq+ysq);

System.out.println("The distance between the centers : " + distance);

System.out.println(r1+r2);

if(distance<=r1+r2) {

System.out.println("Circles do not intersect");

}

}

public boolean intersects(PROJECT other){

return true;

}

double r1;

double r2;

}

If you run it and put in lets say 50 and 50 it seems ok, but then put in 200 and 200 and see if you notice what im talking about.

FrancoNa at 2007-7-14 21:10:07 > top of Java-index,Java Essentials,Java Programming...
# 11

God d@mn it noob! Do things the right way.

Click around and change around the radius values.

Enjoy but dont forget: your report card goes on MY fridge.

import java.awt.*;

import java.awt.event.*;

import java.applet.*;

import java.io.*;

public class Project extends Applet implements ActionListener, MouseListener{

public void init() {

/*

BufferedReader in = new BufferedReader(new InputStreamReader(System.in));

String input;

try{

System.out.print("Enter the Radius of the Circle #1: ");

input = in.readLine();

radius1 = getRadius(input);

System.out.print("Enter the Radius of the Circle #2: ");

input = in.readLine();

radius2 = getRadius(input);

}catch(IOException e){}

*/

//

this.addMouseListener(this);

ball1.setLocation(100, 200);

this.setLayout(null);

rad1.addActionListener(this);

rad2.addActionListener(this);

rad1.setText("" + radius1);

rad2.setText("" + radius2);

rad1.setBounds(80, 10, 50, 22);

rad2.setBounds(210, 10, 50, 22);

this.add(rad1);

this.add(rad2);

}

public void actionPerformed(ActionEvent e) {

if(e.getSource() == rad1){

radius1 = getRadius(rad1.getText());

rad1.setText("" + radius1);

this.repaint();

}

if(e.getSource() == rad2){

radius2 = getRadius(rad2.getText());

rad2.setText("" + radius2);

this.repaint();

}

}

public int getRadius(String input){

int radius;

try{

radius = Integer.parseInt(input);

} catch(NumberFormatException nfe){

radius = defaultRadius;

}

return radius;

}

public void paint(Graphics g) {

int midX = (int)(this.getWidth() / 2.0);

int midY = (int)(this.getHeight() / 2.0);

ball2.setLocation(midX, midY);

setBackground(backgroundColor);

g.setColor(ball2Color);

g.fillOval(ball2.x - radius2, ball2.y - radius2, radius2 * 2, radius2 * 2);

g.setColor(ball1Color);

g.fillOval(ball1.x - radius1, ball1.y - radius1, radius1 * 2, radius1 * 2);

double distance = getDistance(ball1.x - ball2.x, ball1.y - ball2.y);

//System.out.println("Distance Between Centers: " + distance);

//System.out.println("R1 + R2: " + (radius1 + radius2));

g.setColor(textColor);

g.drawString("Radius 1:", 10, 28);

g.drawString("Radius 2:", 140, 28);

g.drawString("Distance: " + distance, 10, 58);

if(intersects(distance)) {

//System.out.println("Circles Do Not Intersect");

g.drawString("Circles Intersect", 10, 88);

}

}

public int getDistance(int dx, int dy){

return (int)Math.rint(Math.sqrt((double)((dx * dx) + (dy * dy))));

}

public boolean intersects(double distance){

return (distance <= (radius1 + radius2));

}

public void mousePressed(MouseEvent e){

ball1.setLocation(e.getPoint());

this.repaint();

}

public void mouseClicked(MouseEvent e){}

public void mouseEntered(MouseEvent e){}

public void mouseExited(MouseEvent e){}

public void mouseReleased(MouseEvent e){}

int defaultRadius = 20;

int radius1 = defaultRadius;

int radius2 = defaultRadius;

Color backgroundColor = new Color(240, 50, 100);

Color ball1Color = Color.YELLOW;

Color ball2Color = Color.GREEN;

Color textColor = Color.BLACK;

TextField rad1 = new TextField();

TextField rad2 = new TextField();

Point ball1 = new Point();

Point ball2 = new Point();

}

TuringPesta at 2007-7-14 21:10:07 > top of Java-index,Java Essentials,Java Programming...
# 12

> First of all, it's not about the distance formula,from one point to another,

> if it was I could finish this in two seconds. Second of all, as I put in a higher number, the circles move more and more

> down-right, why is that? this is my code

Read the API for fillOval, and look at what the parameters really represent. You are interpreting them incorrectly.

MLRona at 2007-7-14 21:10:07 > top of Java-index,Java Essentials,Java Programming...
# 13
franco, how did you do on this project?when's this class over?
TuringPesta at 2007-7-14 21:10:07 > top of Java-index,Java Essentials,Java Programming...
# 14
sorry i didnt get back to you guys i was in new york the whole day. Thank you TuringPest for the code. And also, the class hasn't even started these are my summer projects. Looks like we are goign to be best friends! hahaha
FrancoNa at 2007-7-14 21:10:07 > top of Java-index,Java Essentials,Java Programming...
# 15
Wow, i just tried this program, it's amazing! Thank you sooooo much.
FrancoNa at 2007-7-21 10:08:35 > top of Java-index,Java Essentials,Java Programming...
# 16
Do you know why TuringPest's code works? Or, did you just copy it, see that it works, and let it go at that? You should make sure to understand it, even if it is just your own project and not for a class.
MLRona at 2007-7-21 10:08:35 > top of Java-index,Java Essentials,Java Programming...
# 17

I second MLRon's concern. Make sure you know whats going on.

Try to add functionality off the base code and try out new things -

thats a great was to get familiar with new code.

anyway, good luck with your projects.

hopefully the code gave you same ideas and showed you what can

be done and how easy it can be done.

TuringPesta at 2007-7-21 10:08:35 > top of Java-index,Java Essentials,Java Programming...