List of usage examples for org.eclipse.swt.widgets Canvas getVerticalBar
public ScrollBar getVerticalBar()
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 ww w . j a v a 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, 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;/*ww w . j av a 2 s . 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:org.eclipse.swt.examples.clipboard.ClipboardExample.java
void createImageTransfer(Composite copyParent, Composite pasteParent) { final Image[] copyImage = new Image[] { null }; Label l = new Label(copyParent, SWT.NONE); l.setText("ImageTransfer:"); //$NON-NLS-1$ GridData data = new GridData(); data.verticalSpan = 2;/*from w w w . ja v a 2 s. c om*/ l.setLayoutData(data); final Canvas copyImageCanvas = new Canvas(copyParent, SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL); data = new GridData(GridData.FILL_BOTH); data.verticalSpan = 2; data.widthHint = HSIZE; data.heightHint = VSIZE; copyImageCanvas.setLayoutData(data); final Point copyOrigin = new Point(0, 0); final ScrollBar copyHBar = copyImageCanvas.getHorizontalBar(); copyHBar.setEnabled(false); copyHBar.addListener(SWT.Selection, e -> { if (copyImage[0] != null) { int hSelection = copyHBar.getSelection(); int destX = -hSelection - copyOrigin.x; Rectangle rect = copyImage[0].getBounds(); copyImageCanvas.scroll(destX, 0, 0, 0, rect.width, rect.height, false); copyOrigin.x = -hSelection; } }); final ScrollBar copyVBar = copyImageCanvas.getVerticalBar(); copyVBar.setEnabled(false); copyVBar.addListener(SWT.Selection, e -> { if (copyImage[0] != null) { int vSelection = copyVBar.getSelection(); int destY = -vSelection - copyOrigin.y; Rectangle rect = copyImage[0].getBounds(); copyImageCanvas.scroll(0, destY, 0, 0, rect.width, rect.height, false); copyOrigin.y = -vSelection; } }); copyImageCanvas.addListener(SWT.Paint, e -> { if (copyImage[0] != null) { GC gc = e.gc; gc.drawImage(copyImage[0], copyOrigin.x, copyOrigin.y); Rectangle rect = copyImage[0].getBounds(); Rectangle client = copyImageCanvas.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); } gc.dispose(); } }); Button openButton = new Button(copyParent, SWT.PUSH); openButton.setText("Open Image"); openButton.addSelectionListener(widgetSelectedAdapter(e -> { FileDialog dialog = new FileDialog(shell, SWT.OPEN); dialog.setText("Open an image file or cancel"); String string = dialog.open(); if (string != null) { if (copyImage[0] != null) { System.out.println("CopyImage"); copyImage[0].dispose(); } copyImage[0] = new Image(e.display, string); copyVBar.setEnabled(true); copyHBar.setEnabled(true); copyOrigin.x = 0; copyOrigin.y = 0; Rectangle rect = copyImage[0].getBounds(); Rectangle client = copyImageCanvas.getClientArea(); copyHBar.setMaximum(rect.width); copyVBar.setMaximum(rect.height); copyHBar.setThumb(Math.min(rect.width, client.width)); copyVBar.setThumb(Math.min(rect.height, client.height)); copyImageCanvas.scroll(0, 0, 0, 0, rect.width, rect.height, true); copyVBar.setSelection(0); copyHBar.setSelection(0); copyImageCanvas.redraw(); } })); Button b = new Button(copyParent, SWT.PUSH); b.setText("Copy"); b.addSelectionListener(widgetSelectedAdapter(e -> { if (copyImage[0] != null) { status.setText(""); // Fetch ImageData at current zoom and save in the clip-board. clipboard.setContents(new Object[] { copyImage[0].getImageDataAtCurrentZoom() }, new Transfer[] { ImageTransfer.getInstance() }); } else { status.setText("No image to copy"); } })); final Image[] pasteImage = new Image[] { null }; l = new Label(pasteParent, SWT.NONE); l.setText("ImageTransfer:"); final Canvas pasteImageCanvas = new Canvas(pasteParent, SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL); data = new GridData(GridData.FILL_BOTH); data.widthHint = HSIZE; data.heightHint = VSIZE; pasteImageCanvas.setLayoutData(data); final Point pasteOrigin = new Point(0, 0); final ScrollBar pasteHBar = pasteImageCanvas.getHorizontalBar(); pasteHBar.setEnabled(false); pasteHBar.addListener(SWT.Selection, e -> { if (pasteImage[0] != null) { int hSelection = pasteHBar.getSelection(); int destX = -hSelection - pasteOrigin.x; Rectangle rect = pasteImage[0].getBounds(); pasteImageCanvas.scroll(destX, 0, 0, 0, rect.width, rect.height, false); pasteOrigin.x = -hSelection; } }); final ScrollBar pasteVBar = pasteImageCanvas.getVerticalBar(); pasteVBar.setEnabled(false); pasteVBar.addListener(SWT.Selection, e -> { if (pasteImage[0] != null) { int vSelection = pasteVBar.getSelection(); int destY = -vSelection - pasteOrigin.y; Rectangle rect = pasteImage[0].getBounds(); pasteImageCanvas.scroll(0, destY, 0, 0, rect.width, rect.height, false); pasteOrigin.y = -vSelection; } }); pasteImageCanvas.addListener(SWT.Paint, e -> { if (pasteImage[0] != null) { GC gc = e.gc; gc.drawImage(pasteImage[0], pasteOrigin.x, pasteOrigin.y); Rectangle rect = pasteImage[0].getBounds(); Rectangle client = pasteImageCanvas.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); } } }); b = new Button(pasteParent, SWT.PUSH); b.setText("Paste"); b.addSelectionListener(widgetSelectedAdapter(e -> { ImageData imageData = (ImageData) clipboard.getContents(ImageTransfer.getInstance()); if (imageData != null) { if (pasteImage[0] != null) { System.out.println("PasteImage"); pasteImage[0].dispose(); } status.setText(""); // Consume the ImageData at current zoom as-is. pasteImage[0] = new Image(e.display, new AutoScaleImageDataProvider(imageData)); pasteVBar.setEnabled(true); pasteHBar.setEnabled(true); pasteOrigin.x = 0; pasteOrigin.y = 0; Rectangle rect = pasteImage[0].getBounds(); Rectangle client = pasteImageCanvas.getClientArea(); pasteHBar.setMaximum(rect.width); pasteVBar.setMaximum(rect.height); pasteHBar.setThumb(Math.min(rect.width, client.width)); pasteVBar.setThumb(Math.min(rect.height, client.height)); pasteImageCanvas.scroll(0, 0, 0, 0, rect.width, rect.height, true); pasteVBar.setSelection(0); pasteHBar.setSelection(0); pasteImageCanvas.redraw(); } else { status.setText("No image to paste"); } })); }
From source file:org.eclipse.swt.examples.paint.PaintSurface.java
/** * Constructs a PaintSurface./* w ww.j av a 2s . c o m*/ * <p> * paintCanvas must have SWT.NO_REDRAW_RESIZE and SWT.NO_BACKGROUND styles, * and may have SWT.V_SCROLL and/or SWT.H_SCROLL. * </p> * @param paintCanvas the Canvas object in which to render * @param paintStatus the PaintStatus object to use for providing user feedback * @param fillColor the color to fill the canvas with initially */ public PaintSurface(Canvas paintCanvas, Text statusText, Color fillColor) { this.paintCanvas = paintCanvas; this.statusText = statusText; clearStatus(); /* Set up the drawing surface */ Rectangle displayRect = paintCanvas.getDisplay().getClientArea(); imageWidth = displayRect.width; imageHeight = displayRect.height; image = new Image(paintCanvas.getDisplay(), imageWidth, imageHeight); imageFDC.gc = new GC(image); imageFDC.gc.setBackground(fillColor); imageFDC.gc.fillRectangle(0, 0, imageWidth, imageHeight); displayFDC.gc = new GC(paintCanvas); /* Initialize the session */ setPaintSession(null); /* Add our listeners */ paintCanvas.addDisposeListener(e -> displayFDC.gc.dispose()); paintCanvas.addMouseListener(new MouseAdapter() { @Override public void mouseDown(MouseEvent event) { processMouseEventCoordinates(event); if (paintSession != null) paintSession.mouseDown(event); } @Override public void mouseUp(MouseEvent event) { processMouseEventCoordinates(event); if (paintSession != null) paintSession.mouseUp(event); } @Override public void mouseDoubleClick(MouseEvent event) { processMouseEventCoordinates(event); if (paintSession != null) paintSession.mouseDoubleClick(event); } }); paintCanvas.addMouseMoveListener(event -> { processMouseEventCoordinates(event); if (paintSession != null) paintSession.mouseMove(event); }); paintCanvas.addPaintListener(event -> { if (rubberband.isEmpty()) { // Nothing to merge, so we just refresh event.gc.drawImage(image, displayFDC.xOffset + event.x, displayFDC.yOffset + event.y, event.width, event.height, event.x, event.y, event.width, event.height); } else { /* * Avoid flicker when merging overlayed objects by constructing the image on * a backbuffer first, then blitting it to the screen. */ // Check that the backbuffer is large enough if (paintImage != null) { Rectangle rect1 = paintImage.getBounds(); if ((event.width + event.x > rect1.width) || (event.height + event.y > rect1.height)) { paintFDC.gc.dispose(); paintImage.dispose(); paintImage = null; } } if (paintImage == null) { Display display = getDisplay(); Rectangle rect2 = display.getClientArea(); paintImage = new Image(display, Math.max(rect2.width, event.width + event.x), Math.max(rect2.height, event.height + event.y)); paintFDC.gc = new GC(paintImage); } // Setup clipping and the FDC Region clipRegion = new Region(); event.gc.getClipping(clipRegion); paintFDC.gc.setClipping(clipRegion); clipRegion.dispose(); paintFDC.xOffset = displayFDC.xOffset; paintFDC.yOffset = displayFDC.yOffset; paintFDC.xScale = displayFDC.xScale; paintFDC.yScale = displayFDC.yScale; // Merge the overlayed objects into the image, then blit paintFDC.gc.drawImage(image, displayFDC.xOffset + event.x, displayFDC.yOffset + event.y, event.width, event.height, event.x, event.y, event.width, event.height); rubberband.draw(paintFDC); event.gc.drawImage(paintImage, event.x, event.y, event.width, event.height, event.x, event.y, event.width, event.height); } }); paintCanvas.addControlListener(ControlListener.controlResizedAdapter(e -> handleResize())); /* Set up the paint canvas scroll bars */ ScrollBar horizontal = paintCanvas.getHorizontalBar(); horizontal.setVisible(true); horizontal .addSelectionListener(widgetSelectedAdapter(event -> scrollHorizontally((ScrollBar) event.widget))); ScrollBar vertical = paintCanvas.getVerticalBar(); vertical.setVisible(true); vertical.addSelectionListener(widgetSelectedAdapter(event -> scrollVertically((ScrollBar) event.widget))); handleResize(); }
From source file:PaintExample.java
/** * Constructs a PaintSurface.// ww w.j a v a 2s . c o m * <p> * paintCanvas must have SWT.NO_REDRAW_RESIZE and SWT.NO_BACKGROUND styles, * and may have SWT.V_SCROLL and/or SWT.H_SCROLL. * </p> * @param paintCanvas the Canvas object in which to render * @param paintStatus the PaintStatus object to use for providing user feedback * @param fillColor the color to fill the canvas with initially */ public PaintSurface(Canvas paintCanvas, Text statusText, Color fillColor) { this.paintCanvas = paintCanvas; this.statusText = statusText; clearStatus(); /* Set up the drawing surface */ Rectangle displayRect = paintCanvas.getDisplay().getClientArea(); imageWidth = displayRect.width; imageHeight = displayRect.height; image = new Image(paintCanvas.getDisplay(), imageWidth, imageHeight); imageFDC.gc = new GC(image); imageFDC.gc.setBackground(fillColor); imageFDC.gc.fillRectangle(0, 0, imageWidth, imageHeight); displayFDC.gc = new GC(paintCanvas); /* Initialize the session */ setPaintSession(null); /* Add our listeners */ paintCanvas.addDisposeListener(new DisposeListener() { public void widgetDisposed(DisposeEvent e) { displayFDC.gc.dispose(); } }); paintCanvas.addMouseListener(new MouseAdapter() { public void mouseDown(MouseEvent event) { processMouseEventCoordinates(event); if (paintSession != null) paintSession.mouseDown(event); } public void mouseUp(MouseEvent event) { processMouseEventCoordinates(event); if (paintSession != null) paintSession.mouseUp(event); } public void mouseDoubleClick(MouseEvent event) { processMouseEventCoordinates(event); if (paintSession != null) paintSession.mouseDoubleClick(event); } }); paintCanvas.addMouseMoveListener(new MouseMoveListener() { public void mouseMove(MouseEvent event) { processMouseEventCoordinates(event); if (paintSession != null) paintSession.mouseMove(event); } }); paintCanvas.addPaintListener(new PaintListener() { public void paintControl(PaintEvent event) { if (rubberband.isEmpty()) { // Nothing to merge, so we just refresh event.gc.drawImage(image, displayFDC.xOffset + event.x, displayFDC.yOffset + event.y, event.width, event.height, event.x, event.y, event.width, event.height); } else { /* * Avoid flicker when merging overlayed objects by constructing the image on * a backbuffer first, then blitting it to the screen. */ // Check that the backbuffer is large enough if (paintImage != null) { Rectangle rect = paintImage.getBounds(); if ((event.width + event.x > rect.width) || (event.height + event.y > rect.height)) { paintFDC.gc.dispose(); paintImage.dispose(); paintImage = null; } } if (paintImage == null) { Display display = getDisplay(); Rectangle rect = display.getClientArea(); paintImage = new Image(display, Math.max(rect.width, event.width + event.x), Math.max(rect.height, event.height + event.y)); paintFDC.gc = new GC(paintImage); } // Setup clipping and the FDC Region clipRegion = new Region(); event.gc.getClipping(clipRegion); paintFDC.gc.setClipping(clipRegion); clipRegion.dispose(); paintFDC.xOffset = displayFDC.xOffset; paintFDC.yOffset = displayFDC.yOffset; paintFDC.xScale = displayFDC.xScale; paintFDC.yScale = displayFDC.yScale; // Merge the overlayed objects into the image, then blit paintFDC.gc.drawImage(image, displayFDC.xOffset + event.x, displayFDC.yOffset + event.y, event.width, event.height, event.x, event.y, event.width, event.height); rubberband.draw(paintFDC); event.gc.drawImage(paintImage, event.x, event.y, event.width, event.height, event.x, event.y, event.width, event.height); } } }); paintCanvas.addControlListener(new ControlAdapter() { public void controlResized(ControlEvent event) { handleResize(); } }); /* Set up the paint canvas scroll bars */ ScrollBar horizontal = paintCanvas.getHorizontalBar(); horizontal.setVisible(true); horizontal.addSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent event) { scrollHorizontally((ScrollBar) event.widget); } }); ScrollBar vertical = paintCanvas.getVerticalBar(); vertical.setVisible(true); vertical.addSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent event) { scrollVertically((ScrollBar) event.widget); } }); handleResize(); }