我的窗口(SWT)中有以下 UI 布局:
- 壳
- 选项卡文件夹(填充布局)
- 复合(RowLayout(水平)
- 组(RowLayout,垂直)
- 标签
- 标签
- 文字(宽度:450)
- 按钮
- 组(RowLayout,垂直)
- 标签
- 标签
- 画布(这是问题)
- 组(RowLayout,垂直)
所有的 RowLayouts 都具有以下属性:
wrap = true, justify = true, pack = true, marginLeft = 0
我遇到的问题是,如果我尝试将 any 设置Image
为(在调用事件canvas
时使用 绘制),即使调用了绘制事件(我看到了调用),它通常也根本不显示。如果我设置一个 5 像素的图像并最大化窗口,则图像被绘制,但它也会疯狂地调用绘制事件,并且图像不断闪烁。paint
event.gc.drawImage(0,0)
System.out.println()
我在设置图像后调整画布大小,使用:
canvas.setSize(img.getBounds().width, img.getBounds().width);
如果我删除它,那么闪烁和重复的paint
调用就消失了,但是我仍然无法显示大于 5x5 的图像,它们根本不显示。
这里发生了什么..?我应该切换到 GridLayout 吗?我基本上只想展示两个组,每个组都包含一个垂直的字段/画布列表。
我的 ImgCanvas 类的代码,用于处理图像的显示:
public class ImgCanvas
{
private Canvas canvas;
private Image img;
private int lastImgHash = 0;
public ImgCanvas(Composite parent)
{
canvas = new Canvas(parent, SWT.NONE);
initCanvas();
}
public ImgCanvas(Composite parent, Image img)
{
canvas = new Canvas(parent, SWT.NONE);
setImage(img);
initCanvas();
}
public void setCanvas(Canvas canvas)
{
this.canvas = canvas;
this.initCanvas();
}
public void setImage(Image img)
{
if (this.img != null)
this.img.dispose();
this.img = img;
System.out.println("Set image: " + img.getBounds() + ", " + img.toString());
redraw();
}
public void redraw()
{
canvas.redraw();
}
protected void initCanvas()
{
System.out.println("Canvas started");
canvas.addPaintListener( getPaintListener() );
canvas.addDisposeListener( getDisposeListener() );
}
protected PaintListener getPaintListener()
{
return new PaintListener()
{
public void paintControl(PaintEvent e)
{
System.out.println("Painting");
if (img != null )
{
System.out.println("Img:" + img.getBounds() );
e.gc.drawImage(img, 0, 0);
//canvas.setSize(img.getBounds().width, img.getBounds().width);
//canvas.pack();
}
else
System.out.println("Img is null: " + img);
}
};
}
protected DisposeListener getDisposeListener()
{
return new DisposeListener()
{
@Override
public void widgetDisposed(DisposeEvent e)
{
System.out.println("Disposing");
if (img != null)
img.dispose();
}
};
}
}
这是通过以下方式设置的:
imgCanvas = new ImgCanvas(group2); //2nd group in the layout given above.
然后稍后,在组 1(selectionHandler)中按钮的单击处理程序中,完成以下操作:
public void widgetSelected(SelectionEvent e)
{
//get a screenshot of a particular screen region using Java.Awt.Robot.captureScreenRegion,
//convert the image into a SWT image, and try to show it:
Image screenshot = ImgUtility.getScreenShot(0,0,10,10);
imgCanvas.setImage(screenshot);
System.out.println("redrawn");
}