1 /*******************************************************************************
2  * Copyright (c) 2012, 2016 IBM Corporation and others.
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  *     IBM Corporation - initial API and implementation
13  *******************************************************************************/
14 package org.eclipse.swt.snippets;
15 
16 /*
17  * example snippet: use Java2D to modify an image being displayed in an SWT GUI.
18  * Take a screen snapshot to print the image to a printer.
19  *
20  * For a list of all SWT example snippets see
21  * http://www.eclipse.org/swt/snippets/
22  *
23  * @since 3.8
24  */
25 import static org.eclipse.swt.events.SelectionListener.*;
26 
27 import java.awt.*;
28 import java.awt.Canvas;
29 import java.awt.Color;
30 import java.awt.Image;
31 import java.awt.geom.*;
32 
33 import org.eclipse.swt.*;
34 import org.eclipse.swt.accessibility.*;
35 import org.eclipse.swt.awt.*;
36 import org.eclipse.swt.graphics.*;
37 import org.eclipse.swt.graphics.Point;
38 import org.eclipse.swt.graphics.Rectangle;
39 import org.eclipse.swt.layout.*;
40 import org.eclipse.swt.layout.GridLayout;
41 import org.eclipse.swt.printing.*;
42 import org.eclipse.swt.widgets.*;
43 import org.eclipse.swt.widgets.Button;
44 import org.eclipse.swt.widgets.Composite;
45 import org.eclipse.swt.widgets.FileDialog;
46 import org.eclipse.swt.widgets.Label;
47 
48 public class Snippet361 {
49 	static Composite composite; // SWT
50 	static Canvas canvas; // AWT
51 	static Image image = null; // AWT
52 	static double translateX = 0, translateY = 0;
53 	static double rotate = 0;
54 
main(String[] args)55 	public static void main(String[] args) {
56 		final Display display = new Display();
57 		final Shell shell = new Shell(display);
58 		shell.setText("Snippet 361");
59 		shell.setText("Translate and Rotate an AWT Image in an SWT GUI");
60 		shell.setLayout(new GridLayout(8, false));
61 
62 		Button fileButton = new Button(shell, SWT.PUSH);
63 		fileButton.setText("&Open Image File");
64 		fileButton.addSelectionListener(widgetSelectedAdapter(e -> {
65 			String filename = new FileDialog(shell).open();
66 			if (filename != null) {
67 				image = Toolkit.getDefaultToolkit().getImage(filename);
68 				canvas.repaint();
69 			}
70 		}));
71 
72 		new Label(shell, SWT.NONE).setText("Translate &X by:");
73 		final Combo translateXCombo = new Combo(shell, SWT.NONE);
74 		translateXCombo.setItems("0", "image width", "image height", "100", "200");
75 		translateXCombo.select(0);
76 		translateXCombo.addModifyListener(e -> {
77 			translateX = numericValue(translateXCombo);
78 			canvas.repaint();
79 		});
80 
81 		new Label(shell, SWT.NONE).setText("Translate &Y by:");
82 		final Combo translateYCombo = new Combo(shell, SWT.NONE);
83 		translateYCombo.setItems("0", "image width", "image height", "100", "200");
84 		translateYCombo.select(0);
85 		translateYCombo.addModifyListener(e -> {
86 			translateY = numericValue(translateYCombo);
87 			canvas.repaint();
88 		});
89 
90 		new Label(shell, SWT.NONE).setText("&Rotate by:");
91 		final Combo rotateCombo = new Combo(shell, SWT.NONE);
92 		rotateCombo.setItems("0", "Pi", "Pi/2", "Pi/4", "Pi/8");
93 		rotateCombo.select(0);
94 		rotateCombo.addModifyListener(e -> {
95 			rotate = numericValue(rotateCombo);
96 			canvas.repaint();
97 		});
98 
99 		Button printButton = new Button(shell, SWT.PUSH);
100 		printButton.setText("&Print Image");
101 		printButton.addSelectionListener(widgetSelectedAdapter(e -> {
102 			performPrintAction(display, shell);
103 		}));
104 
105 		composite = new Composite(shell, SWT.EMBEDDED | SWT.BORDER);
106 		GridData data = new GridData(SWT.FILL, SWT.FILL, true, true, 8, 1);
107 		data.widthHint = 640;
108 		data.heightHint = 480;
109 		composite.setLayoutData(data);
110 		Frame frame = SWT_AWT.new_Frame(composite);
111 		canvas = new Canvas() {
112 			@Override
113 			public void paint (Graphics g) {
114 				if (image != null) {
115 					g.setColor(Color.WHITE);
116 					g.fillRect(0, 0, canvas.getWidth(), canvas.getHeight());
117 
118 					/* Use Java2D here to modify the image as desired. */
119 					Graphics2D g2d = (Graphics2D) g;
120 					AffineTransform t = new AffineTransform();
121 					t.translate(translateX, translateY);
122 					t.rotate(rotate);
123 					g2d.setTransform(t);
124 					/*------------*/
125 
126 					g.drawImage(image, 0, 0, this);
127 				}
128 			}
129 		};
130 		frame.add(canvas);
131 		composite.getAccessible().addAccessibleListener(new AccessibleAdapter() {
132 			@Override
133 			public void getName(AccessibleEvent e) {
134 				e.result = "Image drawn in AWT Canvas";
135 			}
136 		});
137 
138 		shell.pack();
139 		shell.open();
140 		while(!shell.isDisposed()) {
141 			if (!display.readAndDispatch()) display.sleep();
142 		}
143 		display.dispose();
144 	}
145 
performPrintAction(final Display display, final Shell shell)146 	private static void performPrintAction(final Display display, final Shell shell) {
147 		Rectangle r = composite.getBounds();
148 		Point p = shell.toDisplay(r.x, r.y);
149 		org.eclipse.swt.graphics.Image snapshotImage
150 			= new org.eclipse.swt.graphics.Image(display, r.width-2, r.height-2);
151 		GC snapshotGC = new GC(display);
152 		snapshotGC.copyArea(snapshotImage, p.x+1, p.y+1);
153 		PrintDialog dialog = new PrintDialog(shell, SWT.NONE);
154 		PrinterData data = new PrinterData();
155 		data.orientation = PrinterData.LANDSCAPE;
156 		dialog.setPrinterData(data);
157 		data = dialog.open();
158 		if (data != null) {
159 			Printer printer = new Printer(data);
160 			Point screenDPI = display.getDPI();
161 			Point printerDPI = printer.getDPI();
162 			int scaleFactor = printerDPI.x / screenDPI.x;
163 			Rectangle trim = printer.computeTrim(0, 0, 0, 0);
164 			if (printer.startJob("Print Image")) {
165 				ImageData imageData = snapshotImage.getImageData();
166 				org.eclipse.swt.graphics.Image printerImage
167 					= new org.eclipse.swt.graphics.Image(printer, imageData);
168 				GC printerGC = new GC(printer);
169 				if (printer.startPage()) {
170 					printerGC.drawImage(
171 						printerImage,
172 						0,
173 						0,
174 						imageData.width,
175 						imageData.height,
176 						-trim.x,
177 						-trim.y,
178 						scaleFactor * imageData.width,
179 						scaleFactor * imageData.height);
180 					printer.endPage();
181 				}
182 				printerGC.dispose();
183 				printer.endJob();
184 			}
185 			printer.dispose();
186 		}
187 		snapshotImage.dispose();
188 		snapshotGC.dispose ();
189 	}
190 
numericValue(Combo combo)191 	static double numericValue(Combo combo) {
192 		String string = combo.getText();
193 		if (string.equals("image width")) return image.getWidth(canvas);
194 		if (string.equals("image height")) return image.getHeight(canvas);
195 		if (string.equals("100")) return 100;
196 		if (string.equals("200")) return 200;
197 		if (string.equals("Pi")) return Math.PI;
198 		if (string.equals("Pi/2")) return Math.PI / 2;
199 		if (string.equals("Pi/4")) return Math.PI / 4;
200 		if (string.equals("Pi/8")) return Math.PI / 8;
201 		/* Allow user-entered numbers. */
202 		Double d = (double) 0;
203 		try {
204 			d = Double.valueOf(string);
205 		} catch(NumberFormatException ex) {
206 		}
207 		return d;
208 	}
209 
210 }
211