【问题标题】:Trouble Drawing Nested Circles绘制嵌套圆时遇到问题
【发布时间】:2013-12-31 10:55:04
【问题描述】:

我的程序询问用户他们想要绘制多少个圆圈,并且应该根据用户输入绘制 N 个嵌套圆圈。到目前为止我的代码如下:

import javax.swing.*;
import java.awt.*;
public class DrawCircles extends JFrame {

DrawCircles(){
    add(new Circle());
}

public static void main(String[] args) {
    String number = JOptionPane.showInputDialog(null, "Please enter the number of circles you wish to display");
    int circles = Integer.parseInt(number);
    DrawCircles d = new DrawCircles();
    d.setTitle("Nested Circles");
    d.setSize(500, 500);
    d.setVisible(true);
    d.setLocation(200,200);

}//end main method




}//end class

class Circle extends JPanel{
public void paint(Graphics g){
    g.drawOval(135, 125, 200, 200);
}//end paint()
}//end class

我不知道如何从 main 方法中获取用户输入并在 for 循环中使用它来绘制 N 个圆圈。此外,我必须相应地调整我的 JFrame 大小,以适应我必须将它们全部放入其中的圆圈数量,我也不知道该怎么做。提前致谢。

【问题讨论】:

    标签: java graphics geometry nested awt


    【解决方案1】:

    首先不要覆盖paint,而是使用paintComponent,并确保在调用时调用super.paintComponent。查看Performing custom painting 了解更多详情。

    只需将circles 值作为参数传递给Circle 类的构造函数和/或提供一个设置器来更改值

    【讨论】:

      猜你喜欢
      • 2014-05-07
      • 1970-01-01
      • 1970-01-01
      • 2021-07-17
      • 2021-12-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多