I tried again and again to find a solution to this problem, but it seems I can’t figure out how to solve it.
I am trying to write a simple circle drawing program with these specifications in a program:
- Set the radius of the circle for the user using the input field (JOptionPane).
- Set the x and y coordinates of the circle in the input field (JOptionPane).
- Calculate the circle circumference.
- Calculate the area of a circle.
- Display the area and circle below the circle pattern.
Be that as it may, he will not draw a circle and calculate the circle and region. Could you help me find a solution to this problem?
Here are my codes:
------- main -------
package circleSquarePackage;
import circleSquarePackage.Circle;
import javax.swing.JOptionPane;
import javax.swing.JFrame;
public class CircleTester {
public static void main(String[] args)
{
JFrame frame = new JFrame();
frame.setSize(600, 500);
frame.setTitle("CircleSquare");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Circle round = new Circle();
frame.add(round);
frame.setVisible(true);
}
}
-------- another class ---------
package circleSquarePackage;
import javax.swing.JComponent;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Ellipse2D.Double;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Color;
import javax.swing.JOptionPane;
public class Circle extends JComponent {
private Ellipse2D.Double sphere;
private int radius;
double circumference, area;
String x1;
String y1;
String r1;
String draw;
int x = 0;
int y = 0;
int r = 0;
public Circle()
{
sphere = new Ellipse2D.Double();
}
public Circle(int xAxis, int yAxis, int rad)
{
rad = r;
xAxis = x;
yAxis = y;
sphere = new Ellipse2D.Double(xAxis, yAxis, rad, rad);
}
public double calcCircumference()
{
return circumference = 2 * Math.PI * radius;
}
public double calcArea()
{
return area = Math.PI * radius * radius;
}
public void inputX()
{
x1 = JOptionPane.showInputDialog(null, "Input center (x value): ");
x = Integer.parseInt(x1);
}
public void inputY()
{
y1 = JOptionPane.showInputDialog(null, "Input center (y value): ");
y = Integer.parseInt(y1);
}
public void inputRadius()
{
r1 = JOptionPane.showInputDialog(null, "Input radius: ");
r = Integer.parseInt(r1);
}
public void paintComponent(Graphics g)
{
Graphics2D g2 = (Graphics2D) g;
g2.setColor(Color.BLUE);
g2.fill(sphere);
g2.draw(sphere);
g2.setColor(Color.BLUE);
g2.drawString("Circumference = " + calcCircumference(), 5, 450);
g2.drawString("Area = " + calcCircumference(), 200, 450);
}
}
UPDATE BASED ON PEESKILLET RESPONSE
Circle class
package circleSquarePackage;
import javax.swing.JComponent;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Ellipse2D.Double;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Color;
import java.awt.Dimension;
import javax.swing.JOptionPane;
public class Circle extends JComponent {
private Ellipse2D.Double sphere;
private int radius;
double circumference, area;
public Circle()
{
sphere = new Ellipse2D.Double();
}
public void setSphere(Ellipse2D.Double sphere) {
this.sphere = sphere;
repaint();
}
public Circle(int xAxis, int yAxis, int rad)
{
sphere = new Ellipse2D.Double(xAxis, yAxis, rad, rad);
}
public double calcCircumference()
{
return circumference = 2 * Math.PI * radius;
}
public double calcArea()
{
return area = Math.PI * radius * radius;
}
public void inputX()
{
int x = Integer.parseInt(JOptionPane.showInputDialog(null, "Enter x"));
double y = sphere.y;
Ellipse2D.Double newSphere = new Ellipse2D.Double(x, y, size, size);
setSphere(newSphere);
}
public void inputY()
{
int y = Integer.parseInt(JOptionPane.showInputDialog(null, "Enter y"));
double x = sphere.x;
Ellipse2D.Double newSphere = new Ellipse2D.Double(x, y, size, size);
setSphere(newSphere);
}
public void inputRadius()
{
int r = Integer.parseInt(JOptionPane.showInputDialog(null, "Enter radius"));
int size = r * 2;
Ellipse2D.Double newSphere = new Ellipse2D.Double(x, y, size, size);
setSphere(newSphere);
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.setColor(Color.BLUE);
g2.fill(sphere);
g2.draw(sphere);
g2.drawString("Circumference: " + calcCircumference(), 5, 490);
}
@Override
public Dimension getPreferredSize() {
return new Dimension(600, 500);
}
}
-------- CircleTester class --------
package circleSquarePackage;
import circleSquarePackage.Circle;
import javax.swing.JOptionPane;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Ellipse2D.Double;
public class CircleTester {
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JFrame frame = new JFrame();
frame.setTitle("CircleSquare");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Circle round = new Circle();
frame.add(round);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
}