import java.io.InputStream;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Pattern;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class ReflectionTest {
private static final String IMAGE_FILE = "assets/icon.png";
public static void main(String[] args) {
final Display display = Display.getDefault();
final Shell shell = new Shell(display);
InputStream ins = ReflectionTest.class.getClassLoader()
.getResourceAsStream(IMAGE_FILE);
if (ins == null)
ins = ReflectionTest.class.getResourceAsStream(IMAGE_FILE);
final Image image = new Image(display, ins);
final Rectangle imageBounds = image.getBounds();
shell.setText("Reflection Demo");
shell.setSize(300, 200);
shell.setLayout(new FillLayout());
final Canvas canvas = new Canvas(shell, SWT.DOUBLE_BUFFERED);
canvas.addDisposeListener(new DisposeListener() {
public void widgetDisposed(DisposeEvent e) {
image.dispose();
}
});
canvas.addPaintListener(new PaintListener() {
public void paintControl(PaintEvent e) {
Rectangle clientArea = canvas.getClientArea();
Pattern pattern = new Pattern(display, 0, 0, 0,
clientArea.height, display
.getSystemColor(SWT.COLOR_BLACK), display
.getSystemColor(SWT.COLOR_GRAY));
e.gc.setBackgroundPattern(pattern);
e.gc.fillRectangle(clientArea);
pattern.dispose();
}
});
canvas.addPaintListener(new ReflectivePaintListener(
imageBounds.height + 10, new PaintListener() {
public void paintControl(PaintEvent e) {
Rectangle clientArea = canvas.getClientArea();
e.gc.drawImage(image,
(clientArea.width - imageBounds.width) / 2, 10);
}
}));
shell.open();
while (!shell.isDisposed())
if (!display.readAndDispatch())
display.sleep();
}
}
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.Transform;
public class ReflectivePaintListener implements PaintListener {
final PaintListener target;
int primaryHeight;
public ReflectivePaintListener(int primaryHeight, PaintListener target) {
if (target == null) throw new NullPointerException();
this.target = target;
this.primaryHeight = primaryHeight;
}
public void paintControl(PaintEvent e) {
Transform transform = new Transform(e.display);
e.gc.getTransform(transform);
target.paintControl(e);
Transform reflect = new Transform(e.display,
1, 0,
0, -.5f,
0, primaryHeight * 3 / 2);
transform.multiply(reflect);
reflect.dispose();
e.gc.setTransform(transform);
e.gc.setAlpha(100);
target.paintControl(e);
transform.dispose();
}
}
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Pattern;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class ReflectionTest {
private static final String IMAGE_FILE = "assets/icon.png";
public static void main(String[] args) {
final Display display = Display.getDefault();
final Shell shell = new Shell(display);
InputStream ins = ReflectionTest.class.getClassLoader()
.getResourceAsStream(IMAGE_FILE);
if (ins == null)
ins = ReflectionTest.class.getResourceAsStream(IMAGE_FILE);
final Image image = new Image(display, ins);
final Rectangle imageBounds = image.getBounds();
shell.setText("Reflection Demo");
shell.setSize(300, 200);
shell.setLayout(new FillLayout());
final Canvas canvas = new Canvas(shell, SWT.DOUBLE_BUFFERED);
canvas.addDisposeListener(new DisposeListener() {
public void widgetDisposed(DisposeEvent e) {
image.dispose();
}
});
canvas.addPaintListener(new PaintListener() {
public void paintControl(PaintEvent e) {
Rectangle clientArea = canvas.getClientArea();
Pattern pattern = new Pattern(display, 0, 0, 0,
clientArea.height, display
.getSystemColor(SWT.COLOR_BLACK), display
.getSystemColor(SWT.COLOR_GRAY));
e.gc.setBackgroundPattern(pattern);
e.gc.fillRectangle(clientArea);
pattern.dispose();
}
});
canvas.addPaintListener(new ReflectivePaintListener(
imageBounds.height + 10, new PaintListener() {
public void paintControl(PaintEvent e) {
Rectangle clientArea = canvas.getClientArea();
e.gc.drawImage(image,
(clientArea.width - imageBounds.width) / 2, 10);
}
}));
shell.open();
while (!shell.isDisposed())
if (!display.readAndDispatch())
display.sleep();
}
}
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.Transform;
public class ReflectivePaintListener implements PaintListener {
final PaintListener target;
int primaryHeight;
public ReflectivePaintListener(int primaryHeight, PaintListener target) {
if (target == null) throw new NullPointerException();
this.target = target;
this.primaryHeight = primaryHeight;
}
public void paintControl(PaintEvent e) {
Transform transform = new Transform(e.display);
e.gc.getTransform(transform);
target.paintControl(e);
Transform reflect = new Transform(e.display,
1, 0,
0, -.5f,
0, primaryHeight * 3 / 2);
transform.multiply(reflect);
reflect.dispose();
e.gc.setTransform(transform);
e.gc.setAlpha(100);
target.paintControl(e);
transform.dispose();
}
}