论坛首页 Java企业应用论坛

Draw2D实例速成学习2

浏览 3449 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (1) :: 隐藏帖 (0)
作者 正文
   发表时间:2009-06-29  


关键代码在
//构建一个可用的图形画布
setFigureCanvas(new FigureCanvas(shell));
//通过子类实现抽象方法getContents,设置可视图形
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");
}
}
结果


  • 大小: 18.6 KB
  • 大小: 6.3 KB
  • 大小: 19.2 KB
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics