Eclipse的非矩形图元
来源:互联网 发布:360软件管家界面太大 编辑:程序博客网 时间:2024/05/17 08:08
现在假设要把原来GefPractice例子里的矩形图元节点换成用椭圆形表示,都需要做哪些改动呢?很显然,首先要把原来继承RectangleFigure的NodeFigure类改为继承Ellipse:
public class NodeFigure extends Ellipse /*RectangleFigure*/{
}
}
这样修改后可以看到编辑器中的图元已经变成椭圆形了。但如果用户点选一个图元,表示选中的边框(选择框)仍然是矩形的,如图1所示:
图1 椭圆形的节点和矩形选择框
如果觉得矩形的选择框不太协调,可以通过覆盖DiagramLayoutEditPolicy的createChildEditPolicy()方法修改。缺省情况下这个方法返回一个ResizableEditPolicy,我们要定义自己的子类(EllipseResizableEditPolicy)来替代它作为返回值。
EllipseResizableEditPolicy里需要覆盖ResizableEditPolicy的两个方法,createSelectionHandles()方法决定“控制柄”(ResizeHandle)和“选择框”(MoveHandle)的相关情况,我们的实现如下:
protected List createSelectionHandles() {
List list = new ArrayList();
//添加选择框
//ResizableHandleKit.addMoveHandle((GraphicalEditPart) getHost(), list);
list.add(new MoveHandle((GraphicalEditPart) getHost()) {
protected void initialize() {
super.initialize();
setBorder(new LineBorder(1) {
public void paint(IFigure figure, Graphics graphics, Insets insets) {
tempRect.setBounds(getPaintRectangle(figure, insets));
if (getWidth() % 2 == 1) {
tempRect.width--;
tempRect.height--;
}
tempRect.shrink(getWidth() / 2, getWidth() / 2);
graphics.setLineWidth(getWidth());
if (getColor() != null)
graphics.setForegroundColor(getColor());
//用椭圆形替代矩形
//graphics.drawRectangle(tempRect);
graphics.drawOval(tempRect);
}
});
}
});
//添加控制柄
ResizableHandleKit.addHandle((GraphicalEditPart) getHost(), list, PositionConstants.EAST);
ResizableHandleKit.addHandle((GraphicalEditPart) getHost(), list, PositionConstants.SOUTH);
ResizableHandleKit.addHandle((GraphicalEditPart) getHost(), list, PositionConstants.WEST);
ResizableHandleKit.addHandle((GraphicalEditPart) getHost(), list, PositionConstants.NORTH);
return list;
}
List list = new ArrayList();
//添加选择框
//ResizableHandleKit.addMoveHandle((GraphicalEditPart) getHost(), list);
list.add(new MoveHandle((GraphicalEditPart) getHost()) {
protected void initialize() {
super.initialize();
setBorder(new LineBorder(1) {
public void paint(IFigure figure, Graphics graphics, Insets insets) {
tempRect.setBounds(getPaintRectangle(figure, insets));
if (getWidth() % 2 == 1) {
tempRect.width--;
tempRect.height--;
}
tempRect.shrink(getWidth() / 2, getWidth() / 2);
graphics.setLineWidth(getWidth());
if (getColor() != null)
graphics.setForegroundColor(getColor());
//用椭圆形替代矩形
//graphics.drawRectangle(tempRect);
graphics.drawOval(tempRect);
}
});
}
});
//添加控制柄
ResizableHandleKit.addHandle((GraphicalEditPart) getHost(), list, PositionConstants.EAST);
ResizableHandleKit.addHandle((GraphicalEditPart) getHost(), list, PositionConstants.SOUTH);
ResizableHandleKit.addHandle((GraphicalEditPart) getHost(), list, PositionConstants.WEST);
ResizableHandleKit.addHandle((GraphicalEditPart) getHost(), list, PositionConstants.NORTH);
return list;
}
createDragSourceFeedbackFigure()方法决定用户拖动图形时,随鼠标移动的半透明图形(即“鬼影”)的形状和颜色,因此我们覆盖这个方法以显示椭圆形的鬼影。
protected IFigure createDragSourceFeedbackFigure() {
//用椭圆替代矩形
//RectangleFigure r = new RectangleFigure();
Ellipse r = new Ellipse();
FigureUtilities.makeGhostShape(r);
r.setLineStyle(Graphics.LINE_DOT);
r.setForegroundColor(ColorConstants.white);
r.setBounds(getInitialFeedbackBounds());
addFeedback(r);
return r;
}
//用椭圆替代矩形
//RectangleFigure r = new RectangleFigure();
Ellipse r = new Ellipse();
FigureUtilities.makeGhostShape(r);
r.setLineStyle(Graphics.LINE_DOT);
r.setForegroundColor(ColorConstants.white);
r.setBounds(getInitialFeedbackBounds());
addFeedback(r);
return r;
}
经过以上这些修改,可以看到选择框和鬼影都是椭圆的了,如图2所示。
转自:http://www.cnblogs.com/bjzhanghao/archive/2006/06/26/436455.html
0 0
- Eclipse的非矩形图元
- GEF常见问题4:非矩形图元
- 非正规矩形的按钮
- 如何创建非矩形的Windows窗体?
- 乘法逆元(对于非质数的乘法逆元)
- OpenGL ES 02 – 绘制图元 2 - 矩形
- chapter2_2 非矩形窗口
- c#非矩形窗体
- 创建非矩形窗口
- 非矩形窗体
- 创建一个非矩形按钮
- C#创建非矩形窗体
- winform创建非矩形窗体
- hdu1010矩形图的搜索
- 非模版友元
- 非成员的友元函数++++C++成员初始化列表
- 模板类的非模板友元函数
- 模板类的非约束模板友元函数
- Android开发之AlertDialog警告提示框删除与取消 详解代码
- 查询省市区,转成 json 表
- 组合索引
- shell基本语法及应用
- Xcode常用的插件
- Eclipse的非矩形图元
- dagger2 让你爱不释手:终结篇
- 杭电Problem1878 欧拉回路 并查集 + 欧拉回路
- Simple way to implement caching in ASP.NET Web API
- 图的遍历
- JAVA 调用Redis接口
- GEF中的锚点Anchor
- 计算几何学习笔记之多边形
- FTP Install