View RSS Feed

My Java Tips

SWT Canvas

Rate this Entry
by , 11-10-2011 at 05:47 PM (1525 Views)
Canvas provide a surface for drawing arbitrary graphics. It supports no styles and events. This is a subclass of class Composite.


This class may be subclassed by custom control implementors who are building controls that are not constructed from aggregates of other controls. That is, they are either painted using SWT graphics calls or are handled by native methods.

The following are the important constructors of Canvas class.

Canvas(Composite parent, int style)
Constructs a new instance of this class given its parent and a style value describing its behavior and appearance.

The following are the important methods of Canvas class.

Caret getCaret()
Returns the caret. The caret for the control is automatically hidden and shown when the control is painted or resized, when focus is gained or lost and when an the control is scrolled. To avoid drawing on top of the caret, the programmer must hide and show the caret when drawing in the window any other time.

void scroll(int destX, int destY, int x, int y, int width, int height, boolean all)
Scrolls a rectangular area of the receiver by first copying the source area to the destination and then causing the area of the source which is not covered by the destination to be repainted. Children that intersect the rectangle are optionally moved during the operation. In addition, outstanding paint events are flushed before the source area is copied to ensure that the contents of the canvas are drawn correctly.

void setCaret(Caret caret)
Sets the receiver's caret. The caret for the control is automatically hidden and shown when the control is painted or resized, when focus is gained or lost and when an the control is scrolled. To avoid drawing on top of the caret, the programmer must hide and show the caret when drawing in the window any other time.

void setFont(Font font)
Sets the font that the receiver will use to paint textual information to the font specified by the argument, or to the default font for that kind of control if the argument is null.

Example

The example below paints a circle in canvas. It uses function drawOval() to draw circle.

Java Code:
import org.eclipse.swt.graphics.*;
import org.eclipse.swt.widgets.*;
import org.eclipse.swt.events.*;

public class SWTCanvas {
	public static void main(String[] args) {
		final Display display = new Display();
		final Shell shell = new Shell(display);
		shell.addPaintListener(new PaintListener() {
			public void paintControl(PaintEvent event) {
				Rectangle rect = shell.getClientArea();
				event.gc.drawOval(0, 0, rect.width - 1, rect.height - 1);
			}
		});
		shell.setBounds(10, 10, 200, 200);
		shell.open();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
		display.dispose();
	}

}

Submit "SWT Canvas" to Facebook Submit "SWT Canvas" to Digg Submit "SWT Canvas" to del.icio.us Submit "SWT Canvas" to StumbleUpon Submit "SWT Canvas" to Google

Tags: canvas, swt Add / Edit Tags
Categories
SWT / JFace

Comments