绘制一个自动旋转的彩色圆盘

public class AnimatePlate extends JFrame{

   

    private static final int DELAY=100;

    private int offset;

    //窗口的边框

    Insets inset;

    //颜色数组

    Color colors[]={Color.RED,Color.ORANGE,Color.YELLOW,Color.GREEN,Color.BLUE,Color.MAGENTA,Color.CYAN};

    //画屏函数

    public void paint(Graphics g){

       super.paint(g);

       if(inset==null){

           inset=this.getInsets();

       }

       int x=inset.left;

       int y=inset.top;

       int width=this.getWidth()-x-inset.right;

       int height=this.getHeight()-y-inset.bottom;

       int start=0;

       int steps=colors.length;

       int stepSize=360/steps;

       //如果不能整除,画一周后将会留下一部分区域。

       int lack=360%steps;

       synchronized (colors) {

           for(int i=0;i<steps;i++){

              if(lack!=0){

                  //画最后一块区域,加上少画的角度

                  if(i==steps-1){

                     stepSize+=lack;

                  }

              }

              int k=(i+offset)%colors.length;

              g.setColor(colors[k]);

              //画椭圆,前四个参数指定外接矩形,然后起始角度,要填充角度

              g.fillArc(x, y, width, height,start, stepSize);

              start+=stepSize;

           }

       }

    }

    public void start(){

       TimerTask task=new TimerTask(){

 

           @Override

           public void run() {

              offset++;

              if(offset>=colors.length){

                  offset=0;

              }

              repaint();

           }

       };

       Timer timer=new Timer();

       timer.schedule(task, 0, AnimatePlate.DELAY);

    }

    public static void main(String[] args) {

       AnimatePlate frame=new AnimatePlate();

       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

       frame.setSize(200, 200);

       frame.setVisible(true);

       frame.start();

 

    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值