下面列出了org.eclipse.swt.widgets.Layout#org.eclipse.swt.widgets.Canvas 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Test
public void testAll() throws Exception {
shell = new Shell();
shell.open();
shell.setLayout(new GridLayout(1, false));
final Canvas canvas = new Canvas(shell, SWT.None);
canvas.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
text = new Text(shell, SWT.READ_ONLY);
text.setFont(XYGraphMediaFactory.getInstance().getFont("default", 18,
SWT.BOLD));
text.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false));
final LightweightSystem lws = new LightweightSystem(canvas);
lws.setContents(getTestBench());
shell.setSize(800, 500);
testGetBeanInfo();
testWidget();
}
void mouseDown( MouseEvent e )
{
if ( e.widget instanceof Canvas )
{
if ( e.widget.getData( ) != null )
{
int markerIndex = ( (Integer) e.widget.getData( ) ).intValue( );
switchMarkerType( markerIndex );
if ( !this.isDisposed( ) && !this.getShell( ).isDisposed( ) )
{
this.getShell( ).close( );
}
}
}
}
/**
* Show the dark panel
*/
public void show() {
if (parent.isDisposed()) {
SWT.error(SWT.ERROR_WIDGET_DISPOSED);
}
panel = new Shell(parent, SWT.APPLICATION_MODAL | SWT.NO_TRIM);
panel.setLayout(new FillLayout());
panel.setAlpha(alpha);
panel.addListener(SWT.KeyUp, event -> {
event.doit = false;
});
canvas = new Canvas(panel, SWT.NO_BACKGROUND | SWT.DOUBLE_BUFFERED);
canvas.addPaintListener(event -> {
paintCanvas(event);
});
panel.setBounds(panel.getDisplay().map(parent, null, parent.getClientArea()));
panel.open();
}
public TimelineComposite(Composite parent, int style) {
super(parent, style);
fResourceManager = new LocalResourceManager(JFaceResources.getResources(), this);
final FillLayout layout = new FillLayout();
layout.marginHeight = 10;
layout.marginWidth = 10;
setLayout(layout);
setBackground(ColorConstants.black);
final Canvas canvas = new Canvas(this, SWT.DOUBLE_BUFFERED);
canvas.setBackground(ColorConstants.black);
final LightweightSystem lightWeightSystem = new LightweightSystem(canvas);
fRootFigure = new RootFigure(fResourceManager);
fRootFigure.setFont(parent.getFont());
lightWeightSystem.setContents(fRootFigure);
// draw2d does not directly support mouseWheelEvents, so register on canvas
canvas.addMouseWheelListener(new TimelineScaler(this));
}
/**
* Handle paint requests for vertical ruler.
*/
protected void paintVerticalRuler(PaintEvent event) {
// FIXME - not i18n safe!!
String label = (disk.getBitmapLabels()[0] + "s").toUpperCase(); //$NON-NLS-1$
if (disk.getBitmapLabels().length == 2) {
label = (disk.getBitmapLabels()[1] + "s").toUpperCase(); //$NON-NLS-1$
}
StringBuffer buf = new StringBuffer();
for (int i=0; i<label.length(); i++) {
if (i>0) buf.append("\n"); //$NON-NLS-1$
buf.append(label.charAt(i));
}
label = buf.toString();
Canvas canvas = (Canvas) event.widget;
Rectangle area = canvas.getClientArea();
event.gc.drawLine(area.x + area.width/2, area.y, area.x + area.width/2, area.y + area.height);
Point size = event.gc.textExtent(label);
event.gc.drawText(label, area.x + area.width/2 - size.x/2, area.y + area.height/2 - size.y/2);
}
/**
* Paint a block map.
*/
private void paintBlockMap(PaintEvent event) {
Canvas canvas = (Canvas) event.widget;
Rectangle area = canvas.getClientArea();
double blocks = disk.getBitmapLength();
double width = area.width;
double height = area.height;
double factor = Math.sqrt(blocks / (width * height));
int xdim = (int) (width * factor + 0.5);
int ydim = (int) (height * factor + 0.5);
if (xdim * ydim < blocks) {
xdim++;
}
if (xdim * ydim < blocks) {
ydim++;
}
paintDiskMap(xdim, ydim, event);
}
@Override
public void initialize(Canvas canvas) {
super.initialize(canvas);
drawCanvas.addPaintListener(new PaintListener() {
@Override
public void paintControl(PaintEvent e) {
if (bufferImage != null && !bufferImage.isDisposed()) {
Rectangle bounds = bufferImage.getBounds();
if (bounds.width >= ( e.width + e.x ) && bounds.height >= ( e.height + e.y )) {
e.gc.drawImage(bufferImage, e.x, e.y, e.width, e.height, e.x, e.y,
e.width, e.height);
}
}
}
});
drawCanvas.addListener(SWT.Resize, new Listener() {
@Override
public void handleEvent(Event event) {
drawChart(true);
}
});
}
public GraphicalViewer(Composite parent) {
canvas = new Canvas(parent, SWT.NONE);
parent.addListener(SWT.Resize, new Listener() {
public void handleEvent(Event event) {
canvas.setBounds(canvas.getParent().getClientArea());
requestImageRedraw();
}
});
canvas.addPaintListener(new PaintListener() {
public void paintControl(PaintEvent e) {
redrawImageIfRequested();
GC gc = e.gc;
paintCanvas(gc);
}
});
}
private void createAnnotationInformation(Composite parent, final Annotation annotation) {
Composite composite= new Composite(parent, SWT.NONE);
composite.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false));
GridLayout layout= new GridLayout(2, false);
layout.marginHeight= 2;
layout.marginWidth= 2;
layout.horizontalSpacing= 0;
composite.setLayout(layout);
final Canvas canvas= new Canvas(composite, SWT.NO_FOCUS);
GridData gridData= new GridData(SWT.BEGINNING, SWT.BEGINNING, false, false);
gridData.widthHint= 17;
gridData.heightHint= 16;
canvas.setLayoutData(gridData);
canvas.addPaintListener(new PaintListener() {
public void paintControl(PaintEvent e) {
e.gc.setFont(null);
fMarkerAnnotationAccess.paint(annotation, e.gc, canvas, new Rectangle(0, 0, 16, 16));
}
});
StyledText text= new StyledText(composite, SWT.MULTI | SWT.WRAP | SWT.READ_ONLY);
GridData data= new GridData(SWT.FILL, SWT.FILL, true, true);
text.setLayoutData(data);
text.setText(annotation.getText());
}
@Override
protected AbstractControllableShape createControllableShape2(
Canvas canvas) {
return new AbstractControllableShape(canvas) {
@Override
public void createControlPoints() {
addControlPoint(new Point(100, 100));
addControlPoint(new Point(300, 100));
addControlPoint(new Point(300, 300));
}
@Override
public QuadraticCurve createGeometry() {
return new QuadraticCurve(getControlPoints());
}
@Override
public void drawShape(GC gc) {
QuadraticCurve c = createGeometry();
gc.drawPath(
new org.eclipse.swt.graphics.Path(Display.getCurrent(),
Geometry2SWT.toSWTPathData(c.toPath())));
}
};
}
@Override
protected AbstractControllableShape createControllableShape2(
Canvas canvas) {
return new AbstractControllableShape(canvas) {
@Override
public void createControlPoints() {
addControlPoint(new Point(100, 100));
addControlPoint(new Point(300, 300));
}
@Override
public Line createGeometry() {
Point[] points = getControlPoints();
return new Line(points[0], points[1]);
}
@Override
public void drawShape(GC gc) {
Line line = createGeometry();
gc.drawPolyline(Geometry2SWT.toSWTPointArray(line));
}
};
}
public void mouseExit(MouseEvent e) {
Item item= (Item) ((Widget) e.getSource()).getData();
if (item != null)
item.deselect();
// if the event lies outside the entire popup, dispose
org.eclipse.swt.graphics.Region region= fShell.getRegion();
Canvas can= (Canvas) e.getSource();
Point p= can.toDisplay(e.x, e.y);
if (region == null) {
Rectangle bounds= fShell.getBounds();
// p= fShell.toControl(p);
if (!bounds.contains(p))
dispose();
} else {
p= fShell.toControl(p);
if (!region.contains(p))
dispose();
}
}
@Override
protected AbstractControllableShape createControllableShape2(
Canvas canvas) {
return new AbstractControllableShape(canvas) {
@Override
public void createControlPoints() {
addControlPoint(new Point(100, 150));
addControlPoint(new Point(400, 200));
addControlPoint(new Point(300, 400));
addControlPoint(new Point(550, 300));
}
@Override
public CubicCurve createGeometry() {
return new CubicCurve(getControlPoints());
}
@Override
public void drawShape(GC gc) {
CubicCurve c = createGeometry();
gc.drawPath(
new org.eclipse.swt.graphics.Path(Display.getCurrent(),
Geometry2SWT.toSWTPathData(c.toPath())));
}
};
}
@Override
protected AbstractControllableShape createControllableShape2(
Canvas canvas) {
return new AbstractControllableShape(canvas) {
@Override
public void createControlPoints() {
addControlPoint(new Point(100, 150));
addControlPoint(new Point(400, 200));
addControlPoint(new Point(550, 300));
}
@Override
public QuadraticCurve createGeometry() {
return new QuadraticCurve(getControlPoints());
}
@Override
public void drawShape(GC gc) {
QuadraticCurve c = createGeometry();
gc.drawPath(
new org.eclipse.swt.graphics.Path(Display.getCurrent(),
Geometry2SWT.toSWTPathData(c.toPath())));
}
};
}
@Override
protected AbstractControllableShape createControllableShape2(
Canvas canvas) {
return new AbstractControllableShape(canvas) {
@Override
public void createControlPoints() {
addControlPoint(new Point(100, 100));
addControlPoint(new Point(300, 300));
}
@Override
public Line createGeometry() {
Point[] points = getControlPoints();
return new Line(points[0], points[1]);
}
@Override
public void drawShape(GC gc) {
Line line = createGeometry();
gc.drawPolyline(Geometry2SWT.toSWTPointArray(line));
}
};
}
@Override
protected AbstractControllableShape createControllableShape1(
Canvas canvas) {
return new AbstractControllableShape(canvas) {
@Override
public void createControlPoints() {
// the ellipse does not have any control points
}
@Override
public Ellipse createGeometry() {
double w5 = getCanvas().getClientArea().width / 5;
double h5 = getCanvas().getClientArea().height / 5;
return new Ellipse(w5, h5, 3 * w5, 3 * h5);
}
@Override
public void drawShape(GC gc) {
Ellipse ellipse = createGeometry();
gc.drawOval((int) ellipse.getX(), (int) ellipse.getY(),
(int) ellipse.getWidth(), (int) ellipse.getHeight());
}
};
}
public MapView(Composite parent) {
this.canvas = new Canvas(parent, SWT.NONE);
this.white = canvas.getDisplay().getSystemColor(SWT.COLOR_WHITE);
canvas.addPaintListener(e -> render(e.gc));
// add mouse listeners
canvas.addMouseWheelListener(e -> {
translation.updateCenter(e.x, e.y, zoom);
if (e.count > 0) {
zoomIn();
} else {
zoomOut();
}
});
canvas.addMouseListener(new DragSupport());
}
/**
* Does not paint hidden annotations. Annotations are hidden when they
* only span one line.
*
* @see ProjectionAnnotation#paint(org.eclipse.swt.graphics.GC,
* org.eclipse.swt.widgets.Canvas,
* org.eclipse.swt.graphics.Rectangle)
*/
@Override
public void paint(GC gc, Canvas canvas, Rectangle rectangle) {
/* workaround for BUG85874 */
/*
* only need to check annotations that are expanded because hidden
* annotations should never have been given the chance to collapse.
*/
if (!isCollapsed()) {
// working with rectangle, so line height
FontMetrics metrics = gc.getFontMetrics();
if (metrics != null) {
// do not draw annotations that only span one line and
// mark them as not visible
if ((rectangle.height / metrics.getHeight()) <= 1) {
visible = false;
return;
}
}
}
visible = true;
super.paint(gc, canvas, rectangle);
}
public long create(GLCanvas canvas, GLData attribs, GLData effective) {
Canvas dummycanvas = new Canvas(canvas.getParent(), checkStyle(canvas.getParent(), canvas.getStyle()));
long context = 0L;
MemoryStack stack = MemoryStack.stackGet(); int ptr = stack.getPointer();
try {
context = create(canvas.handle, dummycanvas.handle, attribs, effective);
} catch (SWTException e) {
stack.setPointer(ptr);
SWT.error(SWT.ERROR_UNSUPPORTED_DEPTH, e);
}
final long finalContext = context;
dummycanvas.dispose();
Listener listener = new Listener() {
public void handleEvent(Event event) {
switch (event.type) {
case SWT.Dispose:
deleteContext(canvas, finalContext);
break;
}
}
};
canvas.addListener(SWT.Dispose, listener);
return context;
}
/**
* Find the canvas of TransGraph. For some reason it's not exposed.
*
* @param composite
* @return Canvas of null if it can't be found.
*/
private Canvas findCanvas( Composite composite ) {
for ( Control child : composite.getChildren() ) {
if ( child instanceof Canvas ) {
return (Canvas) child;
}
if ( child instanceof Composite ) {
Canvas look = findCanvas( (Composite) child );
if ( look != null ) {
return look;
}
}
}
return null;
}
private void createPreviewArea( Composite parent )
{
Composite cmpPreview = ChartUIUtil.createCompositeWrapper( parent );
{
GridData gridData = new GridData( GridData.FILL_BOTH );
gridData.widthHint = CENTER_WIDTH_HINT;
gridData.heightHint = 200;
cmpPreview.setLayoutData( gridData );
}
Label label = new Label( cmpPreview, SWT.NONE );
{
label.setFont( JFaceResources.getBannerFont( ) );
label.setText( Messages.getString( "TaskSelectData.Label.ChartPreview" ) ); //$NON-NLS-1$
}
previewCanvas = new Canvas( cmpPreview, SWT.BORDER );
{
GridData gd = new GridData( GridData.FILL_BOTH );
previewCanvas.setLayoutData( gd );
previewCanvas.setBackground( Display.getDefault( )
.getSystemColor( SWT.COLOR_WIDGET_BACKGROUND ) );
}
}
public ColorPicker(Composite parent) {
super(new Canvas(parent, SWT.DOUBLE_BUFFERED));
setY(128);
wrapped.addListener(SWT.Paint, e -> {
Point size = wrapped.getSize();
Image img = getMapFor(e.display);
e.gc.drawImage(img, 0, 0, _256, _256, 0, 0, size.x, size.y);
});
mouseDown = SwtRx.addListener(wrapped, SWT.MouseDown).map(this::posToColor);
mouseMove = SwtRx.addListener(wrapped, SWT.MouseMove).map(this::posToColor);
}
protected void adjustItemNumber() {
if (fComposite == null)
return;
Control[] children= fComposite.getChildren();
int oldSize= children.length;
int newSize= fInput == null ? 0 : fInput.fAnnotations.length;
Display display= fShell.getDisplay();
// add missing items
for (int i= oldSize; i < newSize; i++) {
Canvas canvas= new Canvas(fComposite, SWT.NONE);
Object gridData= fLayouter.getLayoutData();
canvas.setLayoutData(gridData);
canvas.setBackground(display.getSystemColor(SWT.COLOR_INFO_BACKGROUND));
canvas.addPaintListener(fPaintListener);
canvas.addMouseTrackListener(fMouseTrackListener);
canvas.addMouseListener(fMouseListener);
canvas.addListener(SWT.MenuDetect, fMenuDetectListener);
canvas.addDisposeListener(fDisposeListener);
}
// dispose of exceeding resources
for (int i= oldSize; i > newSize; i--) {
Item item= (Item) children[i - 1].getData();
item.deselect();
children[i - 1].dispose();
}
}
/**
* Constructor.
* @param canvas Canvas where to paint the transitions.
* @param from From image to paint in the canvas.
* @param to To image to paint in the canvas.
* @param direction Direction of the animation effect.
* @param xitionBg Initial background of the transition paint.
*/
private TransitionPainter(Canvas canvas, final Image from, final Image to,
final double direction, final Image xitionBg) {
_canvas = canvas;
_from = from;
_to = to;
_direction = direction;
_xitionBg = xitionBg;
}
@Override
public void paint(GC gc, Canvas canvas, Rectangle bounds) {
Point canvasSize = canvas.getSize();
gc.setBackground(fillColor);
gc.setForeground(strokeColor);
gc.setLineWidth(1);
int x = ViewportAnnotation.INSET;
int y = bounds.y;
int w = canvasSize.x - 2 * ViewportAnnotation.INSET;
int h = bounds.height;
if (multipleLines) {
h += gc.getFontMetrics().getHeight();
}
if (y < 0) {
h = h + y;
y = 0;
}
if (h <= 0) {
return;
}
gc.fillRectangle(x, y, w, h);
gc.drawRectangle(x, y, w, h);
}
/**
* Build the image on top of the login box. If no image has been set, create
* a default image
*/
private void buildImage() {
final Canvas canvas = new Canvas(shell, SWT.DOUBLE_BUFFERED);
final GridData gridData = new GridData(GridData.FILL, GridData.FILL, true, false, 4, 1);
gridData.widthHint = 400;
gridData.heightHint = 60;
canvas.setLayoutData(gridData);
canvas.addPaintListener(e -> {
e.gc.drawImage(image == null ? createDefaultImage(e.width, e.height) : image, 0, 0);
});
}
/**
* Paint the canvas that holds the panel
*
* @param e {@link PaintEvent}
*/
private void paintCanvas(final PaintEvent e) {
// Paint the panel
final Rectangle clientArea = ((Canvas) e.widget).getClientArea();
final GC gc = e.gc;
gc.setBackground(panel.getDisplay().getSystemColor(SWT.COLOR_BLACK));
gc.fillRectangle(clientArea);
}
/**
* Creates the bar that displays the memory
*/
private void createBar() {
bar = new Canvas(this, SWT.NONE);
final GridData gd = new GridData(GridData.FILL, GridData.FILL, true, false);
gd.minimumWidth = 100;
gd.heightHint = 30;
bar.setLayoutData(gd);
heapMaxSize = (int) (Runtime.getRuntime().maxMemory() / (1024 * 1024));
bar.addPaintListener(e -> {
drawBar(e);
});
}
/**
* Paint a map with the given dimensions.
*/
private void paintDiskMap(int xdim, int ydim, PaintEvent event) {
Canvas canvas = (Canvas) event.widget;
Rectangle area = canvas.getClientArea();
area.width-= 2;
area.height-= 2;
int[] ypos = new int[ydim + 1];
for (int i=0; i<ydim; i++) {
ypos[i] = (i * area.height) / ydim + 1;
}
ypos[ydim] = area.height;
int[] xpos = new int[xdim + 1];
for (int i=0; i<xdim; i++) {
xpos[i] = (i * area.width) / xdim + 1;
}
xpos[xdim] = area.width;
Image image = new Image(canvas.getDisplay(), area);
GC gc = new GC(image);
int x = 0;
int y = 0;
DiskUsage usage = disk.getDiskUsage();
for (x=0; x<xdim && usage.hasNext(); x++) {
for (y=0; y<ydim && usage.hasNext(); y++) {
usage.next();
boolean free = usage.isFree();
Rectangle box = new Rectangle(xpos[x], ypos[y],
xpos[x+1]-xpos[x], ypos[y+1]-ypos[y]);
drawBox(box, gc, free ? freeFill : usedFill, black, gray);
}
}
event.gc.drawImage(image, 0, 0);
gc.dispose();
image.dispose();
}
public void paintControl(PaintEvent e) {
Canvas can= (Canvas) e.getSource();
Annotation a= ((Item) can.getData()).fAnnotation;
if (a != null) {
Rectangle rect= new Rectangle(fLayouter.getBorderWidth(), fLayouter.getBorderWidth(), fLayouter.getAnnotationSize(), fLayouter.getAnnotationSize());
if (fAnnotationAccessExtension != null)
fAnnotationAccessExtension.paint(a, e.gc, can, rect);
}
}