Lab 5: Drawing and Comparing Circles The Circles Class Objectives Call methods w
ID: 3722240 • Letter: L
Question
Lab 5: Drawing and Comparing Circles
The Circles Class
Objectives
Call methods with parameters and return values.
Use the DrawingPanel and Graphics classes.
Use if statements to control return values and print statements.
Task
Ask the user for information to draw three circles in a DrawingPanel. Print information about the circles. Each item of information should correspond to a different static method.
Similar to previous programs, your program should start by printing
Lab 5 written by YOURNAME
Details
The DrawingPanel should be 400x300.
Circles
Ask the user for the center and radius of each circle. Use the fillOval method to draw the circles. Note that the parameters to fillOval (the upper-left corner and the width and height of the bounding box) need to be calculated from the information provided by the user (the center and the radius). Also, fill the circles using three different colors.
Methods
Each of the following items should be performed by a separate static method. Each method might have up to six parameters. Each circle is described by three values: the x and y values of the center, and the radius.
One static method should have parameters for the radiuses of two circles (two parameters), and the method should return -1 if the first circle is smaller, return 0 if the two circles have the same size, or return 1 if the first circle is larger. Based on the return value from this method, the main method should print a line of output like:
The green circle is smaller than the red circle.
Another static method should have parameters for the radiuses and locations of two circles (six paramters), and the method should return 1 if the circles intersect or return 0 if the circles do not intersect (alternatively, you could return true or false). If (x1,y1) and (x2,y2) are the centers of the circles and if r1 and r2 are their radiuses, then the circles intersect if the distance between the centers, the square root of:
(x1 - x2)2 + (y1 - y2)2
is less than or equal to r1 + r2. Based on the return values from this method, the main method should print a line of output like:
The blue circle intersects the red circle.
(Optional) Given a circle, return 1 if the circle is completely within the window, return 0 if the circle is partially within the window, or return -1 if the circle is completely outside the window. The program should print something appropriate.
Note that each method needs to be called three times. If your circles are blue, green, and red, then the first method needs to be called for three pairs: blue and green, blue and red, and green and red.
this is the code for the drawing panel:
Explanation / Answer
PROGRAMME :
import java.util.*;
import java.awt.*;
public class Circles {
final static Scanner INPUT = new Scanner(System.in);
//declaring integers
static int rad1, rad2, rad3, x1, x2, x3, y1, y2, y3, diam1, diam2, diam3;
public static void main(String args[]) {
DrawingPanel panel = new DrawingPanel(400, 300); //graphics panel at 400x300 size
Graphics g = panel.getGraphics();
System.out.println("Lab 5 ");
//circle one
System.out.println("Enter the radius for circle one:");
rad1 = INPUT.nextInt();
System.out.println("Enter the coordinates for the center of the first cirlce (x coord first)");
x1 = INPUT.nextInt();
y1 = INPUT.nextInt();
//diam1 = 2 * rad1;
g.setColor(Color.red);
g.fillOval(x1-rad1, y1-rad1, rad1*2, rad1*2);
//circle two
System.out.println("Enter the radius for cirlce two:");
rad2 = INPUT.nextInt();
System.out.println("Enter the coordinates for the center the second circle:");
x2 = INPUT.nextInt();
y2 = INPUT.nextInt();
//diam2 = 2 * rad2;
g.setColor(Color.cyan);
g.fillOval(x2-rad2, y2-rad2, rad2*2, rad2*2);
//circle three
System.out.println("Enter the radius for circle three:");
rad3 = INPUT.nextInt();
System.out.println("Enter the coordinates for the center of the third cirlce:");
x3 = INPUT.nextInt();
y3 = INPUT.nextInt();
//diam3 = 2 * rad3;
g.setColor(Color.green);
g.fillOval(x3-rad3, y3-rad3, rad3*2, rad3*2);
//compare circles based on size to see which is bigger.
int n = compareSizes(rad1, rad2);
if(n == 1) {
System.out.println("The red circle is larger than the cyan.");
}else if(n == -1) {
System.out.println("The cyan cirlce is larger than the red.");
}else {
System.out.println("The red and cyan circles are the same size.");
}
int m = compareSizes(rad2, rad3);
if(m == 1) {
System.out.println("The cyan circle is larger than the green.");
}else if(n == -1) {
System.out.println("The green circle is larger than the cyan.");
}else {
System.out.println("The cyan and green circles are the same size.");
}
int o = compareSizes(rad1, rad3);
if(o == 1) {
System.out.println("The red circle is larger than the green.");
}else if(o == -1) {
System.out.println("The green circle is larger than the red.");
}else {
System.out.println("The red and green circles are the same size.");
}
//Check to see if circles intersect.
boolean i = areIntersecting(x1, y1, rad1, x2, y2, rad2);
if( i == true) {
System.out.println("The red and cyan circles are intersecting.");
}else {
System.out.println("The red and cyan circles do not intersect.");
}
boolean j = areIntersecting(x2, y2, rad2, x3, y3, rad3);
if(j == true) {
System.out.println("The cyan and green circles are intersecting.");
}else {
System.out.println("The cyan and green circles are not intersecting.");
}
boolean k = areIntersecting(x1, y1, rad1, x3, y3, rad3);
if (k == true) {
System.out.println("The red and green circles are intersecting.");
}else {
System.out.println("The red and green circles are not intersecting.");
}
}
public static int compareSizes(int radius1, int radius2) {
if(radius1 > radius2) {
return 1;
}else if(radius1 < radius2) {
return -1;
}else {
return 0;
}
}
public static boolean areIntersecting(int radius1, int radius2, int x1, int y1, int x2, int y2) {
int dist = (int) Math.sqrt(Math.pow((x1-x2), 2) + Math.pow((y1-y2), 2));
int rad = radius1 + radius2;
if(dist <= rad) {
return true;
}else {
return false;
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.