|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright (c) 2025 Yatta Solutions |
| 3 | + * |
| 4 | + * This program and the accompanying materials |
| 5 | + * are made available under the terms of the Eclipse Public License 2.0 |
| 6 | + * which accompanies this distribution, and is available at |
| 7 | + * https://www.eclipse.org/legal/epl-2.0/ |
| 8 | + * |
| 9 | + * SPDX-License-Identifier: EPL-2.0 |
| 10 | + * |
| 11 | + * Contributors: |
| 12 | + * Yatta Solutions - initial API and implementation |
| 13 | + *******************************************************************************/ |
| 14 | +package org.eclipse.swt.snippets; |
| 15 | + |
| 16 | +import org.eclipse.swt.*; |
| 17 | +import org.eclipse.swt.graphics.*; |
| 18 | +import org.eclipse.swt.widgets.*; |
| 19 | + |
| 20 | +/** |
| 21 | + * Demonstration how to custom draw images |
| 22 | + * <p> |
| 23 | + * Snippet to show how to draw on an image loaded from the file system which has |
| 24 | + * multiple variants for different zoom levels. Mainly relevant on Windows, as |
| 25 | + * it uses the Windows-only monitor-specific scaling mode. |
| 26 | + * <p> |
| 27 | + * For a list of all SWT example snippets see |
| 28 | + * http://www.eclipse.org/swt/snippets/ |
| 29 | + * </p> |
| 30 | + */ |
| 31 | +public class Snippet385 { |
| 32 | + |
| 33 | + public static void main(String[] args) { |
| 34 | + System.setProperty("swt.autoScale.updateOnRuntime", "true"); |
| 35 | + Display display = new Display(); |
| 36 | + Shell shell = new Shell(display); |
| 37 | + shell.setText("Watermark over Image using GC"); |
| 38 | + shell.setSize(1200, 900); |
| 39 | + |
| 40 | + final ImageFileNameProvider filenameProvider = zoom -> { |
| 41 | + String path = null; |
| 42 | + switch (zoom) { |
| 43 | + case 150: |
| 44 | + path = "resources/Snippet385/red.jpeg"; |
| 45 | + break; |
| 46 | + case 100: |
| 47 | + path = "resources/Snippet385/black.jpg"; |
| 48 | + break; |
| 49 | + default: |
| 50 | + path = "resources/Snippet385/black.jpg"; |
| 51 | + } |
| 52 | + return path; |
| 53 | + }; |
| 54 | + Image loadedImage = new Image(display, filenameProvider); |
| 55 | + |
| 56 | + int width = loadedImage.getBounds().width; |
| 57 | + int height = loadedImage.getBounds().height; |
| 58 | + |
| 59 | + Image composedImage = new Image(display, (gc, iX, iY) -> { |
| 60 | + gc.drawImage(loadedImage, 0, 0); |
| 61 | + |
| 62 | + gc.setForeground(display.getSystemColor(SWT.COLOR_WHITE)); |
| 63 | + Font font = new Font(display, "Arial", 24, SWT.BOLD); |
| 64 | + gc.setFont(font); |
| 65 | + gc.setAlpha(128); // semi-transparent |
| 66 | + String watermark = "WATERMARK"; |
| 67 | + Point extent = gc.textExtent(watermark); |
| 68 | + int x = (width - extent.x) / 2; |
| 69 | + int y = (height - extent.y) / 2; |
| 70 | + gc.drawText(watermark, x, y, SWT.DRAW_TRANSPARENT); |
| 71 | + font.dispose(); |
| 72 | + }, width, height); |
| 73 | + |
| 74 | + shell.addPaintListener(e -> { |
| 75 | + e.gc.drawImage(composedImage, 0, 0); |
| 76 | + }); |
| 77 | + |
| 78 | + shell.open(); |
| 79 | + while (!shell.isDisposed()) { |
| 80 | + if (!display.readAndDispatch()) |
| 81 | + display.sleep(); |
| 82 | + } |
| 83 | + |
| 84 | + loadedImage.dispose(); |
| 85 | + composedImage.dispose(); |
| 86 | + display.dispose(); |
| 87 | + } |
| 88 | +} |
0 commit comments