日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

一個簡單的時鐘程序

系統 1994 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條評論
主站蜘蛛池模板: 金山区| 聂荣县| 祁阳县| 汾西县| 阿拉善右旗| 乌拉特中旗| 万全县| 云浮市| 集贤县| 宝应县| 上犹县| 大洼县| 天镇县| 临西县| 汤原县| 巍山| 永城市| 洪泽县| 沙湾县| 迁安市| 沈丘县| 庆城县| 柯坪县| 政和县| 荣昌县| 出国| 宁陵县| 简阳市| 泰兴市| 井冈山市| 阳春市| 剑河县| 利辛县| 如东县| 卢氏县| 淮安市| 绍兴市| 宜良县| 富蕴县| 图们市| 利津县|