main方法添加2个集合存储图形

package cn.test.circle;

import java.awt.Color;
import java.awt.Graphics;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JFrame;

public class MyFrame extends JFrame{
    
    private List<MyCircle> list = new ArrayList<MyCircle>();
    
    private List<MyRectangle> recList = new ArrayList<MyRectangle>();
    
    /**
     * 添加到圆的集合
     * @param c
     */
    public void addMyCircleList(MyCircle c){
        this.list.add(c);
    }
    /**
     * 添加矩形到集合
     * @param r
     */
    
    public void addRecList(MyRectangle r){
        this.recList.add(r);
    }
    
    
    @Override
    public void paint(Graphics g) {
        super.paint(g);
        Color color = g.getColor();
        for(MyCircle m:list){
            m.drawMe(g);
        }
        g.setColor(color);
        for(MyRectangle m:recList){
            m.drawMe(g);
        }
    }
    
    
    public static void main(String[] args) {
        new MyFrame();
    }
    
    public  MyFrame(){
        this.setSize(400, 400);
        this.setLocation(300, 80);
        this.setDefaultCloseOperation(3);
        
        //初始化添加圆
        MyCircle c1 = new MyCircle(10, 40, 20, 20);
        MyCircle c2 = new MyCircle(10, 40, 20, 20);
        this.addMyCircleList(c1);
        this.addMyCircleList(c2);
        
        //初始化添加矩形
        MyRectangle r1 = new MyRectangle(10, 30, 40, 50);
        MyRectangle r2 = new MyRectangle(40, 20, 60, 50);
        this.addRecList(r1);
        this.addRecList(r2);
        
        this.setVisible(true);
        new RefreshThread().start();
    }
    
    /**
     * 不断刷新屏幕的线程
     * @author Administrator
     *
     */
    private class RefreshThread extends Thread{
        @Override
        public void run() {
            super.run();
            while(true){
                repaint();
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }
}    

 

posted @ 2018-01-17 14:00  yoyo198212  阅读(159)  评论(0编辑  收藏  举报