一个简单的时钟程序

系统 1516 0
    public class Sample2 {
	private Color black = Display.getCurrent().getSystemColor(SWT.COLOR_BLACK);
	private Color white = Display.getCurrent().getSystemColor(SWT.COLOR_WHITE);
	private Color yellow = Display.getCurrent().getSystemColor(SWT.COLOR_YELLOW);
	private Color red = Display.getCurrent().getSystemColor(SWT.COLOR_RED);
	private Color blue = Display.getCurrent().getSystemColor(SWT.COLOR_BLUE);
	private Color green = Display.getCurrent().getSystemColor(SWT.COLOR_GREEN);

	Timer timer;
	
	int x,y,x0,y0,r,h,olds_x,olds_y,oldm_x,oldm_y,oldh_x,oldh_y,ss,mm,hh,old_m,old_h,ang;
	final double RAD = Math.PI/180;   //

	public static void main(String[] args) {
		Display display = new Display();
		Shell shell = new Shell(display);
		new Sample2().createArea(shell);
//		shell.pack();
		shell.open();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
		display.dispose();
	}

	private void createArea(Shell shell) {
		shell.setText("Sample");
		shell.setLayout(new GridLayout());
		shell.setSize(400, 400);
//		parent.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_BLACK));
		Composite container = new Composite(shell, SWT.BORDER);
		GridLayout gridLayout = new GridLayout();
		gridLayout.numColumns = 1;	gridLayout.marginLeft = 10;	gridLayout.marginRight = 10;
		gridLayout.marginHeight = 10;	gridLayout.marginWidth = 10;	gridLayout.verticalSpacing=0;
		container.setLayout(gridLayout);
		GridData gridData = new GridData(GridData.FILL_BOTH);
		container.setLayoutData(gridData);
		
		final Canvas drawingCanvas = new Canvas(container, SWT.BORDER);
		gridData = new GridData(GridData.FILL_BOTH);
		drawingCanvas.setLayoutData(gridData);
		drawingCanvas.setBackground(black);
		addPaintListener(drawingCanvas);
		
		int delay = 1000;
		ActionListener drawClock = new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				Display.getDefault().syncExec(new Runnable() {
					public void run() {
						if(drawingCanvas.isDisposed())
							return;
						drawingCanvas.redraw();
					}
				});
			}
		};
		timer = new Timer(delay,drawClock);
		timer.start() ;
	}
	
	protected void addPaintListener(Canvas drawingCanvas) {
		drawingCanvas.addPaintListener(new PaintListener() {
			public void paintControl(PaintEvent e) {
				e.gc.setForeground(white);
				Point size = ((Canvas) e.widget).getSize();
				int radius = (Math.min(size.x,size.y)-40)/2;
				Point center = new Point(size.x/2,size.y/2);
				e.gc.setLineCap(SWT.CAP_ROUND);
				e.gc.setLineWidth(3);
				//drawOval (int x, int y, int width, int height)
				e.gc.drawOval(center.x-radius, center.y-radius, 2*radius, 2*radius);
				
				e.gc.setLineWidth(1);
				e.gc.setForeground(red);
				
				e.gc.setForeground(yellow);
				
				//int radius = 
				x0=center.x; y0=center.y; r=radius; h=center.y + radius + 15;
				ang=60;
		        for (int i=1; i<=12; i++) {
		        	x=(int)((r+10)*Math.cos(RAD*ang)+x0);
		        	y=(int)((r+10)*Math.sin(RAD*ang)+y0);
		        	e.gc.drawString(""+i,x,h-y);
		        	ang-=30;
		        }
		        
		        Calendar now = new GregorianCalendar();
		        int nowh = now.get(Calendar.HOUR_OF_DAY );
		        int nowm = now.get(Calendar.MINUTE );
		        int nows = now.get(Calendar.SECOND );
		        String st;
		        if(nowh<10) st = "0"+nowh ; else st = ""+nowh;
		        if(nowm<10) st += ":0"+nowm; else st += ":"+nowm;
		        if(nows<10) st += ":0"+nows; else st += ":"+nows;
//		        System.out.println(st);
		        e.gc.setBackground(green);
		        e.gc.fillRectangle(0+4,0+4,50,14);
		        e.gc.setForeground(blue);
		        e.gc.drawString(st,0+5,0+5);
		        
		        ss = 90-nows*6;
		        mm = 90-nowm*6;
		        hh = 90-nowh*30-nowm/2;
		        x0=center.x; y0=center.y;
//		        g2D.setStroke(new BasicStroke(1.2f));
		        if(olds_x > 0) {
		        	e.gc.setForeground(black);
		        	e.gc.drawLine(x0,y0,olds_x,h-olds_y);
		        } else {
		             old_m = mm;
		             old_h = hh;
		        }
		        x = (int)(r*0.8*Math.cos(RAD*ss))+x0;
		        y = (int)(r*0.8*Math.sin(RAD*ss))+y0;
		        e.gc.setForeground(yellow);
		        e.gc.drawLine(x0,y0,x,h-y);
		        olds_x = x;
		        olds_y = y;
		        e.gc.setLineWidth(2);
		        if(old_m!=mm) {
		        	e.gc.setForeground(black);
		        	e.gc.drawLine(x0,y0,oldm_x,h-oldm_y);
		        }
		        x = (int)(r*0.6*Math.cos(RAD*mm))+x0;
		        y = (int)(r*0.6*Math.sin(RAD*mm))+y0;
		        e.gc.setForeground(green );
		        e.gc.drawLine(x0,y0,x,h-y);
		        oldm_x = x;
		        oldm_y = y;
		        old_m = mm;
		        e.gc.setLineWidth(3);
		        if(old_h!=hh) {
		        	e.gc.setForeground(black);
		        	e.gc.drawLine(x0,y0,oldh_x,h-oldh_y);
		        }
		        x = (int)(r*0.5*Math.cos(RAD*hh))+x0;
		        y = (int)(r*0.5*Math.sin(RAD*hh))+y0;
		        e.gc.setForeground(red );
		        e.gc.drawLine(x0,y0,x,h-y);
		        oldh_x = x;
		        oldh_y = y;
		        old_h = hh;
			}
		});
	}

}
  

 效果图:


一个简单的时钟程序
 

感觉还行,是仿照书上的一个例子写的,书上使用swing实现的。

一个简单的时钟程序


更多文章、技术交流、商务合作、联系博主

微信扫码或搜索:z360901061

微信扫一扫加我为好友

QQ号联系: 360901061

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描下面二维码支持博主2元、5元、10元、20元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。

【本文对您有帮助就好】

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描上面二维码支持博主2元、5元、10元、自定义金额等您想捐的金额吧,站长会非常 感谢您的哦!!!

发表我的评论
最新评论 总共0条评论