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实现的。