日期:2014-05-20  浏览次数:20888 次

雷达时钟
这两天真是闲的蛋疼了,有事也不想做,哎……

Java code

import java.awt.AlphaComposite;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.Toolkit;
import java.awt.geom.Ellipse2D;
import java.awt.image.BufferedImage;
import java.util.Date;

import javax.swing.JFrame;

/**
 * 雷达表。
 * @author Mars.CN
 * @homepage http://hi.baidu.com/2427
 *
 */
public class Radar extends JFrame implements Runnable{
    private int pangle=0;    //当前搜索角度
    private int h_x=0 ,h_y=0 , h_p=360;
    private int m_x=0 ,m_y=0 , m_p=360;
    private int s_x=0 ,s_y=0 , s_p=360;
    private int centerX = 150;
    private int centerY = 150;
    private int r = 100;// 半径
    
    public Radar() {
        setTitle("雷达时钟");
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width - 300) / 2, (screenSize.height - 300) / 2,
                300, 300);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setResizable(false);
        setVisible(true);
        new Thread(this).start();
    }

    public static void main(String[] args) {
        new Radar();
    }

    @Override
    public void paint(Graphics gp) {
        BufferedImage img = new BufferedImage(300, 300, BufferedImage.TYPE_INT_RGB);
        Graphics2D g = (Graphics2D) img.getGraphics();
        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON);

        // 画背景
        g.setColor(new Color(42, 42, 42));
        g.fillRect(0, 0, 300, 300);

        // 画圆
        g.setStroke(new BasicStroke(5f));
        Ellipse2D circle = new Ellipse2D.Double();
        g.setColor(new Color(0, 255, 0));
        circle.setFrameFromCenter(centerX, centerY, centerX + r, centerY + r);
        g.draw(circle);

        // 划十二分线
        g.setStroke(new BasicStroke(1f));
        g.setComposite(AlphaComposite
                .getInstance(AlphaComposite.SRC_ATOP, 0.1f)); // 开始
        for (int i = 0; i < 12; i++) {
            double angle = Math.PI * (180 - 30 * i) / 180.0;
            int x = centerX - (int) (r * Math.cos(angle));
            int y = centerY + (int) (r * Math.sin(angle));
            g.drawLine(centerX, centerY, x, y);
        }
        
        
        circle.setFrameFromCenter(centerX, centerY, centerX + 20, centerY + 20);
        g.draw(circle);
        circle.setFrameFromCenter(centerX, centerY, centerX + 40, centerY + 40);
        g.draw(circle);
        circle.setFrameFromCenter(centerX, centerY, centerX + 60, centerY + 60);
        g.draw(circle);
        circle.setFrameFromCenter(centerX, centerY, centerX + 80, centerY + 80);
        g.draw(circle);
        g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER)); // 结束
        
        g.setStroke(new BasicStroke(3f));
        for(float i=0 ; i<90 ; i++){
            g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, (i/90))); // 开始
            double angle = Math.PI * (180 - (pangle + i)) / 180.0;
            int x = centerX - (int) (r * Math.cos(angle));
            int y = centerY + (int) (r * Math.sin(angle));
            g.drawLine(centerX, centerY, x, y);
            g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER)); // 结束
        }
        
        int m_r=5;
        g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, (h_p*1.0f/360))); // 开始
        circle.setFrameFromCenter(h_x, h_y, h_x+m_r, h_y+m_r );
        g.fill(circle);
        g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER)); // 结束
        g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, (m_p*1.0f/360))); // 开始
        circle.setFrameFromCenter(m_x, m_y, m_x+m_r, m_y+m_r );
        g.fill(circle);
        g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER)); // 结束
        g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, (s_p*1.0f/360))); // 开始
        circle.setFrameFromCenter(s_x, s_y, s_x+m_r, s_y+m_r );
        g.fill(circle);
        g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER)); // 结束
        gp.drawImage(img, 0, 0, img.getWidth(),img.getHeight(),null);
    }

    @Override
    public void run() {
        while(true){
            pangle += 1;
            if(pangle>360){
                pangle=0;
            }
            
            //设置当前时间
            Date now = new Date();
            int h = now.getHours() % 12;
            int m = now.getMinutes();
            int s = now.getSeconds();
            
            double h_angle = Math.PI * (180 - (360/12)* h + 90 -(30*m/59)) / 180.0;
            double m_angle = Math.PI * (180 - (360/60)* m +90 -(6*s/59)) / 180.0;
            double s_angle = Math.PI * (180 - (360/60)* s +90) / 180.0;
            
            if(((360/12)* h-(6*s/59)+220) % 360 == pangle){
                //如果小时的角度和当前扫描角度一样,小时的亮度为最高 360
                h_x = centerX - (int)(40 * Math.cos(h_angle));
                h_y = centerY + (int)(40 * Math.sin(h_angle));
                h_p=360;
            }
            if(((360/60)* m-(6*s/59)+200) % 360==pangle){
                m_x = centerX - (int)(60 * Math.cos(m_angle));
                m_y = centerY + (int)(60 * Math.sin(m_angle));
                m_p=360;
            }
            if(((360/60)* s+190) % 360 == pangle){
                s_x = centerX - (int)(80 * Math.cos(s_angle));
                s_y = centerY + (int)(80 * Math.sin(s_angle));
                s_p=360;
            }
            h_p=h_p-1<0?0:h_p-1;
            m_p=m_p-1<0?0:m_p-1;
            s_p=s_p-1<0?0:s_p-1;
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            repaint();
        }
    }
}