在Java中根据用户输入绘制圆
我一次又一次地试图找到解决这个问题的办法,但我似乎不知道如何解决它 我正试图编写一个简单的程序,用程序中的这些规范画一个圆:在Java中根据用户输入绘制圆,java,swing,paintcomponent,joptionpane,Java,Swing,Paintcomponent,Joptionpane,我一次又一次地试图找到解决这个问题的办法,但我似乎不知道如何解决它 我正试图编写一个简单的程序,用程序中的这些规范画一个圆: 使用输入框(JOptionPane)向用户询问圆的半径 请用户在输入框(JOptionPane)中输入圆的x和y坐标 计算圆的周长 计算圆的面积 显示圆图形下方的面积和周长 不知怎的,它不会画圆,也不会计算周长和面积。你能帮我找到解决这个问题的办法吗 这是我的密码: -------主要------- --------其他类--------- 根据PEESKILLET的回
根据PEESKILLET的回答进行更新 圆类
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);
}
}
--------圈级--------
在无参数
圆
构造函数中,在获取值之前,您正在创建一个球体
(默认值Ellipse2D.Double
)。您应该基于这些值创建球体
,就像在三参数构造函数中一样
从
Ellipse2D.Double()
构造一个新的椭圆E2D,初始化为位置(0,0)和大小(0,0) 另一种设计可能性
Circle
类中有一个setSphere
方法,可以设置椭圆并重新绘制
public void setSphere(Ellepse2D.Double sphere) {
this.sphere = sphere;
repaint();
}
JOptionPane
。似乎是对的。然后,当您获得这些值时,可以调用圆圈
类上的设置球体
,使用新的椭圆E2D.Double
,它将显示在面板中- 在进行自定义绘制时,最好在绘制的组件上覆盖
,只需在框架上调用getPreferredSize()
,而不是pack()
setSize()
- 请参阅初始线程。Swing应用程序应在事件调度线程上启动的上运行
- 另外,在
类中不需要冗余的循环
值。它们已经被x、y等
对象持有。如果需要获取一些值,只需从中获取,即椭圆
,sphere.x
,等等sphere.y.
更新 我想知道如何将请求Circle类中用户输入的JOptionPane放在public void inputX()、public void inputY()和public void inputRadius()下,然后在CircleTester类的main中调用它们
您所能做的就是在每个方法中调用JOPtionPane。假设您只想得到x,然后调用JOptionPane,并基于该值,使用旧值和新x从旧椭圆创建一个新椭圆。然后调用
setSphere
。差不多
public void inputX() {
int x = Integer.parseInt(JOptionPane.showInputDialog(null, "Enter x"));
double y = sphere.y;
double height = sphere.height;
double width = sphere.width;
Ellips2D.Double newSpehere = new Ellipse2D.Double(x, y, width, height);
setSphere(newSphere);
}
你可以这样做,其他人也一样。这样,当您调用该方法时,一旦输入,它将只更改一个变量
您也可以使用一种方法获取所有变量,就像我在示例中所做的那样
getpercentral()
和getArea()
方法,它们会在需要时当场计算这些。否则,如果您为Circle指定了一个setRadius(…)
方法,则必须记住更改setter方法中的其他字段,如果将radius或其他字段设置为公共字段,则所有检查都会丢失。注意安全,对这两个属性使用访问器方法,而不是字段谢谢你的帮助!这管用!我想知道如何将请求Circle类中用户输入的JOptionPane放在public void inputX()、public void inputY()和public void inputRadius()下,然后在CircleTester类的main中调用它们?您可以查看上面的代码来了解我在说什么。您只需在每个方法中调用
JOPtionPane
。假设您只想得到x,然后调用JOptionPane,并基于该值,使用旧值和新x从旧椭圆创建一个新椭圆。然后调用setSphere
。给我一分钟时间举个例子,我在上面的原始代码下面添加了一个新的更新。我在Circle类的public void inputX()和public void inputRadius()以及CircleTester类的public static void main()中留下了一些注释。你能检查一下我做的是否正确吗?inputRadius
你没有使用r
值。您基本上只是创建相同的椭圆。半径是半个圆。你不应该把它乘以2,然后用它来表示高度和宽度吗。既然高度和宽度应该是一样的?看看我在getCircl中是如何做到的
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);
*/
}
});
}
}
public void setSphere(Ellepse2D.Double sphere) {
this.sphere = sphere;
repaint();
}
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Ellipse2D;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;
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);
Ellipse2D.Double newCircle = getCircle();
round.setSphere(newCircle);
}
});
}
private static Ellipse2D.Double getCircle() {
int x = Integer.parseInt(JOptionPane.showInputDialog(null, "Enter x"));
int y = Integer.parseInt(JOptionPane.showInputDialog(null, "Enter x"));
int radius = Integer.parseInt(JOptionPane.showInputDialog(null, "Enter x"));
int size = radius * 2;
return new Ellipse2D.Double(x, y, size, size);
}
}
class Circle extends JComponent {
private Ellipse2D.Double sphere;
public Circle() {
sphere = new Ellipse2D.Double();
}
public void setSphere(Ellipse2D.Double sphere) {
this.sphere = sphere;
repaint();
}
@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);
}
@Override
public Dimension getPreferredSize() {
return new Dimension(300, 300);
}
}
public void inputX() {
int x = Integer.parseInt(JOptionPane.showInputDialog(null, "Enter x"));
double y = sphere.y;
double height = sphere.height;
double width = sphere.width;
Ellips2D.Double newSpehere = new Ellipse2D.Double(x, y, width, height);
setSphere(newSphere);
}