Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/332.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Java中根据用户输入绘制圆_Java_Swing_Paintcomponent_Joptionpane - Fatal编程技术网

在Java中根据用户输入绘制圆

在Java中根据用户输入绘制圆,java,swing,paintcomponent,joptionpane,Java,Swing,Paintcomponent,Joptionpane,我一次又一次地试图找到解决这个问题的办法,但我似乎不知道如何解决它 我正试图编写一个简单的程序,用程序中的这些规范画一个圆: 使用输入框(JOptionPane)向用户询问圆的半径 请用户在输入框(JOptionPane)中输入圆的x和y坐标 计算圆的周长 计算圆的面积 显示圆图形下方的面积和周长 不知怎的,它不会画圆,也不会计算周长和面积。你能帮我找到解决这个问题的办法吗 这是我的密码: -------主要------- --------其他类--------- 根据PEESKILLET的回

我一次又一次地试图找到解决这个问题的办法,但我似乎不知道如何解决它

我正试图编写一个简单的程序,用程序中的这些规范画一个圆:

  • 使用输入框(JOptionPane)向用户询问圆的半径
  • 请用户在输入框(JOptionPane)中输入圆的x和y坐标
  • 计算圆的周长
  • 计算圆的面积
  • 显示圆图形下方的面积和周长
  • 不知怎的,它不会画圆,也不会计算周长和面积。你能帮我找到解决这个问题的办法吗

    这是我的密码:

    -------主要-------

    --------其他类---------


    根据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);
    }
    
    你可以这样做,其他人也一样。这样,当您调用该方法时,一旦输入,它将只更改一个变量

    您也可以使用一种方法获取所有变量,就像我在示例中所做的那样

  • 从Circle类中获取所有UI,这意味着删除所有那些JOptionPane调用
  • 相反,所有用户交互都应该在CircleTester类中
  • Circle类不应该关注用户IO,而应该只关注绘制由其属性定义的圆
  • 为Circle类指定一个构造函数,该构造函数接受有意义的参数
  • 在我看来,Ellipse2D是不需要的,因为您可以通过调用g.drawOval(..)来简单地用图形对象绘制圆,从而简化您的程序
  • 获取CircleTester内部的输入参数后,创建传递从CircleTest获取的参数的圆对象,并显示圆
  • 另外,不要忘记在paintComponent方法覆盖中调用super.paintComponent(g)方法
  • 小挑剔,但我不会声明周长和面积变量,而是给你的圆类
    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);
    }