自定义的SWT控件:缩略图

效果图:
[img]/upload/attachment/45234/e8e11309-b7f7-3f90-847b-b8dd8ce34986.png[/img]

为了在SWT中实现像Windows桌面图标那样的效果,自定义了一个控件,该控件继承自org.eclipse.swt.widgets.Composite,由一个缩略图标(CLabel)和一个图标名称(Label)构成。
缩略图是由图片文件提供,并缩放到固定尺寸。

创建该控件需要向其构造函数传递一个Image和String,分别是缩略图的原图和即将在图标下方显示的名称。

控件的代码如下,很简单:


import java.io.File;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.events.MouseListener;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;

public class Thumbnails extends Composite {
private CLabel cLabelImage;
private Image image;
private Label label;
private String labelText;

private final int IMAGE_SIZE = 164;//缩放后的图片最长的边长为定值。

/**
* 对外使用的构造函数。
* @param parent
* @param style
* @param image
* @param labelText
*/
public Thumbnails(Composite parent, int style, Image image, String labelText) {
super(parent, style);
this.image = image;
this.labelText = labelText;
initGUI();
}

private void initGUI() {
GridData data = new GridData ();
data.widthHint = 170;
data.heightHint = 180;
this.setLayoutData(data);
try {
GridLayout thisLayout = new GridLayout();
this.setLayout(thisLayout);

{
GridData cLabelImageGridData = new GridData();
cLabelImageGridData.horizontalAlignment = GridData.FILL;
cLabelImageGridData.verticalAlignment = GridData.FILL;
cLabelImageGridData.grabExcessVerticalSpace = true;
cLabelImageGridData.grabExcessHorizontalSpace = true;
cLabelImage = new CLabel(this, SWT.NONE);
cLabelImage.setLayoutData(cLabelImageGridData);

// cLabelImage.setImage(this.image);
cLabelImage.addPaintListener(new PaintListener() {
public void paintControl(PaintEvent evt) {
cLabelImagePaintControl(evt);
}
});
}
{
label = new Label(this, SWT.NONE);
GridData labelGridData = new GridData();
labelGridData.horizontalAlignment = GridData.CENTER;
label.setLayoutData(labelGridData);
label.setText(this.labelText);
}
this.layout();
} catch (Exception e) {
e.printStackTrace();
}
}

/**
* 缩放图片
* @param evt
*/
private void cLabelImagePaintControl(PaintEvent evt) {
Rectangle bounds = image.getBounds();
int imageWidth = bounds.width;
int imageHeight = bounds.height;

int thumbWidth = IMAGE_SIZE;// TODO: 缩放后的图片最大宽度
int thumbHeight = IMAGE_SIZE;// TODO: 缩放后的图片最大高度

double ratio = (double)imageWidth/(double)imageHeight;

//若宽大于高
if(ratio>(double)1){
thumbHeight = (int)(thumbWidth/ratio);
}else {
thumbWidth = (int)(thumbHeight*ratio);
}

// 如果图片小于所略图大小, 不作处理
if (imageWidth < IMAGE_SIZE && imageHeight < IMAGE_SIZE) {
thumbWidth = imageWidth;
thumbHeight = imageHeight;
}

evt.gc.drawImage(image, 0, 0, bounds.width, bounds.height, 0, 0,
thumbWidth, thumbHeight);
// evt.gc.drawImage(image, srcX, srcY, srcWidth, srcHeight, destX, destY, destWidth, destHeight)
}


@Override
public void addMouseListener(MouseListener listener) {
super.addMouseListener(listener);
//这样在点击图标或标题的时候,整个控件才能被选中。
cLabelImage.addMouseListener(listener);
label.addMouseListener(listener);
}

@Override
public void dispose() {
if(null !=image && !image.isDisposed()){
image.dispose();
}
if(null !=cLabelImage && !cLabelImage.isDisposed()){
cLabelImage.dispose();
}
if(null !=label && !label.isDisposed()){
label.dispose();
}

super.dispose();
}
/**********************************以下代码用于测试SWT控件,实际使用中不会调用*********************************/
/**
* Auto-generated main method to display this
* org.eclipse.swt.widgets.Composite inside a new Shell.
*/
public static void main(String[] args) {
showGUI();
}

/**
* Auto-generated method to display this org.eclipse.swt.widgets.Composite
* inside a new Shell.
*/
public static void showGUI() {
Display display = Display.getDefault();
Shell shell = new Shell(display);

File imageFile = new File("D:\\sb.jpg");
Image localImage = null;
if (imageFile.exists()) {
ImageDescriptor imageDescriptor = ImageDescriptor.createFromFile(
null, imageFile.getAbsolutePath());
localImage = imageDescriptor.createImage();
}

Thumbnails inst = new Thumbnails(shell, SWT.NULL, localImage, "hello");

Point size = inst.getSize();
shell.setLayout(new FillLayout());
shell.layout();
if (size.x == 0 && size.y == 0) {
inst.pack();
shell.pack();
} else {
Rectangle shellBounds = shell.computeTrim(0, 0, size.x, size.y);
shell.setSize(shellBounds.width, shellBounds.height);
}
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值