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

  Create program that uses functions and reference parameters

Create program that uses functions and reference parameters, and asks user for the outside temperature.

  Write a program using vectors and iterators

Write a program using vectors and iterators that allows a user to maintain a personal list of DVD titles

  Write the code required to analyse and display the data

Calculate and store the average for each row and column. Determine and store the values for the Average Map.

  Write a webservices application

Write a webservices application that does a simple four function calculator

  Iimplement a client-server of the game

Iimplement a client-server version of the rock-paper-scissors-lizard-Spock game.

  Model-view-controller

Explain Model-View-Controller paradigm

  Design a nested program

How many levels of nesting are there in this design?

  Convert celsius temperatures to fahrenheit temperatures

Write a C++ program that converts Celsius Temperatures to Fahrenheit Temperatures.

  Evaluate and output the value in the given base

Write C program that will input two values from the user that are a Value and a Base with which you will evaluate and output the Value in the given Base.

  Design a base class shape with virtual functions

Design a base class shape with virtual functions

  Implementation of classes

Implementation of classes Chart and BarChart. Class barChart chould display a simple textual representation of the data

  Technical paper: memory management

Technical Paper: Memory Management, The intent of this paper is to provide you with an in depth knowledge of how memory is used in executing, your programs and its critical support for applications.

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