日期:2014-05-20 浏览次数:21078 次
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++){