java awt绘图

  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5. package mygriphics;
  6. import java.awt.*;
  7. import java.awt.event.*;
  8. /**
  9.  *
  10.  * @author mdj
  11.  */
  12. public class OvalDrawer extends Frame implements Runnable{
  13.     private Color[] colors={Color.RED,Color.BLACK,Color.BLUE,Color.GREEN,Color.DARK_GRAY};
  14.     private Color color;
  15.     private int x=100,y=100,width=100,height=100;
  16.     public OvalDrawer(String title){
  17.         super(title);
  18.         setSize(300,300);
  19.         setVisible(true);
  20.        
  21.         new Thread(this).start();
  22.     
  23.     }
  24.     public void run(){
  25.         while(true){
  26.         x=(int)(Math.random()*300);
  27.         y=(int)(Math.random()*300);
  28.         width=(int)(Math.random()*100);
  29.         height=(int)(Math.random()*100);
  30.         color=colors[(int)(Math.random()*colors.length-1)];
  31.         repaint();
  32.         try{
  33.         Thread.sleep(400);
  34.         }
  35.         catch(InterruptedException e){
  36.             throw new RuntimeException(e);
  37.         
  38.         }
  39.         }
  40.         
  41.     
  42.     }
  43.     public void update(Graphics g){
  44.         paint(g);
  45.                                      }
  46.     public void paint(Graphics g){
  47.         g.setColor(color);
  48.         g.fillOval(x, y, width, height);
  49.     }
  50.     /**
  51.      * @param args the command line arguments
  52.      */
  53.     public static void main(String[] args) {
  54.         // TODO code application logic here
  55.         new OvalDrawer("paint");
  56.     }
  57. }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值