List of usage examples for org.eclipse.swt.widgets Canvas redraw
public void redraw()
From source file:MainClass.java
public static void main(String[] a) { final Display d = new Display(); final Shell shell = new Shell(d); shell.setSize(250, 200);/* w w w . ja v a 2s.c o m*/ shell.setLayout(new FillLayout()); Canvas drawingCanvas = new Canvas(shell, SWT.NONE); drawingCanvas.addPaintListener(new ArcExamplePaintListener()); drawingCanvas.redraw(); shell.open(); while (!shell.isDisposed()) { if (!d.readAndDispatch()) d.sleep(); } d.dispose(); }
From source file:DrawOval.java
public static void main(String[] args) { Display display = new Display(); Shell shell = new Shell(display); shell.setText("Canvas Example"); shell.setLayout(new FillLayout()); Canvas canvas = new Canvas(shell, SWT.NONE); canvas.addPaintListener(new PaintListener() { public void paintControl(PaintEvent e) { e.gc.drawOval(200, 100, 500, 400); }/* w ww.jav a2 s .c o m*/ }); canvas.redraw(); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) { display.sleep(); } } display.dispose(); }
From source file:CanvasRepaint.java
public static void main(String[] args) { Display display = new Display(); Shell shell = new Shell(display); shell.setText("Canvas Example"); shell.setLayout(new FillLayout()); Canvas canvas = new Canvas(shell, SWT.NONE); canvas.addPaintListener(new PaintListener() { public void paintControl(PaintEvent e) { e.gc.drawRoundRectangle(10, 10, 200, 200, 30, 30); }//w ww . ja v a 2 s . c om }); canvas.redraw(); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) { display.sleep(); } } display.dispose(); }
From source file:org.eclipse.swt.examples.accessibility.AccessibleValueExample.java
public static void main(String[] args) { Display display = new Display(); Shell shell = new Shell(display); shell.setLayout(new FillLayout()); shell.setText("Accessible Value Example"); final Canvas canvas = new Canvas(shell, SWT.DOUBLE_BUFFERED); canvas.addPaintListener(e -> {/*from w ww . j a v a 2 s. c o m*/ Rectangle rect = canvas.getClientArea(); String val = String.valueOf(value); Point size = e.gc.stringExtent(val); e.gc.setBackground(e.display.getSystemColor(SWT.COLOR_LIST_SELECTION)); e.gc.fillRectangle(0, 0, rect.width * value / (max - min), rect.height); e.gc.drawString(val, rect.x + (rect.width - size.x) / 2, rect.y + (rect.height - size.y) / 2, true); }); Accessible accessible = canvas.getAccessible(); accessible.addAccessibleListener(new AccessibleAdapter() { @Override public void getName(AccessibleEvent e) { e.result = "The value of this canvas is " + value; } }); accessible.addAccessibleControlListener(new AccessibleControlAdapter() { @Override public void getRole(AccessibleControlEvent e) { e.detail = ACC.ROLE_PROGRESSBAR; } }); accessible.addAccessibleValueListener(new AccessibleValueAdapter() { @Override public void setCurrentValue(AccessibleValueEvent e) { value = e.value.intValue(); canvas.redraw(); } @Override public void getMinimumValue(AccessibleValueEvent e) { e.value = Integer.valueOf(min); } @Override public void getMaximumValue(AccessibleValueEvent e) { e.value = Integer.valueOf(max); } @Override public void getCurrentValue(AccessibleValueEvent e) { e.value = Integer.valueOf(value); } }); shell.pack(); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } display.dispose(); }
From source file:FontSizeString.java
public static void main(String[] a) { Display display = new Display(); final Shell shell = new Shell(display); shell.setText("Extents"); final Canvas canvas = new Canvas(shell, SWT.NONE); shell.addControlListener(new ControlAdapter() { public void controlResized(ControlEvent event) { canvas.setBounds(shell.getClientArea()); }/*from w w w . j a va 2 s .c o m*/ }); canvas.addPaintListener(new PaintListener() { public void paintControl(PaintEvent event) { event.gc.setFont(font); Point pt = event.gc.stringExtent(STRING); System.out.println(pt); } }); // Create an editor to house the dropdown ControlEditor editor = new ControlEditor(canvas); // Create the combo and fill it final Combo combo = new Combo(canvas, SWT.READ_ONLY); for (int i = 0, n = SIZES.length; i < n; i++) { combo.add(SIZES[i]); } // Set up the editor editor.horizontalAlignment = SWT.CENTER; editor.verticalAlignment = SWT.TOP; Point size = combo.computeSize(SWT.DEFAULT, SWT.DEFAULT); editor.minimumWidth = size.x; editor.minimumHeight = size.y; editor.setEditor(combo); combo.addSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent event) { if (font != null) font.dispose(); font = new Font(shell.getDisplay(), "Helvetica", new Integer(combo.getText()).intValue(), SWT.BOLD); canvas.redraw(); } }); // Select the first item in the combo combo.select(0); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) { display.sleep(); } } if (font != null) font.dispose(); display.dispose(); }
From source file:org.eclipse.swt.snippets.Snippet338.java
public static void main(String[] args) { Display display = new Display(); Shell parentShell = new Shell(display); parentShell.setText("Snippet 338"); parentShell.setBounds(10, 10, 100, 100); parentShell.open();/*from www .j a v a 2s. c om*/ Shell childShell = new Shell(parentShell); childShell.setLayout(new GridLayout()); TabFolder folder = new TabFolder(childShell, SWT.NONE); folder.setLayout(new FillLayout()); TabItem tab1 = new TabItem(folder, SWT.NONE); tab1.setText("Tab &1"); new TabItem(folder, SWT.NONE).setText("Tab &2"); Composite composite = new Composite(folder, SWT.NONE); composite.setLayout(new GridLayout()); tab1.setControl(composite); Text text1 = new Text(composite, SWT.SINGLE); /* canvas represents a custom control */ final Canvas canvas = new Canvas(composite, SWT.BORDER); canvas.setLayoutData(new GridData(300, 200)); canvas.addListener(SWT.Paint, event -> { if (canvas.isFocusControl()) { event.gc.drawText( "focus is here, custom traverse keys:\n\tN: Tab next\n\tP: Tab previous\n\tR: Return\n\tE: Esc\n\tT: Tab Folder next page", 0, 0); } else { event.gc.drawString("focus is not in this control", 0, 0); } }); canvas.addListener(SWT.KeyDown, event -> { int traversal = SWT.NONE; switch (event.keyCode) { case 'n': traversal = SWT.TRAVERSE_TAB_NEXT; break; case 'p': traversal = SWT.TRAVERSE_TAB_PREVIOUS; break; case 'r': traversal = SWT.TRAVERSE_RETURN; break; case 'e': traversal = SWT.TRAVERSE_ESCAPE; break; case 't': traversal = SWT.TRAVERSE_PAGE_NEXT; break; } if (traversal != SWT.NONE) { event.doit = true; /* this will be the Traverse event's initial doit value */ canvas.traverse(traversal, event); } }); canvas.addFocusListener(focusLostAdapter(e -> canvas.redraw())); canvas.addFocusListener(focusGainedAdapter(e -> canvas.redraw())); Text text2 = new Text(composite, SWT.SINGLE); Button button = new Button(childShell, SWT.PUSH); button.setText("Default &Button"); button.addListener(SWT.Selection, event -> System.out.println("Default button selected")); childShell.setDefaultButton(button); Listener printTraverseListener = event -> { StringBuilder buffer = new StringBuilder("Traverse "); buffer.append(event.widget); buffer.append(" type="); switch (event.detail) { case SWT.TRAVERSE_ARROW_NEXT: buffer.append("TRAVERSE_ARROW_NEXT"); break; case SWT.TRAVERSE_ARROW_PREVIOUS: buffer.append("TRAVERSE_ARROW_NEXT"); break; case SWT.TRAVERSE_ESCAPE: buffer.append("TRAVERSE_ESCAPE"); break; case SWT.TRAVERSE_MNEMONIC: buffer.append("TRAVERSE_MNEMONIC"); break; case SWT.TRAVERSE_PAGE_NEXT: buffer.append("TRAVERSE_PAGE_NEXT"); break; case SWT.TRAVERSE_PAGE_PREVIOUS: buffer.append("TRAVERSE_PAGE_PREVIOUS"); break; case SWT.TRAVERSE_RETURN: buffer.append("TRAVERSE_RETURN"); break; case SWT.TRAVERSE_TAB_NEXT: buffer.append("TRAVERSE_TAB_NEXT"); break; case SWT.TRAVERSE_TAB_PREVIOUS: buffer.append("TRAVERSE_TAB_PREVIOUS"); break; } buffer.append(" doit=" + event.doit); buffer.append(" keycode=" + event.keyCode); buffer.append(" char=" + event.character); buffer.append(" stateMask=" + event.stateMask); System.out.println(buffer.toString()); }; childShell.addListener(SWT.Traverse, printTraverseListener); folder.addListener(SWT.Traverse, printTraverseListener); composite.addListener(SWT.Traverse, printTraverseListener); canvas.addListener(SWT.Traverse, printTraverseListener); button.addListener(SWT.Traverse, printTraverseListener); text1.addListener(SWT.Traverse, printTraverseListener); text2.addListener(SWT.Traverse, printTraverseListener); childShell.pack(); childShell.open(); text1.setFocus(); while (!parentShell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } display.dispose(); }
From source file:org.eclipse.swt.snippets.Snippet48.java
public static void main(String[] args) { Display display = new Display(); Shell shell = new Shell(display); shell.setText("Snippet 48"); shell.setLayout(new FillLayout()); Image originalImage = null;//from w w w . ja v a 2 s . co m FileDialog dialog = new FileDialog(shell, SWT.OPEN); dialog.setText("Open an image file or cancel"); String string = dialog.open(); if (string != null) { originalImage = new Image(display, string); } if (originalImage == null) { int width = 150, height = 200; originalImage = new Image(display, width, height); GC gc = new GC(originalImage); gc.fillRectangle(0, 0, width, height); gc.drawLine(0, 0, width, height); gc.drawLine(0, height, width, 0); gc.drawText("Default Image", 10, 10); gc.dispose(); } final Image image = originalImage; final Point origin = new Point(0, 0); final Canvas canvas = new Canvas(shell, SWT.NO_BACKGROUND | SWT.NO_REDRAW_RESIZE | SWT.V_SCROLL | SWT.H_SCROLL); final ScrollBar hBar = canvas.getHorizontalBar(); hBar.addListener(SWT.Selection, e -> { int hSelection = hBar.getSelection(); int destX = -hSelection - origin.x; Rectangle rect = image.getBounds(); canvas.scroll(destX, 0, 0, 0, rect.width, rect.height, false); origin.x = -hSelection; }); final ScrollBar vBar = canvas.getVerticalBar(); vBar.addListener(SWT.Selection, e -> { int vSelection = vBar.getSelection(); int destY = -vSelection - origin.y; Rectangle rect = image.getBounds(); canvas.scroll(0, destY, 0, 0, rect.width, rect.height, false); origin.y = -vSelection; }); canvas.addListener(SWT.Resize, e -> { Rectangle rect = image.getBounds(); Rectangle client = canvas.getClientArea(); hBar.setMaximum(rect.width); vBar.setMaximum(rect.height); hBar.setThumb(Math.min(rect.width, client.width)); vBar.setThumb(Math.min(rect.height, client.height)); int hPage = rect.width - client.width; int vPage = rect.height - client.height; int hSelection = hBar.getSelection(); int vSelection = vBar.getSelection(); if (hSelection >= hPage) { if (hPage <= 0) hSelection = 0; origin.x = -hSelection; } if (vSelection >= vPage) { if (vPage <= 0) vSelection = 0; origin.y = -vSelection; } canvas.redraw(); }); canvas.addListener(SWT.Paint, e -> { GC gc = e.gc; gc.drawImage(image, origin.x, origin.y); Rectangle rect = image.getBounds(); Rectangle client = canvas.getClientArea(); int marginWidth = client.width - rect.width; if (marginWidth > 0) { gc.fillRectangle(rect.width, 0, marginWidth, client.height); } int marginHeight = client.height - rect.height; if (marginHeight > 0) { gc.fillRectangle(0, rect.height, client.width, marginHeight); } }); Rectangle rect = image.getBounds(); shell.setSize(Math.max(200, rect.width - 100), Math.max(150, rect.height - 100)); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } originalImage.dispose(); display.dispose(); }
From source file:ImageScrollFlickerFree.java
public static void main(String[] args) { Display display = new Display(); Shell shell = new Shell(display); shell.setLayout(new FillLayout()); Image originalImage = null;//from ww w . j ava 2s . c o m FileDialog dialog = new FileDialog(shell, SWT.OPEN); dialog.setText("Open an image file or cancel"); String string = dialog.open(); if (string != null) { originalImage = new Image(display, string); } if (originalImage == null) { int width = 150, height = 200; originalImage = new Image(display, width, height); GC gc = new GC(originalImage); gc.fillRectangle(0, 0, width, height); gc.drawLine(0, 0, width, height); gc.drawLine(0, height, width, 0); gc.drawText("Default Image", 10, 10); gc.dispose(); } final Image image = originalImage; final Point origin = new Point(0, 0); final Canvas canvas = new Canvas(shell, SWT.NO_BACKGROUND | SWT.NO_REDRAW_RESIZE | SWT.V_SCROLL | SWT.H_SCROLL); final ScrollBar hBar = canvas.getHorizontalBar(); hBar.addListener(SWT.Selection, new Listener() { public void handleEvent(Event e) { int hSelection = hBar.getSelection(); int destX = -hSelection - origin.x; Rectangle rect = image.getBounds(); canvas.scroll(destX, 0, 0, 0, rect.width, rect.height, false); origin.x = -hSelection; } }); final ScrollBar vBar = canvas.getVerticalBar(); vBar.addListener(SWT.Selection, new Listener() { public void handleEvent(Event e) { int vSelection = vBar.getSelection(); int destY = -vSelection - origin.y; Rectangle rect = image.getBounds(); canvas.scroll(0, destY, 0, 0, rect.width, rect.height, false); origin.y = -vSelection; } }); canvas.addListener(SWT.Resize, new Listener() { public void handleEvent(Event e) { Rectangle rect = image.getBounds(); Rectangle client = canvas.getClientArea(); hBar.setMaximum(rect.width); vBar.setMaximum(rect.height); hBar.setThumb(Math.min(rect.width, client.width)); vBar.setThumb(Math.min(rect.height, client.height)); int hPage = rect.width - client.width; int vPage = rect.height - client.height; int hSelection = hBar.getSelection(); int vSelection = vBar.getSelection(); if (hSelection >= hPage) { if (hPage <= 0) hSelection = 0; origin.x = -hSelection; } if (vSelection >= vPage) { if (vPage <= 0) vSelection = 0; origin.y = -vSelection; } canvas.redraw(); } }); canvas.addListener(SWT.Paint, new Listener() { public void handleEvent(Event e) { GC gc = e.gc; gc.drawImage(image, origin.x, origin.y); Rectangle rect = image.getBounds(); Rectangle client = canvas.getClientArea(); int marginWidth = client.width - rect.width; if (marginWidth > 0) { gc.fillRectangle(rect.width, 0, marginWidth, client.height); } int marginHeight = client.height - rect.height; if (marginHeight > 0) { gc.fillRectangle(0, rect.height, client.width, marginHeight); } } }); shell.setSize(200, 150); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } originalImage.dispose(); display.dispose(); }
From source file:RoundRectangleExample.java
/** * Creates the main window's contents// ww w .ja v a 2 s . c o m * * @param shell the main window */ private void createContents(Shell shell) { shell.setLayout(new FillLayout(SWT.VERTICAL)); // Create the composite that holds the input fields Composite widgetComposite = new Composite(shell, SWT.NONE); widgetComposite.setLayout(new GridLayout(2, false)); // Create the input fields new Label(widgetComposite, SWT.NONE).setText("Arc Width:"); txtArcWidth = new Text(widgetComposite, SWT.BORDER); new Label(widgetComposite, SWT.NONE).setText("Arc Height"); txtArcHeight = new Text(widgetComposite, SWT.BORDER); // Create the button that launches the redraw Button button = new Button(widgetComposite, SWT.PUSH); button.setText("Redraw"); shell.setDefaultButton(button); // Create the canvas to draw the round rectangle on final Canvas drawingCanvas = new Canvas(shell, SWT.NONE); drawingCanvas.addPaintListener(new RoundRectangleExamplePaintListener()); // Add a handler to redraw the round rectangle when pressed button.addSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent e) { drawingCanvas.redraw(); } }); }
From source file:Animations.java
public Animations() { shell.setLayout(new FillLayout()); ImageLoader imageLoader = new ImageLoader(); final ImageData[] imageDatas = imageLoader.load("java2s.gif"); final Image image = new Image(display, imageDatas[0].width, imageDatas[0].height); final Canvas canvas = new Canvas(shell, SWT.NULL); canvas.addPaintListener(new PaintListener() { public void paintControl(PaintEvent e) { e.gc.drawImage(image, 0, 0); }/*w w w . ja v a 2s. c o m*/ }); final GC gc = new GC(image); final Thread thread = new Thread() { int frameIndex = 0; public void run() { while (!isInterrupted()) { frameIndex %= imageDatas.length; final ImageData frameData = imageDatas[frameIndex]; display.asyncExec(new Runnable() { public void run() { Image frame = new Image(display, frameData); gc.drawImage(frame, frameData.x, frameData.y); frame.dispose(); canvas.redraw(); } }); try { // delay Thread.sleep(imageDatas[frameIndex].delayTime * 10); } catch (InterruptedException e) { return; } frameIndex += 1; } } }; shell.addShellListener(new ShellAdapter() { public void shellClosed(ShellEvent e) { thread.interrupt(); } }); shell.setSize(400, 200); shell.open(); thread.start(); // Set up the event loop. while (!shell.isDisposed()) { if (!display.readAndDispatch()) { // If no more entries in event queue display.sleep(); } } display.dispose(); }