SWT/Jface 图片倒影效果

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();
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值