Explore BrainMass
Share

Using the Java GUI

This content was STOLEN from BrainMass.com - View the original, and get the already-completed solution here!

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);
}
}

© BrainMass Inc. brainmass.com October 25, 2018, 9:27 am ad1c9bdddf
https://brainmass.com/computer-science/java/using-java-gui-574610

Solution Summary

This solution involves using a Java GUI to write a program that will decide if if a point is inside a circle

$2.19
See Also This Related BrainMass Solution

Java GUI program to calculate restaurant bill

 All the variables have to be declared and initialized inside the method before they are used.
 All the arrays have to be declared before they are used.
 No global variables are allowed to be used in your application class.

Problem Descriptions

Write a GUI application that produces a restaurant bill. You create a bill calculator to generate window output. You should prompt the user to enter all the dishes and their prices using JOptionPane, and then display all the dishes, their prices, the total price before tax, the tax, and the total price after tax. Your output should be in a window, using a class you have derived from JFrame. Your window should include a title and appropriate icon, which you can choose. Each customer cannot order more than three dishes in each bill,
and the tax rate is 15%. You should have three classes shown as follows:

1. A Bill class to model a bill.
2. A BillFrame class to define a bill window.
3. A BillCalculator class to get a user' inputs and to generate the below outputs.

Assignment Submission

1. Submit a UML class diagram for the Bill class.
2. Submit all the three java source codes.
3. Submit the RED-STAR screenshots.

Program Sample Inputs and Outputs 1

View Full Posting Details