roundrectanglefigure.java

来自「eclipse开发笔记」· Java 代码 · 共 70 行

JAVA
70
字号
package sample;

import org.eclipse.draw2d.Graphics;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.Label;
import org.eclipse.draw2d.LightweightSystem;
import org.eclipse.draw2d.Shape;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Device;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

/**
 * @author xuli
 * @2006-12-20
 * TODO 
 */
public class RoundRectangleFigure extends Shape {

	/**
	 * Creates a RectangleFigure.
	 */
	public RoundRectangleFigure() { }

	/**
	 * @see Shape#fillShape(Graphics)
	 */
	protected void fillShape(Graphics graphics) {
		Rectangle r = getBounds();
		int x = r.x + lineWidth / 2;
		int y = r.y + lineWidth / 2;
		int w = r.width - Math.max(1, lineWidth);
		int h = r.height - Math.max(1, lineWidth);
		Color clr = new Color(Display.getCurrent(),0,255,255);
		graphics.setBackgroundColor(clr);
		graphics.fillRoundRectangle(new Rectangle(x,y,w,h), 20,20);
		graphics.drawString("圆角矩形", r.getCenter());
	}

	/**
	 * @see Shape#outlineShape(Graphics)
	 */
	protected void outlineShape(Graphics graphics) {
		Rectangle r = getBounds();
		int x = r.x + lineWidth / 2;
		int y = r.y + lineWidth / 2;
		int w = r.width - Math.max(1, lineWidth);
		int h = r.height - Math.max(1, lineWidth);
		graphics.drawRoundRectangle(new Rectangle(x,y,w,h), 20,20);
		
	}
	public static void main(String args[]){

		Shell shell = new Shell();  
		shell.open();
		shell.setText("Draw2d 圆角矩形");
		LightweightSystem lws = new LightweightSystem(shell);   
		RoundRectangleFigure rfigure = new RoundRectangleFigure();
		lws.setContents(rfigure);

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

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?