用裴波那契数列所得半径,作为圆的半斤,画图如下:
以下为源代码:
- import java.awt.Color;
- import java.awt.Graphics;
- import javax.swing.JFrame;
- import javax.swing.JPanel;
- public class Draw{
- public static void main(String[] args){
- //初始化frame容器
- Circle e=new Circle();
- e.setVisible(true);
- }
- }
- //定义窗口
- class Circle extends JFrame{
- private static final long serialVersionUID = 1L;
- public Circle(){
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- setSize(1280,768);
- getContentPane().setBackground(Color.red);
- setVisible(true);
- DrawPanel panel=new DrawPanel();
- add(panel);
- }
- }
- //定义画板
- class DrawPanel extends JPanel{
- /**
- *
- */
- private static final long serialVersionUID = 2L;
- public void paintComponent(Graphics g){
- super.paintComponent(g);
- for(int i=0;i<20;i++)
- {
- g.fillOval(fibo(i),fibo(i), fibo(i), fibo(i));
- }
- }
- //递归调用实现裴波那契数列,并将其值作为圆的的半径
- public static int fibo(int x){
- if(x<=2){
- if(x<=0)return 0;
- else
- return 1;
- }
- else{
- return fibo(x-1)+fibo(x-2);
- }
- }
- }