diff --git a/bundles/org.eclipse.swt/Eclipse SWT Custom Widgets/common/org/eclipse/swt/custom/StyledTextRenderer.java b/bundles/org.eclipse.swt/Eclipse SWT Custom Widgets/common/org/eclipse/swt/custom/StyledTextRenderer.java index fa0281c2b31..22af2427048 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT Custom Widgets/common/org/eclipse/swt/custom/StyledTextRenderer.java +++ b/bundles/org.eclipse.swt/Eclipse SWT Custom Widgets/common/org/eclipse/swt/custom/StyledTextRenderer.java @@ -1475,7 +1475,7 @@ void setFont(Font font, int tabs) { if (boldItalicFont != null) boldItalicFont.dispose(); boldFont = italicFont = boldItalicFont = null; regularFont = font; - layout.setText(" "); + layout.setText(" "); layout.setFont(font); layout.setStyle(new TextStyle(getFont(SWT.NORMAL), null, null), 0, 0); layout.setStyle(new TextStyle(getFont(SWT.BOLD), null, null), 1, 1); diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet10.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet10.java index bedaca7f4f2..19f896fe986 100644 --- a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet10.java +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet10.java @@ -32,12 +32,13 @@ public static void main(String[] args) { shell.setText("Advanced Graphics"); FontData fd = shell.getFont().getFontData()[0]; final Font font = new Font(display, fd.getName(), 60, SWT.BOLD | SWT.ITALIC); - final Image image = new Image(display, 640, 480); + final ImageGcDrawer imageGcDrawer = (gc, imageWidth, imageHeight) -> { + gc.setBackground(display.getSystemColor(SWT.COLOR_RED)); + gc.fillOval(0, 0, imageWidth, imageHeight); + }; + final Image image = new Image(display,imageGcDrawer, 640, 480); final Rectangle rect = image.getBounds(); - GC gc = new GC(image); - gc.setBackground(display.getSystemColor(SWT.COLOR_RED)); - gc.fillOval(rect.x, rect.y, rect.width, rect.height); - gc.dispose(); + shell.addListener(SWT.Paint, event -> { GC gc1 = event.gc; Transform tr = new Transform(display); @@ -61,6 +62,7 @@ public static void main(String[] args) { if (!display.readAndDispatch()) display.sleep(); } + image.dispose(); font.dispose(); display.dispose(); diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet104.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet104.java index 9008cab1c5d..29bc9ec9041 100644 --- a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet104.java +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet104.java @@ -26,58 +26,63 @@ public class Snippet104 { -public static void main(String[] args) { - final Display display = new Display(); - final int [] count = new int [] {4}; - final Image image = new Image(display, 300, 300); - GC gc = new GC(image); - gc.setBackground(display.getSystemColor(SWT.COLOR_CYAN)); - gc.fillRectangle(image.getBounds()); - gc.drawText("Splash Screen", 10, 10); - gc.dispose(); - final Shell splash = new Shell(SWT.ON_TOP); - final ProgressBar bar = new ProgressBar(splash, SWT.NONE); - bar.setMaximum(count[0]); - Label label = new Label(splash, SWT.NONE); - label.setImage(image); - FormLayout layout = new FormLayout(); - splash.setLayout(layout); - FormData labelData = new FormData (); - labelData.right = new FormAttachment (100, 0); - labelData.bottom = new FormAttachment (100, 0); - label.setLayoutData(labelData); - FormData progressData = new FormData (); - progressData.left = new FormAttachment (0, 5); - progressData.right = new FormAttachment (100, -5); - progressData.bottom = new FormAttachment (100, -5); - bar.setLayoutData(progressData); - splash.pack(); - Rectangle splashRect = splash.getBounds(); - Rectangle displayRect = display.getBounds(); - int x = (displayRect.width - splashRect.width) / 2; - int y = (displayRect.height - splashRect.height) / 2; - splash.setLocation(x, y); - splash.open(); - display.asyncExec(() -> { - Shell [] shells = new Shell[count[0]]; - for (int i1=0; i1 --count[0]); - bar.setSelection(i1+1); - try {Thread.sleep(1000);} catch (Throwable e) {} - } - splash.close(); - image.dispose(); - for (int i2=0; i2 { + gc.setBackground(display.getSystemColor(SWT.COLOR_CYAN)); + gc.fillRectangle(0, 0, imageWidth, imageHeight); + gc.drawText("Splash Screen", 10, 10); + }; + final Image image = new Image(display, imageGcDrawer, 300, 300); + final Shell splash = new Shell(SWT.ON_TOP); + final ProgressBar bar = new ProgressBar(splash, SWT.NONE); + bar.setMaximum(count[0]); + Label label = new Label(splash, SWT.NONE); + label.setImage(image); + FormLayout layout = new FormLayout(); + splash.setLayout(layout); + FormData labelData = new FormData(); + labelData.right = new FormAttachment(100, 0); + labelData.bottom = new FormAttachment(100, 0); + label.setLayoutData(labelData); + FormData progressData = new FormData(); + progressData.left = new FormAttachment(0, 5); + progressData.right = new FormAttachment(100, -5); + progressData.bottom = new FormAttachment(100, -5); + bar.setLayoutData(progressData); + splash.pack(); + Rectangle splashRect = splash.getBounds(); + Rectangle displayRect = display.getBounds(); + int x = (displayRect.width - splashRect.width) / 2; + int y = (displayRect.height - splashRect.height) / 2; + splash.setLocation(x, y); + splash.open(); + display.asyncExec(() -> { + Shell[] shells = new Shell[count[0]]; + for (int i1 = 0; i1 < count[0]; i1++) { + shells[i1] = new Shell(display); + shells[i1].setSize(300, 300); + shells[i1].addListener(SWT.Close, e -> --count[0]); + bar.setSelection(i1 + 1); + try { + Thread.sleep(1000); + } catch (Throwable e) { + } + } + splash.close(); + image.dispose(); + for (int i2 = 0; i2 < count[0]; i2++) { + shells[i2].setText("SWT Snippet 104 - " + (i2 + 1)); + shells[i2].open(); + } + }); + while (count[0] != 0) { + if (!display.readAndDispatch()) + display.sleep(); } - }); - while (count [0] != 0) { - if (!display.readAndDispatch ()) display.sleep (); + display.dispose(); } - display.dispose(); -} } diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet112.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet112.java index 96e448aef6c..af99ae4c6dd 100644 --- a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet112.java +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet112.java @@ -28,12 +28,14 @@ public class Snippet112 { public static void main (String [] args) { Display display = new Display (); - final Image image = new Image (display, 20, 20); Color color = display.getSystemColor (SWT.COLOR_RED); - GC gc = new GC (image); - gc.setBackground (color); - gc.fillRectangle (image.getBounds ()); - gc.dispose (); + + final ImageGcDrawer imageGcDrawer = (gc, imageWidth, imageHeight) -> { + gc.setBackground(color); + gc.fillRectangle(0, 0, imageWidth, imageHeight); + }; + + final Image image = new Image (display, imageGcDrawer, 20, 20); Shell shell = new Shell (display); shell.setText("Snippet 112"); diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet138.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet138.java index 140ceaa05b6..0f61fbeadca 100644 --- a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet138.java +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet138.java @@ -28,7 +28,7 @@ public class Snippet138 { public static void main(String[] args) { Display display = new Display(); - +/* Image small = new Image(display, 16, 16); GC gc = new GC(small); gc.setBackground(display.getSystemColor(SWT.COLOR_RED)); @@ -40,6 +40,21 @@ public static void main(String[] args) { gc.setBackground(display.getSystemColor(SWT.COLOR_RED)); gc.fillArc(0, 0, 32, 32, 45, 270); gc.dispose(); +*/ + + final ImageGcDrawer imageGcDrawer = (gc, imageWidth, imageHeight) -> { + gc.setBackground(display.getSystemColor(SWT.COLOR_RED)); + gc.fillArc(0, 0, imageWidth, imageHeight, 45, 270); + }; + Image small = new Image(display, imageGcDrawer, 16, 16); + + final ImageGcDrawer imageGcDrawer1 = (gc, imageWidth, imageHeight) -> { + gc.setBackground(display.getSystemColor(SWT.COLOR_RED)); + gc.fillArc(0, 0, imageWidth, imageHeight, 45, 270); + }; + Image large = new Image(display, imageGcDrawer1, 32, 32); + + /* Provide different resolutions for icons to get * high quality rendering wherever the OS needs diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet139.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet139.java index d5f11852fd0..a3d1636f152 100644 --- a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet139.java +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet139.java @@ -91,7 +91,6 @@ static ImageData flip(ImageData srcData, boolean vertical) { public static void main(String[] args) { Display display = new Display(); - // create an image with the word "hello" on it final Image image0 = new Image(display, 50, 30); GC gc = new GC(image0); gc.setBackground(display.getSystemColor(SWT.COLOR_RED)); @@ -100,8 +99,9 @@ public static void main(String[] args) { gc.dispose(); ImageData data = image0.getImageData(); + // rotate and flip this image - final Image image1 = new Image(display, rotate(data, SWT.LEFT)); + final Image image1 = new Image(display, rotate(data, SWT.LEFT)); final Image image2 = new Image(display, rotate(data, SWT.RIGHT)); final Image image3 = new Image(display, rotate(data, SWT.DOWN)); final Image image4 = new Image(display, flip(data, true)); diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet141.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet141.java index d3d2ce755a3..d131459dde1 100644 --- a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet141.java +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet141.java @@ -49,6 +49,7 @@ public static void main(String[] args) { dialog.setFilterExtensions(new String[] {"*.gif"}); String fileName = dialog.open(); final AtomicBoolean stopAnimation = new AtomicBoolean(false); + if (fileName != null) { loader = new ImageLoader(); try { @@ -56,57 +57,42 @@ public static void main(String[] args) { if (imageDataArray.length > 1) { animateThread = new Thread("Animation") { @Override - @SuppressWarnings("unused") public void run() { - /* Create an off-screen image to draw on, and fill it with the shell background. */ - Image offScreenImage = new Image(display, loader.logicalScreenWidth, loader.logicalScreenHeight); - GC offScreenImageGC = new GC(offScreenImage); - offScreenImageGC.setBackground(shellBackground); - offScreenImageGC.fillRectangle(0, 0, loader.logicalScreenWidth, loader.logicalScreenHeight); - + Image offScreenImage = null; try { - /* Create the first image and draw it on the off-screen image. */ + int width = loader.logicalScreenWidth; + int height = loader.logicalScreenHeight; + + ImageGcDrawer offscreenDrawer = (gc, imageWidth, imageHeight) -> { + gc.setBackground(shellBackground); + gc.fillRectangle(0, 0, imageWidth, imageHeight); + }; + offScreenImage = new Image(display, offscreenDrawer, width, height); + GC offScreenImageGC = new GC(offScreenImage); + int imageDataIndex = 0; ImageData imageData = imageDataArray[imageDataIndex]; + if (image != null && !image.isDisposed()) image.dispose(); image = new Image(display, imageData); - offScreenImageGC.drawImage( - image, - 0, - 0, - imageData.width, - imageData.height, - imageData.x, - imageData.y, - imageData.width, - imageData.height); - - /* Now loop through the images, creating and drawing each one - * on the off-screen image before drawing it on the shell. */ + offScreenImageGC.drawImage(image, 0, 0, imageData.width, imageData.height, + imageData.x, imageData.y, imageData.width, imageData.height); + int repeatCount = loader.repeatCount; while ((loader.repeatCount == 0 || repeatCount > 0) && !stopAnimation.get()) { switch (imageData.disposalMethod) { case SWT.DM_FILL_BACKGROUND: - /* Fill with the background color before drawing. */ Color bgColor = null; if (useGIFBackground && loader.backgroundPixel != -1) { bgColor = new Color(imageData.palette.getRGB(loader.backgroundPixel)); } offScreenImageGC.setBackground(bgColor != null ? bgColor : shellBackground); offScreenImageGC.fillRectangle(imageData.x, imageData.y, imageData.width, imageData.height); + if (bgColor != null) bgColor.dispose(); break; case SWT.DM_FILL_PREVIOUS: - /* Restore the previous image before drawing. */ - offScreenImageGC.drawImage( - image, - 0, - 0, - imageData.width, - imageData.height, - imageData.x, - imageData.y, - imageData.width, - imageData.height); + offScreenImageGC.drawImage(image, 0, 0, imageData.width, imageData.height, + imageData.x, imageData.y, imageData.width, imageData.height); break; } @@ -114,37 +100,28 @@ public void run() { imageData = imageDataArray[imageDataIndex]; image.dispose(); image = new Image(display, imageData); - offScreenImageGC.drawImage( - image, - 0, - 0, - imageData.width, - imageData.height, - imageData.x, - imageData.y, - imageData.width, - imageData.height); - - /* Draw the off-screen image to the shell. */ + + offScreenImageGC.drawImage(image, 0, 0, imageData.width, imageData.height, + imageData.x, imageData.y, imageData.width, imageData.height); + shellGC.drawImage(offScreenImage, 0, 0); - /* Sleep for the specified delay time (adding commonly-used slow-down fudge factors). */ try { int ms = imageData.delayTime * 10; if (ms < 20) ms += 30; if (ms < 30) ms += 10; Thread.sleep(ms); } catch (InterruptedException e) { + // ignore } - /* If we have just drawn the last image, decrement the repeat count and start again. */ if (imageDataIndex == imageDataArray.length - 1) repeatCount--; } + offScreenImageGC.dispose(); } catch (SWTException ex) { System.out.println("There was an error animating the GIF"); } finally { if (offScreenImage != null && !offScreenImage.isDisposed()) offScreenImage.dispose(); - if (offScreenImageGC != null && !offScreenImageGC.isDisposed()) offScreenImageGC.dispose(); if (image != null && !image.isDisposed()) image.dispose(); } } @@ -163,4 +140,4 @@ public void run() { stopAnimation.set(true); display.dispose(); } -} +} \ No newline at end of file diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet143.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet143.java index df37a9eaa9b..04387f025eb 100644 --- a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet143.java +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet143.java @@ -27,44 +27,47 @@ public class Snippet143 { -public static void main(String[] args) { - Display display = new Display (); - Shell shell = new Shell (display); - shell.setText("Snippet 143"); - Image image = new Image (display, 16, 16); - Image image2 = new Image (display, 16, 16); - GC gc = new GC(image2); - gc.setBackground(display.getSystemColor(SWT.COLOR_BLACK)); - gc.fillRectangle(image2.getBounds()); - gc.dispose(); - final Tray tray = display.getSystemTray (); - if (tray == null) { - System.out.println ("The system tray is not available"); - } else { - final TrayItem item = new TrayItem (tray, SWT.NONE); - item.setToolTipText("SWT TrayItem"); - item.addListener (SWT.Show, event -> System.out.println("show")); - item.addListener (SWT.Hide, event -> System.out.println("hide")); - item.addListener (SWT.Selection, event -> System.out.println("selection")); - item.addListener (SWT.DefaultSelection, event -> System.out.println("default selection")); - final Menu menu = new Menu (shell, SWT.POP_UP); - for (int i = 0; i < 8; i++) { - MenuItem mi = new MenuItem (menu, SWT.PUSH); - mi.setText ("Item" + i); - mi.addListener (SWT.Selection, event -> System.out.println("selection " + event.widget)); - if (i == 0) menu.setDefaultItem(mi); + public static void main(String[] args) { + Display display = new Display(); + Shell shell = new Shell(display); + shell.setText("Snippet 143"); + ImageGcDrawer imgc1 = (gc, iwidth, iheight) -> {}; + Image image = new Image(display, imgc1, 16, 16); + ImageGcDrawer imgc = (gc, iwidth, iheight) -> { + gc.setBackground(display.getSystemColor(SWT.COLOR_BLACK)); + gc.fillRectangle(0, 0, iwidth, iheight); + }; + Image image2 = new Image(display, imgc, 16, 16); + final Tray tray = display.getSystemTray(); + if (tray == null) { + System.out.println("The system tray is not available"); + } else { + final TrayItem item = new TrayItem(tray, SWT.NONE); + item.setToolTipText("SWT TrayItem"); + item.addListener(SWT.Show, event -> System.out.println("show")); + item.addListener(SWT.Hide, event -> System.out.println("hide")); + item.addListener(SWT.Selection, event -> System.out.println("selection")); + item.addListener(SWT.DefaultSelection, event -> System.out.println("default selection")); + final Menu menu = new Menu(shell, SWT.POP_UP); + for (int i = 0; i < 8; i++) { + MenuItem mi = new MenuItem(menu, SWT.PUSH); + mi.setText("Item" + i); + mi.addListener(SWT.Selection, event -> System.out.println("selection " + event.widget)); + if (i == 0) + menu.setDefaultItem(mi); + } + item.addListener(SWT.MenuDetect, event -> menu.setVisible(true)); + item.setImage(image2); + //item.setHighlightImage(image); } - item.addListener (SWT.MenuDetect, event -> menu.setVisible (true)); - item.setImage (image2); - item.setHighlightImage (image); - } - shell.setBounds(50, 50, 300, 200); - shell.open (); - while (!shell.isDisposed ()) { - if (!display.readAndDispatch ()) display.sleep (); + shell.setBounds(50, 50, 300, 200); + shell.open(); + while (!shell.isDisposed()) { + if (!display.readAndDispatch()) + display.sleep(); + } + image.dispose(); + image2.dispose(); + display.dispose(); } - image.dispose (); - image2.dispose (); - display.dispose (); -} -} +} \ No newline at end of file diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet156.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet156.java index 156864359c3..8b3d8fb44e7 100644 --- a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet156.java +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet156.java @@ -31,168 +31,177 @@ public class Snippet156 { -static BufferedImage convertToAWT(ImageData data) { - ColorModel colorModel = null; - PaletteData palette = data.palette; - if (palette.isDirect) { - colorModel = new DirectColorModel(data.depth, palette.redMask, palette.greenMask, palette.blueMask); - BufferedImage bufferedImage = new BufferedImage(colorModel, colorModel.createCompatibleWritableRaster(data.width, data.height), false, null); - for (int y = 0; y < data.height; y++) { - for (int x = 0; x < data.width; x++) { - int pixel = data.getPixel(x, y); - RGB rgb = palette.getRGB(pixel); - bufferedImage.setRGB(x, y, rgb.red << 16 | rgb.green << 8 | rgb.blue); + static BufferedImage convertToAWT(ImageData data) { + ColorModel colorModel = null; + PaletteData palette = data.palette; + if (palette.isDirect) { + colorModel = new DirectColorModel(data.depth, palette.redMask, palette.greenMask, palette.blueMask); + BufferedImage bufferedImage = new BufferedImage(colorModel, + colorModel.createCompatibleWritableRaster(data.width, data.height), false, null); + for (int y = 0; y < data.height; y++) { + for (int x = 0; x < data.width; x++) { + int pixel = data.getPixel(x, y); + RGB rgb = palette.getRGB(pixel); + bufferedImage.setRGB(x, y, rgb.red << 16 | rgb.green << 8 | rgb.blue); + } } - } - return bufferedImage; - } else { - RGB[] rgbs = palette.getRGBs(); - byte[] red = new byte[rgbs.length]; - byte[] green = new byte[rgbs.length]; - byte[] blue = new byte[rgbs.length]; - for (int i = 0; i < rgbs.length; i++) { - RGB rgb = rgbs[i]; - red[i] = (byte)rgb.red; - green[i] = (byte)rgb.green; - blue[i] = (byte)rgb.blue; - } - if (data.transparentPixel != -1) { - colorModel = new IndexColorModel(data.depth, rgbs.length, red, green, blue, data.transparentPixel); + return bufferedImage; } else { - colorModel = new IndexColorModel(data.depth, rgbs.length, red, green, blue); - } - BufferedImage bufferedImage = new BufferedImage(colorModel, colorModel.createCompatibleWritableRaster(data.width, data.height), false, null); - WritableRaster raster = bufferedImage.getRaster(); - int[] pixelArray = new int[1]; - for (int y = 0; y < data.height; y++) { - for (int x = 0; x < data.width; x++) { - int pixel = data.getPixel(x, y); - pixelArray[0] = pixel; - raster.setPixel(x, y, pixelArray); + RGB[] rgbs = palette.getRGBs(); + byte[] red = new byte[rgbs.length]; + byte[] green = new byte[rgbs.length]; + byte[] blue = new byte[rgbs.length]; + for (int i = 0; i < rgbs.length; i++) { + RGB rgb = rgbs[i]; + red[i] = (byte) rgb.red; + green[i] = (byte) rgb.green; + blue[i] = (byte) rgb.blue; + } + if (data.transparentPixel != -1) { + colorModel = new IndexColorModel(data.depth, rgbs.length, red, green, blue, data.transparentPixel); + } else { + colorModel = new IndexColorModel(data.depth, rgbs.length, red, green, blue); + } + BufferedImage bufferedImage = new BufferedImage(colorModel, + colorModel.createCompatibleWritableRaster(data.width, data.height), false, null); + WritableRaster raster = bufferedImage.getRaster(); + int[] pixelArray = new int[1]; + for (int y = 0; y < data.height; y++) { + for (int x = 0; x < data.width; x++) { + int pixel = data.getPixel(x, y); + pixelArray[0] = pixel; + raster.setPixel(x, y, pixelArray); + } } + return bufferedImage; } - return bufferedImage; } -} -static ImageData convertToSWT(BufferedImage bufferedImage) { - if (bufferedImage.getColorModel() instanceof DirectColorModel) { - DirectColorModel colorModel = (DirectColorModel)bufferedImage.getColorModel(); - PaletteData palette = new PaletteData(colorModel.getRedMask(), colorModel.getGreenMask(), colorModel.getBlueMask()); - ImageData data = new ImageData(bufferedImage.getWidth(), bufferedImage.getHeight(), colorModel.getPixelSize(), palette); - for (int y = 0; y < data.height; y++) { - for (int x = 0; x < data.width; x++) { - int rgb = bufferedImage.getRGB(x, y); - int pixel = palette.getPixel(new RGB((rgb >> 16) & 0xFF, (rgb >> 8) & 0xFF, rgb & 0xFF)); - data.setPixel(x, y, pixel); - if (colorModel.hasAlpha()) { - data.setAlpha(x, y, (rgb >> 24) & 0xFF); + static ImageData convertToSWT(BufferedImage bufferedImage) { + if (bufferedImage.getColorModel() instanceof DirectColorModel) { + DirectColorModel colorModel = (DirectColorModel) bufferedImage.getColorModel(); + PaletteData palette = new PaletteData(colorModel.getRedMask(), colorModel.getGreenMask(), + colorModel.getBlueMask()); + ImageData data = new ImageData(bufferedImage.getWidth(), bufferedImage.getHeight(), + colorModel.getPixelSize(), palette); + for (int y = 0; y < data.height; y++) { + for (int x = 0; x < data.width; x++) { + int rgb = bufferedImage.getRGB(x, y); + int pixel = palette.getPixel(new RGB((rgb >> 16) & 0xFF, (rgb >> 8) & 0xFF, rgb & 0xFF)); + data.setPixel(x, y, pixel); + if (colorModel.hasAlpha()) { + data.setAlpha(x, y, (rgb >> 24) & 0xFF); + } } } - } - return data; - } else if (bufferedImage.getColorModel() instanceof IndexColorModel) { - IndexColorModel colorModel = (IndexColorModel)bufferedImage.getColorModel(); - int size = colorModel.getMapSize(); - byte[] reds = new byte[size]; - byte[] greens = new byte[size]; - byte[] blues = new byte[size]; - colorModel.getReds(reds); - colorModel.getGreens(greens); - colorModel.getBlues(blues); - RGB[] rgbs = new RGB[size]; - for (int i = 0; i < rgbs.length; i++) { - rgbs[i] = new RGB(reds[i] & 0xFF, greens[i] & 0xFF, blues[i] & 0xFF); - } - PaletteData palette = new PaletteData(rgbs); - ImageData data = new ImageData(bufferedImage.getWidth(), bufferedImage.getHeight(), colorModel.getPixelSize(), palette); - data.transparentPixel = colorModel.getTransparentPixel(); - WritableRaster raster = bufferedImage.getRaster(); - int[] pixelArray = new int[1]; - for (int y = 0; y < data.height; y++) { - for (int x = 0; x < data.width; x++) { - raster.getPixel(x, y, pixelArray); - data.setPixel(x, y, pixelArray[0]); + return data; + } else if (bufferedImage.getColorModel() instanceof IndexColorModel) { + IndexColorModel colorModel = (IndexColorModel) bufferedImage.getColorModel(); + int size = colorModel.getMapSize(); + byte[] reds = new byte[size]; + byte[] greens = new byte[size]; + byte[] blues = new byte[size]; + colorModel.getReds(reds); + colorModel.getGreens(greens); + colorModel.getBlues(blues); + RGB[] rgbs = new RGB[size]; + for (int i = 0; i < rgbs.length; i++) { + rgbs[i] = new RGB(reds[i] & 0xFF, greens[i] & 0xFF, blues[i] & 0xFF); + } + PaletteData palette = new PaletteData(rgbs); + ImageData data = new ImageData(bufferedImage.getWidth(), bufferedImage.getHeight(), + colorModel.getPixelSize(), palette); + data.transparentPixel = colorModel.getTransparentPixel(); + WritableRaster raster = bufferedImage.getRaster(); + int[] pixelArray = new int[1]; + for (int y = 0; y < data.height; y++) { + for (int x = 0; x < data.width; x++) { + raster.getPixel(x, y, pixelArray); + data.setPixel(x, y, pixelArray[0]); + } } + return data; } - return data; + return null; } - return null; -} -static ImageData createSampleImage(Display display) { - Image image = new Image(display, 100, 100); - Rectangle bounds = image.getBounds(); - GC gc = new GC(image); - gc.setBackground(display.getSystemColor(SWT.COLOR_BLUE)); - gc.fillRectangle(bounds); - gc.setBackground(display.getSystemColor(SWT.COLOR_GREEN)); - gc.fillOval(0, 0, bounds.width, bounds.height); - gc.setForeground(display.getSystemColor(SWT.COLOR_RED)); - gc.drawLine(0, 0, bounds.width, bounds.height); - gc.drawLine(bounds.width, 0, 0, bounds.height); - gc.dispose(); - ImageData data = image.getImageData(); - image.dispose(); - return data; -} + static ImageData createSampleImage(Display display) { -public static void main(String[] args) { - Display display = new Display(); - Shell shell = new Shell(display); - shell.setText("SWT Image"); - ImageData data; - if (args.length > 0) { - String fileName = args[0]; - data = new ImageData(fileName); - } else { - data = createSampleImage(display); + ImageGcDrawer imageGcDrawer = (gc, imageWidth, imageHeight) -> { + gc.setBackground(display.getSystemColor(SWT.COLOR_BLUE)); + gc.fillRectangle(0, 0, imageWidth, imageHeight); + gc.setBackground(display.getSystemColor(SWT.COLOR_GREEN)); + gc.fillOval(0, 0, imageWidth, imageHeight); + gc.setForeground(display.getSystemColor(SWT.COLOR_RED)); + gc.drawLine(0, 0, imageWidth, imageHeight); + gc.drawLine(imageWidth, 0, 0, imageHeight); + }; + Image image = new Image(display, imageGcDrawer, 100, 100); + ImageData data = image.getImageData(); + return data; } - final Image swtImage = new Image(display, data); - final BufferedImage awtImage = convertToAWT(data); - final Image swtImage2 = new Image(display, convertToSWT(awtImage)); - shell.addListener(SWT.Paint, e -> { - int y = 10; - if (swtImage != null) { - e.gc.drawImage(swtImage, 10, y); - y += swtImage.getBounds().height + 10; - } - if (swtImage2 != null) { - e.gc.drawImage(swtImage2, 10, y); + + public static void main(String[] args) { + Display display = new Display(); + Shell shell = new Shell(display); + shell.setText("SWT Image"); + ImageData data; + if (args.length > 0) { + String fileName = args[0]; + data = new ImageData(fileName); + } else { + data = createSampleImage(display); } - }); - Frame frame = new Frame() { - private static final long serialVersionUID = 1L; + final Image swtImage = new Image(display, data); + final BufferedImage awtImage = convertToAWT(data); + final Image swtImage2 = new Image(display, convertToSWT(awtImage)); + shell.addListener(SWT.Paint, e -> { + int y = 10; + if (swtImage != null) { + e.gc.drawImage(swtImage, 10, y); + y += swtImage.getBounds().height + 10; + } + if (swtImage2 != null) { + e.gc.drawImage(swtImage2, 10, y); + } + }); + Frame frame = new Frame() { + private static final long serialVersionUID = 1L; - @Override - public void paint(Graphics g) { - Insets insets = getInsets(); - if (awtImage != null) { - g.drawImage(awtImage, 10 + insets.left, 10 + insets.top, null); + @Override + public void paint(Graphics g) { + Insets insets = getInsets(); + if (awtImage != null) { + g.drawImage(awtImage, 10 + insets.left, 10 + insets.top, null); + } } + }; + frame.setTitle("AWT Image"); + shell.setLocation(50, 50); + Rectangle bounds = swtImage.getBounds(); + shell.setSize(bounds.width + 50, bounds.height * 2 + 100); + Point size = shell.getSize(); + Point location = shell.getLocation(); + Insets insets = frame.getInsets(); + frame.setLocation(location.x + size.x + 10, location.y); + frame.setSize(size.x - (insets.left + insets.right), size.y - (insets.top + insets.bottom)); + frame.setVisible(true); + shell.open(); + while (!shell.isDisposed()) { + if (!display.readAndDispatch()) + display.sleep(); } - }; - frame.setTitle("AWT Image"); - shell.setLocation(50, 50); - Rectangle bounds = swtImage.getBounds(); - shell.setSize(bounds.width + 50, bounds.height * 2 + 100); - Point size = shell.getSize(); - Point location = shell.getLocation(); - Insets insets = frame.getInsets(); - frame.setLocation(location.x + size.x + 10, location.y); - frame.setSize(size.x - (insets.left + insets.right), size.y - (insets.top + insets.bottom)); - frame.setVisible(true); - shell.open(); - while (!shell.isDisposed()) { - if (!display.readAndDispatch()) display.sleep(); + if (swtImage != null) + swtImage.dispose(); + if (swtImage2 != null) + swtImage.dispose(); + frame.dispose(); + display.dispose(); + /* + * Note: If you are using JDK 1.3.x, you need to use System.exit(0) at the end + * of your program to exit AWT. This is because in 1.3.x, AWT does not exit when + * the frame is disposed, because the AWT thread is not a daemon. This was fixed + * in JDK 1.4.x with the addition of the AWT Shutdown thread. + */ } - if (swtImage != null) swtImage.dispose(); - if (swtImage2 != null) swtImage.dispose(); - frame.dispose(); - display.dispose(); - /* Note: If you are using JDK 1.3.x, you need to use System.exit(0) at the end of your program to exit AWT. - * This is because in 1.3.x, AWT does not exit when the frame is disposed, because the AWT thread is not a daemon. - * This was fixed in JDK 1.4.x with the addition of the AWT Shutdown thread. - */ -} } diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet162.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet162.java index faf4db49736..80261d0644f 100644 --- a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet162.java +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet162.java @@ -31,65 +31,68 @@ public class Snippet162 { - final static String[] ITEM_NAMES = {"first item", "second", "third", "fourth", "fifth"}; + final static String[] ITEM_NAMES = { "first item", "second", "third", "fourth", "fifth" }; -public static void main (String [] args) { - Display display = new Display (); - final Image checkedImage = getStateImage (display, true); - final Image uncheckedImage = getStateImage (display, false); + public static void main(String[] args) { + Display display = new Display(); + final Image checkedImage = getStateImage(display, true); + final Image uncheckedImage = getStateImage(display, false); - Shell shell = new Shell (display); - shell.setText("Snippet 162"); - shell.setLayout (new FillLayout ()); + Shell shell = new Shell(display); + shell.setText("Snippet 162"); + shell.setLayout(new FillLayout()); - final Table table = new Table (shell, SWT.FULL_SELECTION | SWT.BORDER); - for (int i = 0; i < ITEM_NAMES.length; i++) { - TableItem item = new TableItem (table, SWT.NONE); - item.setText (ITEM_NAMES[i]); - item.setImage (i % 2 == 0 ? checkedImage : uncheckedImage); - } - table.addSelectionListener(widgetDefaultSelectedAdapter(e -> { - TableItem item = (TableItem)e.item; + final Table table = new Table(shell, SWT.FULL_SELECTION | SWT.BORDER); + for (int i = 0; i < ITEM_NAMES.length; i++) { + TableItem item = new TableItem(table, SWT.NONE); + item.setText(ITEM_NAMES[i]); + item.setImage(i % 2 == 0 ? checkedImage : uncheckedImage); + } + table.addSelectionListener(widgetDefaultSelectedAdapter(e -> { + TableItem item = (TableItem) e.item; item.setImage(item.getImage() == checkedImage ? uncheckedImage : checkedImage); })); - table.getAccessible ().addAccessibleControlListener (new AccessibleControlAdapter () { - @Override - public void getRole(AccessibleControlEvent e) { - e.detail = ACC.ROLE_CHECKBUTTON; - } - @Override - public void getState (AccessibleControlEvent e) { - if (e.childID >= 0 && e.childID < table.getItemCount ()) { - TableItem item = table.getItem (e.childID); - if (item.getImage() == checkedImage) { - e.detail |= ACC.STATE_CHECKED; + table.getAccessible().addAccessibleControlListener(new AccessibleControlAdapter() { + @Override + public void getRole(AccessibleControlEvent e) { + e.detail = ACC.ROLE_CHECKBUTTON; + } + + @Override + public void getState(AccessibleControlEvent e) { + if (e.childID >= 0 && e.childID < table.getItemCount()) { + TableItem item = table.getItem(e.childID); + if (item.getImage() == checkedImage) { + e.detail |= ACC.STATE_CHECKED; + } } } - } - }); + }); - shell.pack(); - shell.open (); - while (!shell.isDisposed ()) { - if (!display.readAndDispatch ()) display.sleep (); + shell.pack(); + shell.open(); + while (!shell.isDisposed()) { + if (!display.readAndDispatch()) + display.sleep(); + } + checkedImage.dispose(); + uncheckedImage.dispose(); + display.dispose(); } - checkedImage.dispose (); - uncheckedImage.dispose (); - display.dispose (); -} -static Image getStateImage (Display display, boolean checked) { - Image image = new Image (display, 16, 16); - GC gc = new GC (image); - gc.setBackground (display.getSystemColor (SWT.COLOR_YELLOW)); - gc.fillOval (0, 0, 16, 16); - if (checked) { - gc.setForeground (display.getSystemColor (SWT.COLOR_DARK_GREEN)); - gc.drawLine (0, 0, 16, 16); - gc.drawLine (16, 0, 0, 16); + static Image getStateImage(Display display, boolean checked) { + ImageGcDrawer imageGcDrawer = (gc, imageWidth, imageHeight) -> { + gc.setBackground(display.getSystemColor(SWT.COLOR_YELLOW)); + gc.fillOval(0, 0, imageWidth, imageHeight); + if (checked) { + gc.setForeground(display.getSystemColor(SWT.COLOR_DARK_GREEN)); + gc.drawLine(0, 0, imageWidth, imageHeight); + gc.drawLine(imageWidth, 0, 0, imageHeight); + } + }; + Image image = new Image(display, imageGcDrawer, 16, 16); + + return image; } - gc.dispose (); - return image; -} } \ No newline at end of file diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet165.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet165.java index 0093a4378bc..15bbf24a31e 100644 --- a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet165.java +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet165.java @@ -32,13 +32,14 @@ public class Snippet165 { public static void main (String [] args) { Display display = new Display (); - Image image = new Image(display, 16, 16); - GC gc = new GC(image); - gc.setBackground(display.getSystemColor(SWT.COLOR_BLUE)); - gc.fillRectangle(0, 0, 16, 16); - gc.setBackground(display.getSystemColor(SWT.COLOR_YELLOW)); - gc.fillRectangle(3, 3, 10, 10); - gc.dispose(); + ImageGcDrawer imageGcDrawer = (gc, imageWidth, imageHeight) -> { + gc.setBackground(display.getSystemColor(SWT.COLOR_BLUE)); + gc.fillRectangle(0, 0, 16, 16); + gc.setBackground(display.getSystemColor(SWT.COLOR_YELLOW)); + gc.fillRectangle(3, 3, 10, 10); + }; + Image image = new Image(display, imageGcDrawer, 16, 16); + final Shell shell = new Shell (display); shell.setText("Snippet 165"); shell.setLayout(new GridLayout()); diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet200.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet200.java index 1e45fb06492..93ebeb8fe0b 100644 --- a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet200.java +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet200.java @@ -30,20 +30,21 @@ public class Snippet200 { public static void main(String[] args) { Display display = new Display(); //define a pattern on an image - final Image image = new Image(display, 1000, 1000); - Color blue = display.getSystemColor(SWT.COLOR_BLUE); - Color yellow = display.getSystemColor(SWT.COLOR_YELLOW); - Color white = display.getSystemColor(SWT.COLOR_WHITE); - GC gc = new GC(image); - gc.setBackground(white); - gc.setForeground(yellow); - gc.fillGradientRectangle(0, 0, 1000, 1000, true); - for (int i=-500; i<1000; i+=10) { - gc.setForeground(blue); - gc.drawLine(i, 0, 500 + i, 1000); - gc.drawLine(500 + i, 0, i, 1000); - } - gc.dispose(); + ImageGcDrawer imageGcDrawer = (gc, imageWidth, imageHeight) -> { + Color blue = display.getSystemColor(SWT.COLOR_BLUE); + Color yellow = display.getSystemColor(SWT.COLOR_YELLOW); + Color white = display.getSystemColor(SWT.COLOR_WHITE); + gc.setBackground(white); + gc.setForeground(yellow); + gc.fillGradientRectangle(0, 0, 1000, 1000, true); + for (int i=-500; i<1000; i+=10) { + gc.setForeground(blue); + gc.drawLine(i, 0, 500 + i, 1000); + gc.drawLine(500 + i, 0, i, 1000); + } + }; + final Image image = new Image(display, imageGcDrawer, 1000, 1000); + final Pattern pattern; try { pattern = new Pattern(display, image); diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet205.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet205.java index ea9cfcbf562..3f9745c9ccb 100644 --- a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet205.java +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet205.java @@ -26,20 +26,25 @@ import org.eclipse.swt.graphics.*; import org.eclipse.swt.widgets.*; - public class Snippet205 { public static void main(String[] args) { + Display display = new Display(); final Shell shell = new Shell(display, SWT.SHELL_TRIM | SWT.DOUBLE_BUFFERED); shell.setText("Embedding objects in text"); - final Image[] images = {new Image(display, 32, 32), new Image(display, 20, 40), new Image(display, 40, 20)}; + final Image[] images = new Image[3]; + final Rectangle[] rects = { new Rectangle(0, 0, 32, 32), new Rectangle(0, 0, 20, 40), + new Rectangle(0, 0, 40, 20) }; int[] colors = {SWT.COLOR_BLUE, SWT.COLOR_MAGENTA, SWT.COLOR_GREEN}; for (int i = 0; i < images.length; i++) { - GC gc = new GC(images[i]); - gc.setBackground(display.getSystemColor(colors[i])); - gc.fillRectangle(images[i].getBounds()); - gc.dispose(); + final int index = i; + + ImageGcDrawer imageGcDrawer = (gc, imageWidth, imageHeight) -> { + gc.setBackground(display.getSystemColor(colors[index])); + gc.fillRectangle(rects[index]); + }; + images[i] = new Image(display, imageGcDrawer, rects[i].width, rects[i].height); } final Button button = new Button(shell, SWT.PUSH); @@ -50,9 +55,8 @@ public static void main(String[] args) { final TextLayout layout = new TextLayout(display); layout.setText(text); for (int i = 0; i < images.length; i++) { - Rectangle bounds = images[i].getBounds(); TextStyle imageStyle = new TextStyle(null, null, null); - imageStyle.metrics = new GlyphMetrics(bounds.height, 0, bounds.width); + imageStyle.metrics = new GlyphMetrics(rects[i].height, 0, rects[i].width); layout.setStyle(imageStyle, imageOffsets[i], imageOffsets[i]); } Rectangle bounds = button.getBounds(); @@ -91,4 +95,4 @@ public static void main(String[] args) { } display.dispose(); } -} +} \ No newline at end of file diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet207.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet207.java index c16a2cbe2f0..185c452fa0c 100644 --- a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet207.java +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet207.java @@ -28,16 +28,16 @@ public class Snippet207 { public static void main(String[] args) { final Display display = new Display(); - final Image image = new Image(display, 110, 60); - GC gc = new GC(image); - Font font = new Font(display, "Times", 30, SWT.BOLD); - gc.setFont(font); - gc.setBackground(display.getSystemColor(SWT.COLOR_RED)); - gc.fillRectangle(0, 0, 110, 60); - gc.setForeground(display.getSystemColor(SWT.COLOR_WHITE)); - gc.drawText("SWT", 10, 10, true); - font.dispose(); - gc.dispose(); + ImageGcDrawer imageGcDrawer = (gc, imageWidth, imageHeight) -> { + Font font = new Font(display, "Times", 30, SWT.BOLD); + gc.setFont(font); + gc.setBackground(display.getSystemColor(SWT.COLOR_RED)); + gc.fillRectangle(0, 0, 110, 60); + gc.setForeground(display.getSystemColor(SWT.COLOR_WHITE)); + gc.drawText("SWT", 10, 10, true); + font.dispose(); + }; + final Image image = new Image(display, imageGcDrawer, 110, 60); final Rectangle rect = image.getBounds(); Shell shell = new Shell(display); diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet214.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet214.java index b9b7c3efcf7..fb3c413bda9 100644 --- a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet214.java +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet214.java @@ -46,13 +46,13 @@ public static void main(String [] args) { button.setText ("Button " + i); } shell.addListener (SWT.Resize, event -> { + ImageGcDrawer imageGcDrawer = (gc, imageWidth, imageHeight) -> { + gc.setForeground (display.getSystemColor (SWT.COLOR_WHITE)); + gc.setBackground (display.getSystemColor (SWT.COLOR_BLUE)); + gc.fillGradientRectangle (0, 0, imageWidth, imageHeight, false); + }; Rectangle rect = shell.getClientArea (); - Image newImage = new Image (display, Math.max (1, rect.width), 1); - GC gc = new GC (newImage); - gc.setForeground (display.getSystemColor (SWT.COLOR_WHITE)); - gc.setBackground (display.getSystemColor (SWT.COLOR_BLUE)); - gc.fillGradientRectangle (rect.x, rect.y, rect.width, 1, false); - gc.dispose (); + Image newImage = new Image (display, imageGcDrawer, Math.max (1, rect.width), 1); shell.setBackgroundImage (newImage); if (oldImage != null) oldImage.dispose (); oldImage = newImage; diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet218.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet218.java index 47357565a3c..ced879c627a 100644 --- a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet218.java +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet218.java @@ -50,12 +50,12 @@ public static void main(String [] args) { styledText.setForeground(display.getSystemColor (SWT.COLOR_BLUE)); styledText.addListener (SWT.Resize, event -> { Rectangle rect = styledText.getClientArea (); - Image newImage = new Image (display, 1, Math.max (1, rect.height)); - GC gc = new GC (newImage); - gc.setForeground (display.getSystemColor (SWT.COLOR_WHITE)); - gc.setBackground (display.getSystemColor (SWT.COLOR_YELLOW)); - gc.fillGradientRectangle (rect.x, rect.y, 1, rect.height, true); - gc.dispose (); + ImageGcDrawer imageGcDrawer = (gc, imageWidth, imageHeight) -> { + gc.setForeground (display.getSystemColor (SWT.COLOR_WHITE)); + gc.setBackground (display.getSystemColor (SWT.COLOR_YELLOW)); + gc.fillGradientRectangle (rect.x, rect.y, 1, rect.height, true); + }; + Image newImage = new Image (display, imageGcDrawer, 1, Math.max (1, rect.height)); styledText.setBackgroundImage (newImage); if (oldImage != null) oldImage.dispose (); oldImage = newImage; diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet220.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet220.java index 25d5ee7db0d..49ae642f9c6 100644 --- a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet220.java +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet220.java @@ -35,13 +35,13 @@ public static void main(String [] args) { Shell shell = new Shell(display); shell.setText("Snippet 220"); shell.setBounds(10, 10, 350, 200); - Image xImage = new Image (display, 16, 16); - GC gc = new GC(xImage); - gc.setForeground(display.getSystemColor(SWT.COLOR_RED)); - gc.drawLine(1, 1, 14, 14); - gc.drawLine(1, 14, 14, 1); - gc.drawOval(2, 2, 11, 11); - gc.dispose(); + ImageGcDrawer imageGcDrawer = (gc, imageWidth, imageHeight) -> { + gc.setForeground(display.getSystemColor(SWT.COLOR_RED)); + gc.drawLine(1, 1, 14, 14); + gc.drawLine(1, 14, 14, 1); + gc.drawOval(2, 2, 11, 11); + }; + Image xImage = new Image (display, imageGcDrawer, 16, 16); final int IMAGE_MARGIN = 2; final Tree tree = new Tree(shell, SWT.CHECK); tree.setBounds(10, 10, 300, 150); diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet223.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet223.java index c74b7dd98ca..67138c2a2a4 100644 --- a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet223.java +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet223.java @@ -22,9 +22,9 @@ */ import org.eclipse.swt.*; +import org.eclipse.swt.graphics.*; import org.eclipse.swt.layout.*; import org.eclipse.swt.widgets.*; -import org.eclipse.swt.graphics.*; public class Snippet223 { diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet246.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet246.java index 331228deac4..a2db7063b31 100644 --- a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet246.java +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet246.java @@ -37,7 +37,7 @@ public static void main(String[] args) { String executionPath = System.getProperty("user.dir"); Label label = new Label(shell, SWT.WRAP); - label.setText("File created as: " + executionPath.replace("\\", "/")+"/swt.png"); + label.setText("File created as: " + executionPath.replace("\\", "/") + "/swt.png"); shell.pack(); shell.open(); while (!shell.isDisposed()) { @@ -49,18 +49,19 @@ public static void main(String[] args) { private static void createImage() { Font font = new Font(display, "Comic Sans MS", 48, SWT.BOLD); - Image image = new Image(display, 174, 96); - GC gc = new GC(image); - gc.setBackground(display.getSystemColor(SWT.COLOR_WHITE)); - gc.fillRectangle(image.getBounds()); - gc.setFont(font); - gc.setForeground(display.getSystemColor(SWT.COLOR_RED)); - gc.drawString("S", 3, 10); - gc.setForeground(display.getSystemColor(SWT.COLOR_GREEN)); - gc.drawString("W", 50, 10); - gc.setForeground(display.getSystemColor(SWT.COLOR_BLUE)); - gc.drawString("T", 124, 10); - gc.dispose(); + + ImageGcDrawer imageGcDrawer = (gc, imageWidth, imageHeight) -> { + gc.setBackground(display.getSystemColor(SWT.COLOR_WHITE)); + gc.fillRectangle(new Rectangle(0, 0, 174, 96)); + gc.setFont(font); + gc.setForeground(display.getSystemColor(SWT.COLOR_RED)); + gc.drawString("S", 3, 10); + gc.setForeground(display.getSystemColor(SWT.COLOR_GREEN)); + gc.drawString("W", 50, 10); + gc.setForeground(display.getSystemColor(SWT.COLOR_BLUE)); + gc.drawString("T", 124, 10); + }; + Image image = new Image(display, imageGcDrawer, 174, 96); ImageLoader loader = new ImageLoader(); loader.data = new ImageData[] { image.getImageData() }; diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet275.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet275.java index b99c8d0a0ca..71d895afd0d 100644 --- a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet275.java +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet275.java @@ -25,44 +25,46 @@ public class Snippet275 { -static String value; -public static void main (String[] args) { - final int INTERVAL = 888; - final Display display = new Display (); - final Image image = new Image (display, 750, 750); - GC gc = new GC (image); - gc.setBackground (display.getSystemColor (SWT.COLOR_RED)); - gc.fillRectangle (image.getBounds ()); - gc.dispose (); + static String value; - Shell shell = new Shell (display); - shell.setText("Snippet 275"); - shell.setBounds (10, 10, 790, 790); - final Canvas canvas = new Canvas (shell, SWT.NONE); - canvas.setBounds (10, 10, 750, 750); - canvas.addListener (SWT.Paint, event -> { - value = String.valueOf (System.currentTimeMillis ()); - event.gc.drawImage (image, 0, 0); - event.gc.drawString (value, 10, 10, true); - }); - display.timerExec (INTERVAL, new Runnable () { - @Override - public void run () { - if (canvas.isDisposed ()) return; - // canvas.redraw (); // <-- bad, damages more than is needed - GC gc = new GC (canvas); - Point extent = gc.stringExtent (value + '0'); - gc.dispose (); - canvas.redraw (10, 10, extent.x, extent.y, false); - display.timerExec (INTERVAL, this); + public static void main(String[] args) { + final int INTERVAL = 888; + final Display display = new Display(); + ImageGcDrawer imageGcDrawer = (gc, imageWidth, imageHeight) -> { + gc.setBackground(display.getSystemColor(SWT.COLOR_RED)); + gc.fillRectangle(new Rectangle(0, 0, imageWidth, imageHeight)); + }; + final Image image = new Image(display, imageGcDrawer, 750, 750); + Shell shell = new Shell(display); + shell.setText("Snippet 275"); + shell.setBounds(10, 10, 790, 790); + final Canvas canvas = new Canvas(shell, SWT.NONE); + canvas.setBounds(10, 10, 750, 750); + canvas.addListener(SWT.Paint, event -> { + value = String.valueOf(System.currentTimeMillis()); + event.gc.drawImage(image, 0, 0); + event.gc.drawString(value, 10, 10, true); + }); + display.timerExec(INTERVAL, new Runnable() { + @Override + public void run() { + if (canvas.isDisposed()) + return; + // canvas.redraw (); // <-- bad, damages more than is needed + GC gc = new GC(canvas); + Point extent = gc.stringExtent(value + '0'); + gc.dispose(); + canvas.redraw(10, 10, extent.x, extent.y, false); + display.timerExec(INTERVAL, this); + } + }); + shell.open(); + while (!shell.isDisposed()) { + if (!display.readAndDispatch()) + display.sleep(); } - }); - shell.open (); - while (!shell.isDisposed ()){ - if (!display.readAndDispatch ()) display.sleep (); + image.dispose(); + display.dispose(); } - image.dispose (); - display.dispose (); -} -} +} \ No newline at end of file diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet288.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet288.java index 4065a003fb9..23d25d9955f 100644 --- a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet288.java +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet288.java @@ -87,65 +87,56 @@ private static void loadAllImages(String directory, String[] filenames) throws S imageDataArray = new ImageData[numItems][]; image = new Image[numItems][]; for (int i = 0; i < numItems; i++) { - loader[i] = new ImageLoader(); - int fullWidth = loader[i].logicalScreenWidth; - int fullHeight = loader[i].logicalScreenHeight; - imageDataArray[i] = loader[i].load(directory + File.separator + filenames[i]); - int numFramesOfAnimation = imageDataArray[i].length; - image[i] = new Image[numFramesOfAnimation]; - for (int j = 0; j < numFramesOfAnimation; j++) { - if (j == 0) { - //for the first frame of animation, just draw the first frame - image[i][j] = new Image(display, imageDataArray[i][j]); - fullWidth = imageDataArray[i][j].width; - fullHeight = imageDataArray[i][j].height; - } - else { - //after the first frame of animation, draw the background or previous frame first, then the new image data - image[i][j] = new Image(display, fullWidth, fullHeight); - GC gc = new GC(image[i][j]); - gc.setBackground(shellBackground); - gc.fillRectangle(0, 0, fullWidth, fullHeight); - ImageData imageData = imageDataArray[i][j]; - switch (imageData.disposalMethod) { - case SWT.DM_FILL_BACKGROUND: - /* Fill with the background color before drawing. */ - Color bgColor = null; - if (useGIFBackground && loader[i].backgroundPixel != -1) { - bgColor = new Color(imageData.palette.getRGB(loader[i].backgroundPixel)); - } - gc.setBackground(bgColor != null ? bgColor : shellBackground); - gc.fillRectangle(imageData.x, imageData.y, imageData.width, imageData.height); - break; - default: - /* Restore the previous image before drawing. */ - gc.drawImage( - image[i][j-1], - 0, - 0, - fullWidth, - fullHeight, - 0, - 0, - fullWidth, - fullHeight); - break; - } - Image newFrame = new Image(display, imageData); - gc.drawImage(newFrame, - 0, - 0, - imageData.width, - imageData.height, - imageData.x, - imageData.y, - imageData.width, - imageData.height); - newFrame.dispose(); - gc.dispose(); - } - } + loader[i] = new ImageLoader(); + int fullWidth = loader[i].logicalScreenWidth; + int fullHeight = loader[i].logicalScreenHeight; + imageDataArray[i] = loader[i].load(directory + File.separator + filenames[i]); + int numFramesOfAnimation = imageDataArray[i].length; + image[i] = new Image[numFramesOfAnimation]; + + for (int j = 0; j < numFramesOfAnimation; j++) { + final int frameIndex = j; + final int imageIndex = i; + + if (frameIndex == 0) { + image[imageIndex][frameIndex] = new Image(display, imageDataArray[imageIndex][frameIndex]); + fullWidth = imageDataArray[imageIndex][frameIndex].width; + fullHeight = imageDataArray[imageIndex][frameIndex].height; + } else { + ImageGcDrawer imageGcDrawer = (gc, imageWidth, imageHeight) -> { + gc.setBackground(shellBackground); + gc.fillRectangle(0, 0, imageWidth, imageWidth); + ImageData imageData = imageDataArray[imageIndex][frameIndex]; + + switch (imageData.disposalMethod) { + case SWT.DM_FILL_BACKGROUND: + Color bgColor = null; + if (useGIFBackground && loader[imageIndex].backgroundPixel != -1) { + bgColor = new Color(imageData.palette.getRGB(loader[imageIndex].backgroundPixel)); + } + gc.setBackground(bgColor != null ? bgColor : shellBackground); + gc.fillRectangle(imageData.x, imageData.y, imageData.width, imageData.height); + break; + default: + gc.drawImage( + image[imageIndex][frameIndex - 1], + 0, 0, imageWidth, imageWidth, + 0, 0, imageWidth, imageWidth); + break; + } + + Image newFrame = new Image(display, imageData); + gc.drawImage(newFrame, + 0, 0, imageData.width, imageData.height, + imageData.x, imageData.y, imageData.width, imageData.height); + newFrame.dispose(); + }; + + image[imageIndex][frameIndex] = new Image(display, imageGcDrawer, fullWidth, fullHeight); + } + } } + } private static void startAnimationThreads() { diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet34.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet34.java index 83d4224ab21..7b94db40c29 100644 --- a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet34.java +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet34.java @@ -27,12 +27,12 @@ public class Snippet34 { public static void main (String[] args) { Display display = new Display(); - Image image = new Image (display, 16, 16); Color color = display.getSystemColor (SWT.COLOR_RED); - GC gc = new GC (image); - gc.setBackground (color); - gc.fillRectangle (image.getBounds ()); - gc.dispose (); + ImageGcDrawer imageGcDrawer = (gc, imageWidth, imageHeight) -> { + gc.setBackground (color); + gc.fillRectangle (new Rectangle(0,0,imageWidth, imageHeight)); + }; + Image image = new Image (display, imageGcDrawer, 16, 16); Shell shell = new Shell (display); shell.setText("Snippet 34"); Label label = new Label (shell, SWT.BORDER); diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet349.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet349.java index 2073b17e82e..cabd852a117 100644 --- a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet349.java +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet349.java @@ -146,12 +146,12 @@ public static void main(String [] args) { } static Image createImage(Display display, int width, int height) { - Image result = new Image(display, width, height); - GC gc = new GC(result); - for (int x = -height; x < width; x += 4) { - gc.drawLine(x, 0, x + height, height); - } - gc.dispose(); + ImageGcDrawer imageGcDrawer = (gc, imageWidth, imageHeight) -> { + for (int x = -height; x < width; x += 4) { + gc.drawLine(x, 0, x + height, height); + } + }; + Image result = new Image(display, imageGcDrawer, width, height); return result; } } diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet36.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet36.java index ed8a88da297..a1237156693 100644 --- a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet36.java +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet36.java @@ -12,7 +12,6 @@ * IBM Corporation - initial API and implementation *******************************************************************************/ package org.eclipse.swt.snippets; - /* * ToolBar example snippet: create a flat tool bar (images) * @@ -27,12 +26,13 @@ public class Snippet36 { public static void main (String [] args) { Display display = new Display(); - Image image = new Image (display, 16, 16); Color color = display.getSystemColor (SWT.COLOR_RED); - GC gc = new GC (image); - gc.setBackground (color); - gc.fillRectangle (image.getBounds ()); - gc.dispose (); + ImageGcDrawer imageGcDrawer = (gc, imageWidth, imageHeight) -> { + gc.setBackground(color); + gc.fillRectangle(0, 0, imageWidth, imageHeight); + + }; + Image image = new Image(display, imageGcDrawer, 16, 16); Shell shell = new Shell (display); shell.setText("Snippet 36"); ToolBar toolBar = new ToolBar (shell, SWT.FLAT | SWT.BORDER); @@ -52,4 +52,4 @@ public static void main (String [] args) { display.dispose (); } -} +} \ No newline at end of file diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet365.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet365.java index fda3bc3fe64..21b079231c4 100644 --- a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet365.java +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet365.java @@ -89,11 +89,13 @@ public static void main(String[] args) { // Gradient background for Shell shell.addListener(SWT.Resize, event -> { Rectangle rect = shell.getClientArea(); - Image newImage = new Image(display, Math.max(1, rect.width), 1); + ImageGcDrawer imageGcDrawer = (gc, imageWidth, imageHeight) -> { + gc.setForeground(display.getSystemColor(SWT.COLOR_BLUE)); + gc.setBackground(display.getSystemColor(SWT.COLOR_GREEN)); + gc.fillGradientRectangle(0, 0, imageWidth, imageHeight, false); + }; + Image newImage = new Image(display, imageGcDrawer, Math.max(1, rect.width), 1); GC gc = new GC(newImage); - gc.setForeground(display.getSystemColor(SWT.COLOR_BLUE)); - gc.setBackground(display.getSystemColor(SWT.COLOR_GREEN)); - gc.fillGradientRectangle(rect.x, rect.y, rect.width, 1, false); gc.dispose(); shell.setBackgroundImage(newImage); if (oldImage != null) @@ -457,13 +459,12 @@ public static void main(String[] args) { private static Image getBackgroundImage(final Display display) { if (newImage == null) { - Rectangle rect = new Rectangle(0, 0, 115, 5); - newImage = new Image(display, Math.max(1, rect.width), 1); - GC gc = new GC(newImage); - gc.setForeground(display.getSystemColor(SWT.COLOR_WHITE)); - gc.setBackground(display.getSystemColor(SWT.COLOR_RED)); - gc.fillGradientRectangle(rect.x, rect.y, rect.width, 1, false); - gc.dispose(); + ImageGcDrawer imageGcDrawer = (gc, imageWidth, imageHeight) -> { + gc.setForeground(display.getSystemColor(SWT.COLOR_WHITE)); + gc.setBackground(display.getSystemColor(SWT.COLOR_RED)); + gc.fillGradientRectangle(0, 0, imageWidth, imageHeight, false); + }; + newImage = new Image(display, imageGcDrawer, Math.max(1, 115), 1); } return newImage; } diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet367.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet367.java index 54b53dc1d39..9b8539047d3 100644 --- a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet367.java +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet367.java @@ -138,13 +138,16 @@ public int getGcStyle() { 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 (); - } + final ImageGcDrawer customImageGcDrawer = (gc, imageWidth, imageHeight) -> paintImage (gc, new Point(imageWidth, imageHeight)); + new ImageGcDrawer() { + + @Override + public void drawOn(GC gc, int imageWidth, int imageHeight) { + paintImage (gc, new Point(imageWidth, imageHeight)); + } + }; + + Image image = new Image(display, customImageGcDrawer, size.x, size.y); Label imageLabel = new Label (shell, SWT.NONE); imageLabel.setImage (image); imageLabel.setLayoutData (new GridData (SWT.BEGINNING, SWT.BEGINNING, false, false, 2, 1)); @@ -154,15 +157,11 @@ public int getGcStyle() { 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 (); - } + + final ImageGcDrawer customImageGcDrawer1 = (gc, imageWidth, imageHeight) -> paintImage(gc, new Point(imageWidth, imageHeight)); + Image temp = new Image(display, customImageGcDrawer1, size.x * zoom / 100, size.y * zoom / 100); + return temp.getImageData(); + })); imageLabel.setLayoutData (new GridData (SWT.BEGINNING, SWT.BEGINNING, false, false, 2, 1)); @@ -171,15 +170,9 @@ public int getGcStyle() { 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 (); - } + final ImageGcDrawer customImageGcDrawer1 = (gc, imageWidth, imageHeight) -> paintImage2 (gc, new Point(imageWidth, imageHeight), zoom / 100); + Image temp = new Image (display, customImageGcDrawer1, size.x * zoom / 100, size.y * zoom / 100); + return temp.getImageData(); })); imageLabel.setLayoutData (new GridData (SWT.BEGINNING, SWT.BEGINNING, false, false, 2, 1)); diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet387.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet387.java index d6e61cea1ac..4338e9fa6f8 100644 --- a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet387.java +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet387.java @@ -88,15 +88,10 @@ private static void activateMonitorSpecificScaling() { } private static Image createStaticImage(Display display, String text, boolean disposeGC) { - Image staticImage = new Image(display, IMAGE_WIDTH, IMAGE_HEIGHT); - GC imageGC = new GC(staticImage); - try { - drawImageContent(imageGC, text, IMAGE_WIDTH, IMAGE_HEIGHT); - } finally { - if (disposeGC) { - imageGC.dispose(); - } - } + ImageGcDrawer imageGcDrawer = (gc, imageWidth, imageHeight) -> { + drawImageContent(gc, text, imageWidth, imageHeight); + }; + Image staticImage = new Image(display, imageGcDrawer, IMAGE_WIDTH, IMAGE_HEIGHT); return staticImage; } diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet43.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet43.java index 23574b9baef..3450125a92a 100644 --- a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet43.java +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet43.java @@ -32,14 +32,14 @@ public static void main (String [] args) { Caret caret = new Caret (shell, SWT.NONE); Color white = display.getSystemColor (SWT.COLOR_WHITE); Color black = display.getSystemColor (SWT.COLOR_BLACK); - final Image image = new Image (display, 20, 20); - GC gc = new GC (image); - gc.setBackground (black); - gc.fillRectangle (0, 0, 20, 20); - gc.setForeground (white); - gc.drawLine (0, 0, 19, 19); - gc.drawLine (19, 0, 0, 19); - gc.dispose (); + ImageGcDrawer imageGcDrawer = (gc, imageWidth, imageHeight) -> { + gc.setBackground (black); + gc.fillRectangle (0, 0, 20, 20); + gc.setForeground (white); + gc.drawLine (0, 0, 19, 19); + gc.drawLine (19, 0, 0, 19); + }; + final Image image = new Image (display, imageGcDrawer, 20, 20); caret.setLocation (10, 10); caret.setImage (image); caret.setVisible (true); diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet47.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet47.java index 465733c4233..e7dc0976d0a 100644 --- a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet47.java +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet47.java @@ -12,7 +12,6 @@ * IBM Corporation - initial API and implementation *******************************************************************************/ package org.eclipse.swt.snippets; - /* * ToolBar example snippet: create tool bar (normal, hot and disabled images) * @@ -30,26 +29,26 @@ public static void main (String [] args) { Shell shell = new Shell (display); shell.setText("Snippet 47"); - Image image = new Image (display, 20, 20); - Color color = display.getSystemColor (SWT.COLOR_BLUE); - GC gc = new GC (image); - gc.setBackground (color); - gc.fillRectangle (image.getBounds ()); - gc.dispose (); + ImageGcDrawer imageGcDrawer = (gc, imageWidth, imageHeight) -> { + Color color = display.getSystemColor(SWT.COLOR_BLUE); + gc.setBackground(color); + gc.fillRectangle(new Rectangle(0, 0, 20, 20)); + }; + Image image = new Image(display, imageGcDrawer, 20, 20); - Image disabledImage = new Image (display, 20, 20); - color = display.getSystemColor (SWT.COLOR_GREEN); - gc = new GC (disabledImage); - gc.setBackground (color); - gc.fillRectangle (disabledImage.getBounds ()); - gc.dispose (); + imageGcDrawer = (gc, imageWidth, imageHeight) -> { + Color color = display.getSystemColor(SWT.COLOR_GREEN); + gc.setBackground(color); + gc.fillRectangle(new Rectangle(0, 0, 20, 20)); + }; + Image disabledImage = new Image(display, imageGcDrawer, 20, 20); - Image hotImage = new Image (display, 20, 20); - color = display.getSystemColor (SWT.COLOR_RED); - gc = new GC (hotImage); - gc.setBackground (color); - gc.fillRectangle (hotImage.getBounds ()); - gc.dispose (); + imageGcDrawer = (gc, imageWidth, imageHeight) -> { + Color color = display.getSystemColor(SWT.COLOR_RED); + gc.setBackground(color); + gc.fillRectangle(new Rectangle(0, 0, 20, 20)); + }; + Image hotImage = new Image(display, imageGcDrawer, 20, 20); ToolBar bar = new ToolBar (shell, SWT.BORDER | SWT.FLAT); Rectangle clientArea = shell.getClientArea (); @@ -71,4 +70,4 @@ public static void main (String [] args) { hotImage.dispose (); display.dispose (); } -} +} \ No newline at end of file diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet48.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet48.java index 82791711ba5..6dd2fc41b94 100644 --- a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet48.java +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet48.java @@ -40,13 +40,13 @@ public static void main (String [] args) { } 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 (); + ImageGcDrawer imageGcDrawer = (gc, imageWidth, imageHeight) -> { + gc.fillRectangle (0, 0, width, height); + gc.drawLine (0, 0, width, height); + gc.drawLine (0, height, width, 0); + gc.drawText ("Default Image", 10, 10); + }; + originalImage = new Image (display, imageGcDrawer, width, height); } final Image image = originalImage; final Point origin = new Point (0, 0); diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet7.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet7.java index 22aeec70bb8..0dceabb0fea 100644 --- a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet7.java +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet7.java @@ -12,7 +12,6 @@ * IBM Corporation - initial API and implementation *******************************************************************************/ package org.eclipse.swt.snippets; - /* * example snippet: create a table (lazy) * @@ -28,11 +27,11 @@ public class Snippet7 { public static void main (String [] args) { final Display display = new Display (); - final Image image = new Image (display, 16, 16); - GC gc = new GC (image); - gc.setBackground (display.getSystemColor (SWT.COLOR_RED)); - gc.fillRectangle (image.getBounds ()); - gc.dispose (); + ImageGcDrawer imageGcDrawer = (gc, imageWidth, imageHeight) -> { + gc.setBackground(display.getSystemColor(SWT.COLOR_RED)); + gc.fillRectangle(new Rectangle(0, 0, imageWidth, imageHeight)); + }; + final Image image = new Image(display, imageGcDrawer, 16, 16); final Shell shell = new Shell (display); shell.setText ("Lazy Table"); shell.setLayout (new FillLayout ()); @@ -62,4 +61,4 @@ public void run () { image.dispose (); display.dispose (); } -} +} \ No newline at end of file diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet70.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet70.java index 56b8c3c9429..47ccf5f40ac 100644 --- a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet70.java +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet70.java @@ -31,17 +31,17 @@ public static void main (String [] args) { Color white = display.getSystemColor (SWT.COLOR_WHITE); Color black = display.getSystemColor (SWT.COLOR_BLACK); - Image image = new Image (display, 20, 20); - GC gc = new GC (image); - gc.setBackground (red); - gc.fillRectangle (5, 5, 10, 10); - gc.dispose (); + ImageGcDrawer imageGcDrawer = (gc, imageWidth, imageHeight) -> { + gc.setBackground (red); + gc.fillRectangle (5, 5, 10, 10); + }; + Image image = new Image (display, imageGcDrawer, 20, 20); ImageData imageData = image.getImageData (); PaletteData palette = new PaletteData (new RGB (0, 0, 0),new RGB (0xFF, 0xFF, 0xFF)); ImageData maskData = new ImageData (20, 20, 1, palette); Image mask = new Image (display, maskData); - gc = new GC (mask); + GC gc = new GC (mask); gc.setBackground (black); gc.fillRectangle (0, 0, 20, 20); gc.setBackground (white); diff --git a/tests/org.eclipse.swt.tests/ManualTests/org/eclipse/swt/tests/manual/Snippet36.java b/tests/org.eclipse.swt.tests/ManualTests/org/eclipse/swt/tests/manual/Snippet36.java index 96424447873..f7b0ed848c2 100644 --- a/tests/org.eclipse.swt.tests/ManualTests/org/eclipse/swt/tests/manual/Snippet36.java +++ b/tests/org.eclipse.swt.tests/ManualTests/org/eclipse/swt/tests/manual/Snippet36.java @@ -8,8 +8,8 @@ */ import org.eclipse.swt.SWT; import org.eclipse.swt.graphics.Color; -import org.eclipse.swt.graphics.GC; import org.eclipse.swt.graphics.Image; +import org.eclipse.swt.graphics.ImageGcDrawer; import org.eclipse.swt.graphics.Rectangle; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Shell; @@ -20,17 +20,19 @@ public class Snippet36 { public static void main (String [] args) { Display display = new Display(); - Image image1 = new Image (display, 16, 16); + Color color = display.getSystemColor (SWT.COLOR_RED); - GC gc = new GC (image1); - gc.setBackground (color); - gc.fillRectangle (image1.getBounds ()); - gc.dispose (); - Image image2 = new Image (display, 200, 200); - gc = new GC (image2); - gc.setBackground (color); - gc.fillRectangle (image2.getBounds ()); - gc.dispose (); + ImageGcDrawer imageGcDrawer = (gc, imageWidth, imageHeight) -> { + gc.setBackground (color); + gc.fillRectangle (new Rectangle(0,0,imageWidth, imageHeight)); + }; + Image image1 = new Image (display, imageGcDrawer, 16, 16); + + imageGcDrawer = (gc, imageWidth, imageHeight) -> { + gc.setBackground (color); + gc.fillRectangle (new Rectangle(0,0,imageWidth, imageHeight)); + }; + Image image2 = new Image (display, imageGcDrawer, 200, 200); Shell shell = new Shell (display); shell.setText("Snippet 36"); ToolBar toolBar = new ToolBar (shell, SWT.FLAT | SWT.BORDER);