Draw2D实例速成学习2

[img]/upload/attachment/120249/7813950c-ba7c-3d6b-b693-29b163fa3f17.gif[/img]
[img]/upload/attachment/120247/96356acc-2eb7-3e24-bbb7-5e426b63e93e.gif[/img]
关键代码在
[color=green]//构建一个可用的图形画布[/color]
setFigureCanvas(new FigureCanvas(shell));
[color=green]//通过子类实现抽象方法getContents,设置可视图形[/color]
getFigureCanvas().setContents(contents = getContents());
//ContentsTrackXXX方法缺省设置为false,当改变图形大小时,追踪其高度与宽度
getFigureCanvas().getViewport().setContentsTracksHeight(true);
getFigureCanvas().getViewport().setContentsTracksWidth(true);
getFigureCanvas().setLayoutData(new GridData(GridData.FILL_BOTH));
//通过重写该方法实现与shell的关联操作
hookShell();
sizeShell();
____________________使用abstractExample________________________________
/**
* 重写AbstractExample#getContents()
* org.eclipse.draw2d.FlowLayout
* 在container中加入多种shape,并在container上使用FlowLayout布局
*/
protected IFigure getContents() {
Figure container = new Figure();
container.setBorder(new LineBorder());
container.setLayoutManager(layout = new FlowLayout());

Shape shape;
shape = new Ellipse();
shape.setBackgroundColor(ColorConstants.blue);
shape.setSize(60,40);
container.add(shape);

shape = new RectangleFigure();
shape.setBackgroundColor(ColorConstants.red);
shape.setSize(30,70);
container.add(shape);

shape = new RoundedRectangle();
shape.setBackgroundColor(ColorConstants.yellow);
shape.setSize(90,30);
container.add(shape);

shape = new RectangleFigure();
shape.setBackgroundColor(ColorConstants.gray);
shape.setSize(50,80);
container.add(shape);

shape = new Ellipse();
shape.setBackgroundColor(ColorConstants.green);
shape.setSize(50,50);
container.add(shape);

shape = new Triangle();
shape.setBackgroundColor(ColorConstants.black);
shape.setSize(50,50);
container.add(shape);

shape = new RoundedRectangle();
shape.setBackgroundColor(ColorConstants.cyan);
shape.setSize(50,50);
container.add(shape);

shape = new RectangleFigure();
shape.setBackgroundColor(ColorConstants.darkGreen);
shape.setSize(50,50);
container.add(shape);

shape = new Triangle();
shape.setBackgroundColor(ColorConstants.orange);
shape.setSize(50,50);
container.add(shape);

shape = new Ellipse();
shape.setBackgroundColor(ColorConstants.red);
shape.setSize(50,50);
container.add(shape);

shape = new RoundedRectangle();
shape.setBackgroundColor(ColorConstants.yellow);
shape.setSize(50,50);
container.add(shape);

return container;
}


/**
* @see org.eclipse.draw2d.examples.AbstractExample#hookShell()
*/
protected void hookShell() {
Composite composite = new Composite(shell, 0);
composite.setLayoutData(new GridData(GridData.FILL_VERTICAL));
composite.setLayout(new GridLayout());
final Button horizontal = new Button(composite, SWT.CHECK);
horizontal.setText("Horizontal");
horizontal.setSelection(true);
horizontal.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
layout.setHorizontal(!layout.isHorizontal());
contents.revalidate();
shell.layout(true);
}
});
{
Group major = new Group(composite, 0);
major.setLayout(new FillLayout(SWT.VERTICAL));
major.setText("Major Axis");

Button left = new Button(major, SWT.RADIO);
left.setText("Top/Left");
left.setSelection(true);
left.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
layout.setMajorAlignment(FlowLayout.ALIGN_LEFTTOP);
contents.revalidate();
}
});

Button center = new Button(major, SWT.RADIO);
center.setText("Middle/Center");
center.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
layout.setMajorAlignment(FlowLayout.ALIGN_CENTER);
contents.revalidate();
}
});

Button right = new Button(major, SWT.RADIO);
right.setText("Buttom/Right");
right.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
layout.setMajorAlignment(FlowLayout.ALIGN_RIGHTBOTTOM);
contents.revalidate();
}
});

final Scale spacing = new Scale(major, 0);
spacing.setMinimum(0);
spacing.setMaximum(20);
spacing.setSelection(5);
spacing.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
layout.setMajorSpacing(spacing.getSelection());
contents.revalidate();
}
});
Label spacingLabel = new Label(major, SWT.CENTER);
spacingLabel.setText("Spacing");
}

{

Group minor = new Group(composite, 0);
minor.setLayout(new FillLayout(SWT.VERTICAL));
minor.setText("minor axis");

Button left = new Button(minor, SWT.RADIO);
left.setText("Top/Left");
left.setSelection(true);
left.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
layout.setMinorAlignment(FlowLayout.ALIGN_LEFTTOP);
contents.revalidate();
}
});

Button center = new Button(minor, SWT.RADIO);
center.setText("Middle/Center");
center.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
layout.setMinorAlignment(FlowLayout.ALIGN_CENTER);
contents.revalidate();
}
});

Button right = new Button(minor, SWT.RADIO);
right.setText("Buttom/Right");
right.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
layout.setMinorAlignment(FlowLayout.ALIGN_RIGHTBOTTOM);
contents.revalidate();
}
});

final Scale spacing = new Scale(minor, 0);
spacing.setMinimum(0);
spacing.setMaximum(20);
spacing.setSelection(5);
spacing.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
layout.setMinorSpacing(spacing.getSelection());
contents.revalidate();
}
});
Label spacingLabel = new Label(minor, SWT.CENTER);
spacingLabel.setText("Spacing");
}
}
结果

[img]/upload/attachment/120259/b190a115-d70f-307a-a6ab-5474c69ed075.gif[/img]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值