List of usage examples for org.eclipse.swt.widgets Canvas Canvas
public Canvas(Composite parent, int style)
From source file:SWTTest.java
public static void main(String[] args) { Display display = new Display(); Shell shell = new Shell(display); shell.setSize(350, 350);/*from w w w . j a va2s.c o m*/ shell.setLayout(new GridLayout()); Canvas canvas = new Canvas(shell, SWT.NO_BACKGROUND); GridData data = new GridData(GridData.FILL_BOTH); canvas.setLayoutData(data); final Graphics2DRenderer renderer = new Graphics2DRenderer(); canvas.addPaintListener(new PaintListener() { public void paintControl(PaintEvent e) { Point controlSize = ((Control) e.getSource()).getSize(); GC gc = e.gc; // gets the SWT graphics context from the event renderer.prepareRendering(gc); // prepares the Graphics2D // renderer // gets the Graphics2D context and switch on the antialiasing Graphics2D g2d = renderer.getGraphics2D(); g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON); // paints the background with a color gradient g2d.setPaint(new GradientPaint(0.0f, 0.0f, java.awt.Color.yellow, (float) controlSize.x, (float) controlSize.y, java.awt.Color.white)); g2d.fillRect(0, 0, controlSize.x, controlSize.y); // draws rotated text g2d.setFont(new java.awt.Font("SansSerif", java.awt.Font.BOLD, 16)); g2d.setColor(java.awt.Color.blue); g2d.translate(controlSize.x / 2, controlSize.y / 2); int nbOfSlices = 18; for (int i = 0; i < nbOfSlices; i++) { g2d.drawString("Angle = " + (i * 360 / nbOfSlices) + "\u00B0", 30, 0); g2d.rotate(-2 * Math.PI / nbOfSlices); } // now that we are done with Java2D, renders Graphics2D // operation // on the SWT graphics context renderer.render(gc); // now we can continue with pure SWT paint operations gc.drawOval(0, 0, controlSize.x, controlSize.y); } }); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } display.dispose(); renderer.dispose(); System.exit(0); }
From source file:org.eclipse.swt.examples.accessibility.AccessibleActionExample.java
public static void main(String[] args) { Display display = new Display(); Shell shell = new Shell(display); shell.setLayout(new FillLayout()); shell.setText("Accessible Action Example"); Button button = new Button(shell, SWT.PUSH); button.setText("Button"); final Canvas customButton = new Canvas(shell, SWT.NONE) { @Override//from www . j a va 2 s . co m public Point computeSize(int wHint, int hHint, boolean changed) { GC gc = new GC(this); Point point = gc.stringExtent(buttonText); gc.dispose(); point.x += MARGIN; point.y += MARGIN; return point; } }; customButton.addPaintListener(e -> { Rectangle clientArea = customButton.getClientArea(); Point stringExtent = e.gc.stringExtent(buttonText); int x = clientArea.x + (clientArea.width - stringExtent.x) / 2; int y = clientArea.y + (clientArea.height - stringExtent.y) / 2; e.gc.drawString(buttonText, x, y); }); customButton.addMouseListener(MouseListener.mouseDownAdapter(e -> { int actionIndex = (e.button == 1) ? 0 : 1; customButtonAction(actionIndex); })); customButton.addKeyListener(new KeyAdapter() { @Override public void keyPressed(KeyEvent e) { int modifierKeys = e.stateMask & SWT.MODIFIER_MASK; if (modifierKeys == SWT.CTRL || modifierKeys == 0) { if (e.character == '1') customButtonAction(0); else if (e.character == '2') customButtonAction(1); } } }); Accessible accessible = customButton.getAccessible(); accessible.addAccessibleListener(new AccessibleAdapter() { @Override public void getName(AccessibleEvent e) { e.result = buttonText; } @Override public void getKeyboardShortcut(AccessibleEvent e) { e.result = "CTRL+1"; // default action is 'action 1' } }); accessible.addAccessibleControlListener(new AccessibleControlAdapter() { @Override public void getRole(AccessibleControlEvent e) { e.detail = ACC.ROLE_PUSHBUTTON; } }); accessible.addAccessibleActionListener(new AccessibleActionAdapter() { @Override public void getActionCount(AccessibleActionEvent e) { e.count = 2; } @Override public void getName(AccessibleActionEvent e) { if (0 <= e.index && e.index <= 1) { if (e.localized) { e.result = AccessibleActionExample.getResourceString("action" + e.index); } else { e.result = "Action" + e.index; //$NON-NLS-1$ } } } @Override public void getDescription(AccessibleActionEvent e) { if (0 <= e.index && e.index <= 1) { e.result = AccessibleActionExample.getResourceString("action" + e.index + "description"); } } @Override public void doAction(AccessibleActionEvent e) { if (0 <= e.index && e.index <= 1) { customButtonAction(e.index); e.result = ACC.OK; } } @Override public void getKeyBinding(AccessibleActionEvent e) { switch (e.index) { case 0: e.result = "1;CTRL+1"; break; case 1: e.result = "2;CTRL+2"; break; default: e.result = null; } } }); shell.pack(); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } display.dispose(); }
From source file:org.eclipse.swt.snippets.Snippet352.java
public static void main(String[] args) { final Display display = new Display(); final Shell shell = new Shell(display); shell.setText("Snippet 352"); shell.setLayout(new FillLayout(SWT.HORIZONTAL)); shell.setText("Touch demonstration"); TouchListener tl = e -> {//from w w w . j a va 2 s .co m Touch touches[] = e.touches; for (int i = 0; i < touches.length; i++) { Touch currTouch = touches[i]; if ((currTouch.state & (SWT.TOUCHSTATE_UP)) != 0) { touchLocations.remove(currTouch.id); } else { CircleInfo info = touchLocations.get(currTouch.id); Point newPoint = Display.getCurrent().map(null, (Control) e.widget, new Point(currTouch.x, currTouch.y)); if (info == null) { info = new CircleInfo(newPoint, display.getSystemColor((colorIndex + 2) % PAINTABLE_COLORS)); colorIndex++; } info.center = newPoint; touchLocations.put(currTouch.id, info); } } Control c = (Control) e.widget; c.redraw(); }; PaintListener pl = e -> { for (CircleInfo ci : touchLocations.values()) { e.gc.setBackground(ci.color); e.gc.fillOval(ci.center.x - CIRCLE_RADIUS, ci.center.y - CIRCLE_RADIUS, CIRCLE_RADIUS * 2, CIRCLE_RADIUS * 2); } }; Canvas c = new Canvas(shell, SWT.NONE); c.setTouchEnabled(true); c.setSize(800, 800); c.addTouchListener(tl); c.addPaintListener(pl); shell.setSize(800, 800); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } display.dispose(); }
From source file:org.eclipse.swt.snippets.Snippet367.java
public static void main(String[] args) { final ImageFileNameProvider filenameProvider = zoom -> { switch (zoom) { case 100: return IMAGE_PATH_100; case 150: return IMAGE_PATH_150; case 200: return IMAGE_PATH_200; default:/*from www . j av a 2 s. c o m*/ return null; } }; final ImageDataProvider imageDataProvider = zoom -> { switch (zoom) { case 100: return new ImageData(IMAGE_PATH_100); case 150: return new ImageData(IMAGE_PATH_150); case 200: return new ImageData(IMAGE_PATH_200); default: return null; } }; final Display display = new Display(); final Shell shell = new Shell(display); shell.setText("Snippet367"); shell.setLayout(new GridLayout(3, false)); Menu menuBar = new Menu(shell, SWT.BAR); shell.setMenuBar(menuBar); MenuItem fileItem = new MenuItem(menuBar, SWT.CASCADE); fileItem.setText("&File"); Menu fileMenu = new Menu(menuBar); fileItem.setMenu(fileMenu); MenuItem exitItem = new MenuItem(fileMenu, SWT.PUSH); exitItem.setText("&Exit"); exitItem.addListener(SWT.Selection, e -> shell.close()); new Label(shell, SWT.NONE).setText(IMAGE_200 + ":"); new Label(shell, SWT.NONE).setImage(new Image(display, IMAGE_PATH_200)); new Button(shell, SWT.PUSH).setImage(new Image(display, IMAGE_PATH_200)); new Label(shell, SWT.NONE).setText(IMAGE_150 + ":"); new Label(shell, SWT.NONE).setImage(new Image(display, IMAGE_PATH_150)); new Button(shell, SWT.NONE).setImage(new Image(display, IMAGE_PATH_150)); new Label(shell, SWT.NONE).setText(IMAGE_100 + ":"); new Label(shell, SWT.NONE).setImage(new Image(display, IMAGE_PATH_100)); new Button(shell, SWT.NONE).setImage(new Image(display, IMAGE_PATH_100)); createSeparator(shell); new Label(shell, SWT.NONE).setText("ImageFileNameProvider:"); new Label(shell, SWT.NONE).setImage(new Image(display, filenameProvider)); new Button(shell, SWT.NONE).setImage(new Image(display, filenameProvider)); new Label(shell, SWT.NONE).setText("ImageDataProvider:"); new Label(shell, SWT.NONE).setImage(new Image(display, imageDataProvider)); new Button(shell, SWT.NONE).setImage(new Image(display, imageDataProvider)); createSeparator(shell); new Label(shell, SWT.NONE).setText("1. Canvas\n(PaintListener)"); final Point size = new Point(550, 40); final Canvas canvas = new Canvas(shell, SWT.NONE); canvas.addPaintListener(e -> { Point size1 = canvas.getSize(); paintImage(e.gc, size1); }); GridData gridData = new GridData(size.x, size.y); gridData.horizontalSpan = 2; canvas.setLayoutData(gridData); createSeparator(shell); new Label(shell, SWT.NONE).setText("2. Painted image\n (default resolution)"); Image image = new Image(display, size.x, size.y); GC gc = new GC(image); try { paintImage(gc, size); } finally { gc.dispose(); } Label imageLabel = new Label(shell, SWT.NONE); imageLabel.setImage(image); imageLabel.setLayoutData(new GridData(SWT.BEGINNING, SWT.BEGINNING, false, false, 2, 1)); createSeparator(shell); new Label(shell, SWT.NONE).setText("3. Painted image\n(multi-res, unzoomed paint)"); imageLabel = new Label(shell, SWT.NONE); imageLabel.setImage(new Image(display, (ImageDataProvider) zoom -> { Image temp = new Image(display, size.x * zoom / 100, size.y * zoom / 100); GC gc1 = new GC(temp); try { paintImage(gc1, size); return temp.getImageData(); } finally { gc1.dispose(); temp.dispose(); } })); imageLabel.setLayoutData(new GridData(SWT.BEGINNING, SWT.BEGINNING, false, false, 2, 1)); createSeparator(shell); new Label(shell, SWT.NONE).setText("4. Painted image\n(multi-res, zoomed paint)"); imageLabel = new Label(shell, SWT.NONE); imageLabel.setImage(new Image(display, (ImageDataProvider) zoom -> { Image temp = new Image(display, size.x * zoom / 100, size.y * zoom / 100); GC gc1 = new GC(temp); try { paintImage2(gc1, new Point(size.x * zoom / 100, size.y * zoom / 100), zoom / 100); return temp.getImageData(); } finally { gc1.dispose(); temp.dispose(); } })); imageLabel.setLayoutData(new GridData(SWT.BEGINNING, SWT.BEGINNING, false, false, 2, 1)); createSeparator(shell); new Label(shell, SWT.NONE).setText("5. 50x50 box\n(Display#getDPI(): " + display.getDPI().x + ")"); Label box = new Label(shell, SWT.NONE); box.setBackground(display.getSystemColor(SWT.COLOR_WIDGET_DARK_SHADOW)); box.setLayoutData(new GridData(50, 50)); shell.pack(); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } display.dispose(); }
From source file:org.eclipse.swt.examples.accessibility.ControlsWithLabelsExample.java
public static void main(String[] args) { display = new Display(); shell = new Shell(display); shell.setLayout(new GridLayout(4, true)); shell.setText("All Controls Test"); new Label(shell, SWT.NONE).setText("Label for Label"); label = new Label(shell, SWT.NONE); label.setText("Label"); new Label(shell, SWT.NONE).setText("Label for CLabel"); cLabel = new CLabel(shell, SWT.NONE); cLabel.setText("CLabel"); new Label(shell, SWT.NONE).setText("Label for Push Button"); buttonPush = new Button(shell, SWT.PUSH); buttonPush.setText("Push Button"); new Label(shell, SWT.NONE).setText("Label for Radio Button"); buttonRadio = new Button(shell, SWT.RADIO); buttonRadio.setText("Radio Button"); new Label(shell, SWT.NONE).setText("Label for Check Button"); buttonCheck = new Button(shell, SWT.CHECK); buttonCheck.setText("Check Button"); new Label(shell, SWT.NONE).setText("Label for Toggle Button"); buttonToggle = new Button(shell, SWT.TOGGLE); buttonToggle.setText("Toggle Button"); new Label(shell, SWT.NONE).setText("Label for Editable Combo"); combo = new Combo(shell, SWT.BORDER); for (int i = 0; i < 4; i++) { combo.add("item" + i); }/*from www. j av a2 s.c o m*/ combo.select(0); new Label(shell, SWT.NONE).setText("Label for Read-Only Combo"); combo = new Combo(shell, SWT.READ_ONLY | SWT.BORDER); for (int i = 0; i < 4; i++) { combo.add("item" + i); } combo.select(0); new Label(shell, SWT.NONE).setText("Label for CCombo"); cCombo = new CCombo(shell, SWT.BORDER); for (int i = 0; i < 5; i++) { cCombo.add("item" + i); } cCombo.select(0); new Label(shell, SWT.NONE).setText("Label for List"); list = new List(shell, SWT.SINGLE | SWT.BORDER); list.setItems("Item0", "Item1", "Item2"); new Label(shell, SWT.NONE).setText("Label for Spinner"); spinner = new Spinner(shell, SWT.BORDER); new Label(shell, SWT.NONE).setText("Label for Single-line Text"); textSingle = new Text(shell, SWT.SINGLE | SWT.BORDER); textSingle.setText("Contents of Single-line Text"); new Label(shell, SWT.NONE).setText("Label for Multi-line Text"); textMulti = new Text(shell, SWT.MULTI | SWT.BORDER); textMulti.setText("\nContents of Multi-line Text\n"); new Label(shell, SWT.NONE).setText("Label for StyledText"); styledText = new StyledText(shell, SWT.MULTI | SWT.BORDER); styledText.setText("\nContents of Multi-line StyledText\n"); new Label(shell, SWT.NONE).setText("Label for Table"); table = new Table(shell, SWT.MULTI | SWT.FULL_SELECTION | SWT.BORDER); table.setHeaderVisible(true); table.setLinesVisible(true); for (int col = 0; col < 3; col++) { TableColumn column = new TableColumn(table, SWT.NONE); column.setText("Col " + col); column.setWidth(50); } for (int row = 0; row < 3; row++) { TableItem item = new TableItem(table, SWT.NONE); item.setText(new String[] { "C0R" + row, "C1R" + row, "C2R" + row }); } new Label(shell, SWT.NONE).setText("Label for Tree"); tree = new Tree(shell, SWT.BORDER | SWT.MULTI); for (int i = 0; i < 3; i++) { TreeItem item = new TreeItem(tree, SWT.NONE); item.setText("Item" + i); for (int j = 0; j < 4; j++) { new TreeItem(item, SWT.NONE).setText("Item" + i + j); } } new Label(shell, SWT.NONE).setText("Label for Tree with columns"); treeTable = new Tree(shell, SWT.BORDER | SWT.MULTI); treeTable.setHeaderVisible(true); treeTable.setLinesVisible(true); for (int col = 0; col < 3; col++) { TreeColumn column = new TreeColumn(treeTable, SWT.NONE); column.setText("Col " + col); column.setWidth(50); } for (int i = 0; i < 3; i++) { TreeItem item = new TreeItem(treeTable, SWT.NONE); item.setText(new String[] { "I" + i + "C0", "I" + i + "C1", "I" + i + "C2" }); for (int j = 0; j < 4; j++) { new TreeItem(item, SWT.NONE) .setText(new String[] { "I" + i + j + "C0", "I" + i + j + "C1", "I" + i + j + "C2" }); } } new Label(shell, SWT.NONE).setText("Label for ToolBar"); toolBar = new ToolBar(shell, SWT.FLAT); for (int i = 0; i < 3; i++) { ToolItem item = new ToolItem(toolBar, SWT.PUSH); item.setText("Item" + i); item.setToolTipText("ToolItem ToolTip" + i); } new Label(shell, SWT.NONE).setText("Label for CoolBar"); coolBar = new CoolBar(shell, SWT.FLAT); for (int i = 0; i < 2; i++) { CoolItem coolItem = new CoolItem(coolBar, SWT.PUSH); ToolBar coolItemToolBar = new ToolBar(coolBar, SWT.FLAT); int toolItemWidth = 0; for (int j = 0; j < 2; j++) { ToolItem item = new ToolItem(coolItemToolBar, SWT.PUSH); item.setText("Item" + i + j); item.setToolTipText("ToolItem ToolTip" + i + j); if (item.getWidth() > toolItemWidth) toolItemWidth = item.getWidth(); } coolItem.setControl(coolItemToolBar); Point size = coolItemToolBar.computeSize(SWT.DEFAULT, SWT.DEFAULT); Point coolSize = coolItem.computeSize(size.x, size.y); coolItem.setMinimumSize(toolItemWidth, coolSize.y); coolItem.setPreferredSize(coolSize); coolItem.setSize(coolSize); } new Label(shell, SWT.NONE).setText("Label for Canvas"); canvas = new Canvas(shell, SWT.BORDER); canvas.setLayoutData(new GridData(64, 64)); canvas.addPaintListener(e -> e.gc.drawString("Canvas", 15, 25)); canvas.setCaret(new Caret(canvas, SWT.NONE)); /* Hook key listener so canvas will take focus during traversal in. */ canvas.addKeyListener(new KeyAdapter() { @Override public void keyPressed(KeyEvent e) { } @Override public void keyReleased(KeyEvent e) { } }); /* Hook traverse listener to make canvas give up focus during traversal out. */ canvas.addTraverseListener(e -> e.doit = true); new Label(shell, SWT.NONE).setText("Label for Group"); group = new Group(shell, SWT.NONE); group.setText("Group"); group.setLayout(new FillLayout()); new Text(group, SWT.SINGLE | SWT.BORDER).setText("Text in Group"); new Label(shell, SWT.NONE).setText("Label for TabFolder"); tabFolder = new TabFolder(shell, SWT.NONE); for (int i = 0; i < 3; i++) { TabItem item = new TabItem(tabFolder, SWT.NONE); item.setText("TabItem &" + i); item.setToolTipText("TabItem ToolTip" + i); Text itemText = new Text(tabFolder, SWT.SINGLE | SWT.BORDER); itemText.setText("Text for TabItem " + i); item.setControl(itemText); } new Label(shell, SWT.NONE).setText("Label for CTabFolder"); cTabFolder = new CTabFolder(shell, SWT.BORDER); for (int i = 0; i < 3; i++) { CTabItem item = new CTabItem(cTabFolder, SWT.NONE); item.setText("CTabItem &" + i); item.setToolTipText("CTabItem ToolTip" + i); Text itemText = new Text(cTabFolder, SWT.SINGLE | SWT.BORDER); itemText.setText("Text for CTabItem " + i); item.setControl(itemText); } cTabFolder.setSelection(cTabFolder.getItem(0)); new Label(shell, SWT.NONE).setText("Label for Scale"); scale = new Scale(shell, SWT.NONE); new Label(shell, SWT.NONE).setText("Label for Slider"); slider = new Slider(shell, SWT.NONE); new Label(shell, SWT.NONE).setText("Label for ProgressBar"); progressBar = new ProgressBar(shell, SWT.NONE); progressBar.setSelection(50); new Label(shell, SWT.NONE).setText("Label for Sash"); sash = new Sash(shell, SWT.NONE); shell.pack(); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } display.dispose(); }
From source file:XOR.java
public XOR() { shell.setLayout(new FillLayout()); final Canvas canvas = new Canvas(shell, SWT.NULL); canvas.setBackground(display.getSystemColor(SWT.COLOR_WHITE)); canvas.addPaintListener(new PaintListener() { public void paintControl(PaintEvent e) { e.gc.setXORMode(true);//from w w w . j a v a 2 s . c o m e.gc.setBackground(display.getSystemColor(SWT.COLOR_GREEN)); e.gc.fillOval(60, 10, 100, 100); // Top e.gc.setBackground(display.getSystemColor(SWT.COLOR_RED)); e.gc.fillOval(10, 60, 120, 120); // left bottom e.gc.setBackground(display.getSystemColor(SWT.COLOR_BLUE)); e.gc.fillOval(110, 60, 100, 100); // right bottom } }); shell.setSize(300, 220); shell.open(); //textUser.forceFocus(); // Set up the event loop. while (!shell.isDisposed()) { if (!display.readAndDispatch()) { // If no more entries in event queue display.sleep(); } } display.dispose(); }
From source file:Clipping.java
public Clipping() { shell.setLayout(new FillLayout()); final Canvas canvas = new Canvas(shell, SWT.NULL); final Image image = new Image(display, "java2s.gif"); canvas.addPaintListener(new PaintListener() { public void paintControl(PaintEvent e) { Region region = new Region(); region.add(new int[] { 60, 10, 10, 100, 110, 100 }); e.gc.setClipping(region);//from ww w . j a va 2s . co m e.gc.drawImage(image, 0, 0); } }); shell.setSize(200, 140); shell.open(); //textUser.forceFocus(); // Set up the event loop. while (!shell.isDisposed()) { if (!display.readAndDispatch()) { // If no more entries in event queue display.sleep(); } } display.dispose(); }
From source file:AlphaFadeIn.java
public AlphaFadeIn() { shell.setLayout(new FillLayout()); final Canvas canvas = new Canvas(shell, SWT.NULL); ImageData imageData = new ImageData("java2s.gif"); ImageData imageData2 = imageData.scaledTo(100, 100); Image image2 = new Image(display, imageData2); byte[] alphaValues = new byte[imageData.height * imageData.width]; for (int j = 0; j < imageData.height; j++) { for (int i = 0; i < imageData.width; i++) { alphaValues[j * imageData.width + i] = (byte) (255 - 255 * i / imageData.width); }//from w ww . ja v a 2s . c o m } imageData.alphaData = alphaValues; final Image image = new Image(display, imageData); canvas.addPaintListener(new PaintListener() { public void paintControl(PaintEvent e) { e.gc.drawImage(image, 10, 10); } }); shell.setSize(200, 100); shell.open(); //textUser.forceFocus(); // Set up the event loop. while (!shell.isDisposed()) { if (!display.readAndDispatch()) { // If no more entries in event queue display.sleep(); } } display.dispose(); }
From source file:DrawTextDemo.java
public DrawTextDemo() { shell.setLayout(new FillLayout()); final Canvas canvas = new Canvas(shell, SWT.NO_BACKGROUND); final Image image = new Image(display, "java2s.gif"); canvas.addPaintListener(new PaintListener() { public void paintControl(PaintEvent e) { Rectangle size = image.getBounds(); // Draws the background image. e.gc.drawImage(image, 0, 0, size.width, size.height, 0, 0, canvas.getSize().x, canvas.getSize().y); Font font = new Font(display, "Tahoma", 18, SWT.BOLD); e.gc.setFont(font);//from ww w . j a v a 2s . c om e.gc.setForeground(display.getSystemColor(SWT.COLOR_WHITE)); e.gc.setBackground(display.getSystemColor(SWT.COLOR_BLUE)); String english = "SWT rocks!"; String chinese = "\u4e2d\u6587\u6c49\u5b57\u6d4b\u8bd5"; e.gc.drawString(english, 10, 10); e.gc.drawString(chinese, 10, 80, true); String text = "Text to be drawn in the center"; Point textSize = e.gc.textExtent(text); e.gc.drawText(text, (canvas.getSize().x - textSize.x) / 2, (canvas.getSize().y - textSize.y) / 2); font.dispose(); } }); shell.setSize(300, 150); shell.open(); //textUser.forceFocus(); // Set up the event loop. while (!shell.isDisposed()) { if (!display.readAndDispatch()) { // If no more entries in event queue display.sleep(); } } display.dispose(); }
From source file:LineExample.java
/** * Creates the main window's contents/*from w w w.j a v a2s . c om*/ * * @param shell the main window */ private void createContents(Shell shell) { shell.setLayout(new FillLayout()); // Create a canvas to draw on Canvas canvas = new Canvas(shell, SWT.NONE); // Add a handler to do the drawing canvas.addPaintListener(new PaintListener() { public void paintControl(PaintEvent e) { // Get the canvas and its size Canvas canvas = (Canvas) e.widget; int maxX = canvas.getSize().x; int maxY = canvas.getSize().y; // Calculate the middle int halfX = (int) maxX / 2; int halfY = (int) maxY / 2; // Set the drawing color to blue e.gc.setForeground(e.display.getSystemColor(SWT.COLOR_BLUE)); // Set the width of the lines to draw e.gc.setLineWidth(10); // Draw a vertical line halfway across the canvas e.gc.drawLine(halfX, 0, halfX, maxY); // Draw a horizontal line halfway down the canvas e.gc.drawLine(0, halfY, maxX, halfY); } }); }