this.canvas = new Canvas(shell, SWT.NO_BACKGROUND);
我使用的是PaintListener:
this.canvas.addPaintListener(new PaintListener() {
@Override
public void paintControl(PaintEvent e) {
// Draw images
synchronized (imageMarks) {
for (ImageMark mark : Whiteboard.this.imageMarks)
{
Image image = Whiteboard.this.getImage(mark.id);
Point position = ScaledPoint.toSWTPoint(Whiteboard.this.getCanvasSize(), mark.getPosition());
Point bounds = mark.getUnscaledBoundaries(Whiteboard.this.getCanvasSize());
e.gc.drawImage(image, 0, 0, image.getBounds().width, image.getBounds().height, position.x, position.y,
bounds.x, bounds.y);
}
}
// Draw pencil marks
synchronized (pencilMarks) {
e.gc.setLineWidth(LINE_WIDTH);
for (double[] line : Whiteboard.this.pencilMarks)
{
Point lastPosPoint = ScaledPoint.toSWTPoint(Whiteboard.this.getCanvasSize(), new ScaledPoint(line[0], line[2]));
Point newPosPoint = ScaledPoint.toSWTPoint(Whiteboard.this.getCanvasSize(), new ScaledPoint(line[1], line[3]));
e.gc.drawLine(lastPosPoint.x, lastPosPoint.y, newPosPoint.x, newPosPoint.y);
}
}
// Draw pointer, assuming it's there
if (pointerMark != null)
{
synchronized (pointerMark) {
Point pos = ScaledPoint.toSWTPoint(Whiteboard.this.getCanvasSize(), pointerMark.getPosition());
if (pointerMark.isFlipped())
e.gc.drawImage(Whiteboard.pointerImageFlipped, pos.x, pos.y);
else
e.gc.drawImage(Whiteboard.pointerImage, pos.x, pos.y);
}
}
}
});
并通过canvas.redraw()调用重绘画布。在64位Linux上,这似乎没有任何问题,但奇怪的是,在64位Windows上,没有任何东西最终被删除或重绘。例如,如果调整屏幕大小,铅笔标记也不会调整大小,它们最终会被切割出屏幕。当添加新标记时(换句话说,当再次调用绘图侦听器时),重新定位的标记将重新绘制在不与窗口缩放的旧标记之上。换句话说,我相信canvas.redraw()上没有清除画布。有解决方法吗?
答案 0 :(得分:2)
您正在指定SWT.NO_BACKGROUND,它会在每次绘制前停止清除画布。
如果您使用SWT.NO_BACKGROUND,绘制画布的每个像素都是您的绘画方法。
SWT.NO_BACKGROUND JavaDoc:
默认情况下,在窗口小部件绘制之前,客户端区域将填充 目前的背景。指定此样式时,背景为 没有填写,应用程序负责填充每个像素 客户区域。此样式可用作替代 "双缓冲"为了减少闪烁。这种风格没有 意思是"透明" - 被遮挡的小部件无法通过。