我正在尝试编写一个简单的程序来绘制符合以下规格的圆形:
1.使用输入框(JOptionPane)询问用户圆的半径。 2.使用输入框(JOptionPane)询问用户圆的x和y坐标。 3.计算圆的周长。 4.计算圆的面积。 5.在圆形图下方显示面积和周长。
然而,它无法绘制圆形,也无法计算周长和面积。您能否帮助我找到解决此问题的方法吗?
以下是我的代码:
------- 主函数 -------
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();
// Display Circle in the frame
frame.setSize(600, 500);
frame.setTitle("CircleSquare");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Create Circle Components
Circle round = new Circle();
frame.add(round);
frame.setVisible(true);
}
}
-------- 其他类 ---------
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 {
// Member instance field
private Ellipse2D.Double sphere;
private int radius;
double circumference, area;
String x1; // starting x co-ordinate
String y1; // starting y co-ordinate
String r1; // radius for circle
String draw;
int x = 0;
int y = 0;
int r = 0;
// Default constructor
public Circle()
{
sphere = new Ellipse2D.Double();
}
// User defined constructor
public Circle(int xAxis, int yAxis, int rad)
{
rad = r;
xAxis = x;
yAxis = y;
sphere = new Ellipse2D.Double(xAxis, yAxis, rad, rad);
}
// Accessor methods
public double calcCircumference()
{
return circumference = 2 * Math.PI * radius;
}
public double calcArea()
{
return area = Math.PI * radius * radius;
}
// Methods
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)
{
// Cast 1D to 2D graphics
Graphics2D g2 = (Graphics2D) g;
g2.setColor(Color.BLUE); // set circle color to blue
g2.fill(sphere);
g2.draw(sphere);
g2.setColor(Color.BLUE);
g2.drawString("Circumference = " + calcCircumference(), 5, 450);
g2.drawString("Area = " + calcCircumference(), 200, 450);
}
}
根据Peeskillet的回答更新
Circle类
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 {
// Member instance field
private Ellipse2D.Double sphere;
private int radius;
double circumference, area;
// Default constructor
public Circle()
{
sphere = new Ellipse2D.Double();
}
public void setSphere(Ellipse2D.Double sphere) {
this.sphere = sphere;
repaint();
}
// User defined constructor
public Circle(int xAxis, int yAxis, int rad)
{
sphere = new Ellipse2D.Double(xAxis, yAxis, rad, rad);
}
// Accessor methods
public double calcCircumference()
{
return circumference = 2 * Math.PI * radius;
}
public double calcArea()
{
return area = Math.PI * radius * radius;
}
// Methods
public void inputX()
{
int x = Integer.parseInt(JOptionPane.showInputDialog(null, "Enter x"));
double y = sphere.y; // why is there a double y here when it asks for x?
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()
{
// is this how I do for radius?
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); // set circle color to blue
g2.fill(sphere);
g2.draw(sphere);
g2.drawString("Circumference: " + calcCircumference(), 5, 490);
}
@Override
public Dimension getPreferredSize() {
return new Dimension(600, 500);
}
}
-------- CircleTester类 --------
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 {
/*
private static Ellipse2D.Double getCircle() {
int x = Integer.parseInt(JOptionPane.showInputDialog(null, "Enter integer for x-coordinates:"));
int y = Integer.parseInt(JOptionPane.showInputDialog(null, "Enter integer for y-coordinates:"));
int radius = Integer.parseInt(JOptionPane.showInputDialog(null, "Enter radius of circle:"));
int size = radius * 2;
return new Ellipse2D.Double(x, y, size, size);
}*/
public static void main(String[] args)
{
// Is there a way to call the inputX(), inputY(), and inputRadius() here?
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);
/*
Ellipse2D.Double newCircle = getCircle();
round.setSphere(newCircle);
*/
}
});
}
}
JOptionPane
。比如说,如果你只想获取 x 的值,那么就调用 JOptionPane,并根据该值使用旧的值创建一个新的 Ellipse,然后调用setSphere
。稍等一下,我来给你举个例子。 - Paul SamsothainputRadius
你没有使用r
值。你基本上只是创建了相同的椭圆。半径是一个圆的一半。所以你不应该将其乘以2并将其用于高度和宽度吗?因为高度和宽度应该相同。看看我在getCircle()
方法中是如何做的。 - Paul Samsotharound.inputX()
。round
是Circle
的实例。inputX()
是一个实例方法,不是静态方法。当你使用Circle.inputX()
时,这是一个静态调用。你需要在Circle
的实例上调用它。你应该对两者之间的区别进行一些研究。 - Paul Samsotha