用Draw2d画一个静止的时钟

package org.autumn.draw2d.example;

import org.eclipse.draw2d.ColorConstants;
import org.eclipse.draw2d.Ellipse;
import org.eclipse.draw2d.Figure;
import org.eclipse.draw2d.FigureCanvas;
import org.eclipse.draw2d.GridLayout;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.Polyline;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

/**
 * 
 * 用Draw2d画一个静止的时钟
 * 
 * @author sunny
 * 
 */
public class Clock {

	/**
	 * 圆半径
	 */
	private static final int RADIUS = 100;

	/**
	 * 时钟与窗体的间隔距离
	 */
	private static final int MARGIN = 20;

	/**
	 * 时钟上的小时数
	 */
	private static final int HOURS = 12;

	/**
	 * 时钟上的分钟数
	 */
	private static final int MINUTES = 60;

	/**
	 * 小时的刻度
	 */
	private static final int LEN_OF_HOUR = 10;

	/**
	 * 分钟的刻度
	 */
	private static final int LEN_OF_MINUTE = 5;

	public static void main(String[] args) {
		Shell shell = new Shell();
		shell.setText("Clock");
		shell.setSize(3 * RADIUS, 3 * RADIUS);
		shell.setLayout(new FillLayout());

		IFigure panel = new Figure();
		FigureCanvas fc = new FigureCanvas(shell);
		fc.setContents(panel);
		fc.getViewport().setContentsTracksHeight(true);
		fc.getViewport().setContentsTracksWidth(true);
		fc.setLayoutData(new GridData(GridData.FILL_BOTH));
		GridLayout gl = new GridLayout();
		gl.marginWidth = MARGIN;
		gl.marginHeight = MARGIN;
		panel.setLayoutManager(gl);

		// 画圆
		Ellipse ellipse = new Ellipse();
		ellipse.setFill(true);
		ellipse.setBackgroundColor(ColorConstants.white);
		ellipse.setBounds(new Rectangle(0, 0, 2 * RADIUS, 2 * RADIUS));
		panel.add(ellipse);

		// 时钟
		Polyline hour = new Polyline();
		hour.setForegroundColor(ColorConstants.red);
		hour.addPoint(new Point((RADIUS + MARGIN), (RADIUS / 10 * 3 + MARGIN)));
		hour.addPoint(new Point((RADIUS + MARGIN), (RADIUS + MARGIN)));
		hour.setLineWidthFloat(5f);
		ellipse.add(hour);

		// 分钟
		Polyline minute = new Polyline();
		minute.setForegroundColor(ColorConstants.green);
		minute.addPoint(new Point((RADIUS + MARGIN), (RADIUS + MARGIN)));
		minute.addPoint(new Point((RADIUS * 2 + MARGIN - LEN_OF_HOUR), (RADIUS + MARGIN)));
		minute.setLineWidthFloat(3f);
		ellipse.add(minute);

		// 秒钟
		Polyline second = new Polyline();
		second.setForegroundColor(ColorConstants.gray);
		second.addPoint(new Point((RADIUS + MARGIN), (RADIUS + MARGIN)));
		second.addPoint(new Point((RADIUS / 2 + MARGIN), (RADIUS + MARGIN + getEdge(RADIUS / 2))));
		second.setLineWidthFloat(2f);
		ellipse.add(second);

		// 刻度
		for (int i = 1; i <= MINUTES; i++) {
			int len = LEN_OF_MINUTE;
			float lineWidth = 1f;
			if (i % (MINUTES / HOURS) == 0) {
				len = LEN_OF_HOUR;
				lineWidth = 2f;
			}

			int relative = RADIUS + MARGIN;
			double angle = (2 * Math.PI / MINUTES) * i;
			Point from = new Point((relative + RADIUS * Math.sin(angle)), (relative + RADIUS * Math.cos(angle)));
			Point to = new Point((relative + (RADIUS - len) * Math.sin(angle)), (relative + (RADIUS - len)
					* Math.cos(angle)));

			Polyline line = new Polyline();
			line.setForegroundColor(ColorConstants.black);
			line.addPoint(from);
			line.addPoint(to);
			line.setLineWidthFloat(lineWidth);
			ellipse.add(line);
		}

		shell.open();
		Display display = Display.getDefault();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
	}

	private static double getEdge(int i) {
		return Math.sqrt(RADIUS * RADIUS - i * i);
	}

}


用Draw2d画一个静止的时钟

你可能感兴趣的:(draw2d,ellipse)