Program that draws a circle with radius 100 and center 200

Assignment Help C/C++ Programming
Reference no: EM13939935

Write a program that draws a circle with radius 100 and center (200, 200). Ask the user to specify the x- and y-coordinates of a point. Draw the point as a small circle. If the point lies inside the circle, color the small circle green. Otherwise, color it red. In your exercise, declare a class Circle and a method boolean isInside(Point2D.Double p).

Here is a sample program output:

Use the following class as your main class:
import java.awt.geom.Point2D;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

/**
This program views a circle and a pocint.
*/
public class CircleViewerSnap
{
public static void main(String[] args)
{
JFrame frame = new JFrame();

final int FRAME_WIDTH = 400;
final int FRAME_HEIGHT = 400;

frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
frame.setTitle("CircleViewer");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

Point2D.Double point = new Point2D.Double(150, 150);

CircleComponent component = new CircleComponent(point);
frame.add(component);

frame.setVisible(true);
}
}

Complete the following classes in your solution:
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.geom.Point2D;
import java.awt.geom.Ellipse2D;

/**
This class implements a circle and a boolean function to
test if a user-given point is inside this circle.
*/
public class Circle
{
private double xCenter;
private double yCenter;
private double radius;
private Color color;

/**
Constructs a circle.
@param x the x-coordinate of the center
@param y the y-coordinate of the center
@param r the radius
@param aColor the color
*/
public Circle(double x, double y, double r, Color aColor)
{
xCenter = x;
yCenter = y;
radius = r;
color = aColor;
}

/**
Draws a circle and a point.
@param g2 the graphics content
*/
public void draw(Graphics2D g2)
{
g2.setColor(color);
Ellipse2D.Double circle
= new Ellipse2D.Double(xCenter - radius, yCenter - radius,
2 * radius, 2 * radius);
g2.draw(circle);
}

/**
Determine if point is inside or outside the circle
@param p the point to test if it is inside the circle
@return true if the point is inside the circle
*/
public boolean isInside(Point2D.Double p)
{
. . .
}
}

--------------------------------------------------------------------------------

import javax.swing.JComponent;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Point2D;
import java.awt.geom.Ellipse2D;
import javax.swing.JOptionPane;

/**
Draws a circle and a point. The point is colored green if it falls
inside the circle, red otherwise.
*/
public class CircleComponent extends JComponent
{
private Circle circle;
private Circle smallCircle;

public CircleComponent(Point2D.Double point)
{
circle = new Circle(200, 200, 100, Color.BLACK);
final double SMALL_RADIUS = 3;
Color color;
if(. . .)
color = Color.GREEN;
else
color = Color.RED;
smallCircle = new Circle(point.getX(), point.getY(), SMALL_RADIUS, color);
}

public void paintComponent(Graphics g)
{
Graphics2D g2 = (Graphics2D) g;
circle.draw(g2);
smallCircle.draw(g2);
}
}

Use the following class in your solution:
import java.awt.geom.Point2D;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

/**
This program views a circle and a point.
*/
public class CircleViewer
{
public static void main(String[] args)
{
JFrame frame = new JFrame();

final int FRAME_WIDTH = 400;
final int FRAME_HEIGHT = 400;

frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
frame.setTitle("CircleViewer");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

String input = JOptionPane.showInputDialog("x:");
double x = Double.parseDouble(input);

input = JOptionPane.showInputDialog("y:");
double y = Double.parseDouble(input);

Point2D.Double point = new Point2D.Double(x, y);

CircleComponent component = new CircleComponent(point);
frame.add(component);

frame.setVisible(true);
}
}

I HAVE COME UP WITH THE FOLLOWING CODE BUT CAN'T SEEM TO GET THE POINTER TO WORK. PLEASE HELP.

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.geom.Point2D;
import java.awt.geom.Ellipse2D;

/**
This class implements a circle and a boolean function to
test if a user-given point is inside this circle.
*/
public class Circle
{
private double xCenter;
private double yCenter;
private double radius;
private Color color;

/**
Constructs a circle.
@param x the x-coordinate of the center
@param y the y-coordinate of the center
@param r the radius
@param aColor the color
*/
public Circle(double x, double y, double r, Color aColor)
{
xCenter = x;
yCenter = y;
radius = r;
color = aColor;
}

/**
Draws a circle and a point.
@param g2 the graphics content
*/
public void draw(Graphics2D g2)
{
g2.setColor(color);
Ellipse2D.Double circle
= new Ellipse2D.Double(xCenter - radius, yCenter - radius,
2 * radius, 2 * radius);
g2.draw(circle);
}

/**
Determine if point is inside or outside the circle
@param p the point to test if it is inside the circle
@return true if the point is inside the circle
*/

public boolean isInside(Point2D.Double p)
{
double distance;

distance = Math.sqrt(Math.pow(xCenter - p.x,2)+(Math.pow(yCenter - p.y,2)));

if(distance < radius)
{
return true;
}
else
{
return false;
}
}
}

------------------------------------------
import javax.swing.JComponent;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Point2D;
import java.awt.geom.Ellipse2D;
import javax.swing.JOptionPane;

/**
Draws a circle and a point. The point is colored green if it falls
inside the circle, red otherwise.
*/
public class CircleComponent extends JComponent
{
private Circle circle;
private Circle smallCircle;

public CircleComponent(Point2D.Double point)
{
circle = new Circle(200, 200, 100, Color.BLACK);
final double SMALL_RADIUS = 3;
Color color;
if (circle.isInside()== true)
color = Color.GREEN;
else
color = Color.RED;
smallCircle = new Circle(point.getX(), point.getY(), SMALL_RADIUS, color);
}

public void paintComponent(Graphics g)
{
Graphics2D g2 = (Graphics2D) g;
circle.draw(g2);
smallCircle.draw(g2);
}
}

---------------------------------

import java.awt.geom.Point2D;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

/**
This program views a circle and a point.
*/
public class CircleViewer
{
public static void main(String[] args)
{
JFrame frame = new JFrame();

final int FRAME_WIDTH = 400;
final int FRAME_HEIGHT = 400;

frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
frame.setTitle("CircleViewer");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

String input = JOptionPane.showInputDialog("x:");
double x = Double.parseDouble(input);

input = JOptionPane.showInputDialog("y:");
double y = Double.parseDouble(input);

Point2D.Double point = new Point2D.Double(x, y);

CircleComponent component = new CircleComponent(point);
frame.add(component);

frame.setVisible(true);
}
}

-------------------------------------

import java.awt.geom.Point2D;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
/**
This program views a circle and a pocint.
*/
public class CircleViewerSnap
{
public static void main(String[] args)
{
JFrame frame = new JFrame();
final int FRAME_WIDTH = 400;
final int FRAME_HEIGHT = 400;
frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
frame.setTitle("CircleViewer");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Point2D.Double point = new Point2D.Double(150, 150);

CircleComponent component = new CircleComponent(point);
frame.add(component);
frame.setVisible(true);
}
}

Reference no: EM13939935

Questions Cloud

Issue of preferred stock : Holdup Bank has an issue of preferred stock with a $4.95 stated dividend that just sold for $86 per share. What is the bank’s cost of preferred stock?
General funds are blocked by the argentine government : General Eletric's funds are blocked by the Argentine government. GE still wants to continue its operations in the country. Under the circumstance, GE should
Draw the shear force diagram : a) Draw the shear force diagram and indicate the position of maximum bending moment.
Essay - ethical implications of a social psychology study : In this assignment, you will write an essay about the research methods and ethical implications of a social psychology study. You will get information about the study from one of the entries in the SPARQ "Solutions Catalog"
Program that draws a circle with radius 100 and center 200 : Write a program that draws a circle with radius 100 and center (200, 200). Ask the user to specify the x- and y-coordinates of a point
Evaluating two different cookie-baking ovens : You are evaluating two different cookie-baking ovens. The Pillsbury 707 costs $61,000, has a 5-year life, and has an annual OCF (after tax) of –$10,800 per year. The Keebler CookieMunster costs $94,000, has a 7-year life, and has an annual OCF (after..
How much cost should be allocated : How much cost should be allocated to Type 1 and Type 2, respectively?
Evaluating a project for the tiff-any golf club : You are evaluating a project for The Tiff-any golf club, guaranteed to correct that nasty slice. You estimate the sales price of The Tiff-any to be $460 per unit and sales volume to be 1,000 units in year 1; 900 units in year 2; and 1,325 units in ye..
Amount of recorded interest expense : A company issued five-year, 7% bonds with a par value of $175,000. The market rate when the bonds were issued was 6.5%. The company received $198,975 cash for the bonds. Using the straight-line method, the amount of recorded interest expense for the ..

Reviews

Write a Review

C/C++ Programming Questions & Answers

  Which manufactures electrical switches

Jake Company, which manufactures electrical switches, uses a standard cost system and carries all inventories at standard. The standard manufacturing overhead costs per switch are based on direct labor hours and are shown below:Variable overhead (5 h..

  Write a program in which the program print out the input

use (switch statement) to write a program in which the program print out the input (single character) if the character is not '2','t', or 'w'. Use 'default' and 'break' wisely.

  Ruby implement primitive types

How does Ruby implement primitive types, such as those for integer and floating-point data?  3-What is the single most important practical difference between Smalltalk and C++?

  Explain the tree traversals in all orders

If the node 30 is deleted, the erase algorithm selects which node as the replacement node?

  Uml diagram adds an element to the front of the queue?

Refer to the figure above. Assume you add the numbers 23, 76, 64 in this order. The output of the program will be which of the following?

  Create a c++ console application

Objective: Create a C++ console application that will model the characteristics of a resistor. Create a multifile project. Create and add to the project an h file containing the resistor-class definition. Create and add to the project a cpp file cont..

  Elements that must be included in a loop

What are the three elements that must be included in a loop in order for the loop to successfully perform correctly - What will happen if these statements are not included? Provide examples.

  Write a function that takes two point arguments

Write a function that takes two POINT arguments and returns the midpoint between them and define a function distance() that takes two POINT arguments and returns the distance between them.

  Write a program that will be used to gather statistical data

Write a program that will be used to gather statistical data about the number of movies

  Develop a time delay procedure

Develop a time delay procedure for the 2.0 GHz Pentium 4 that waits for 80s.

  Describe the probability of the moves

Write a program in C++ to describe the Probability of the moves

  Least number of comparisons needed

What is the least number of comparisons needed to sort an array of 6 numbers, in the worst case, using any sorting algorithm that sorts with binary comparisons? Explain

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd