首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何将Zest图的一部分复制到SWT画布上

如何将Zest图的一部分复制到SWT画布上
EN

Stack Overflow用户
提问于 2014-07-03 13:40:27
回答 1查看 321关注 0票数 2

我正在使用环境基金(Draw2d 3.9,ZEST1.5.0)开发一个新的Eclipse插件,并使用定制的数字来表示一些数据。我现在正在尝试开发一个可调节的镜头视图。所需的功能是鼠标将被拖动在主窗口上,而“镜头视图窗口”将表示选定的区域。

为此,我使用一个带有SWT画布的窗口作为镜头视图。主窗口的(图)内容在透镜帆布上画。然后,我将镜头画布的另一个区域(可能在可见区域之外)复制到点0,0 (镜头画布)。问题是,镜头画布忽略了在其可见区域外绘制的所有内容,当我复制上面提到的区域时,我会变成黑色而不是内容。

将图形的内容复制到图像,然后绘制图像的一部分并不是解决方案,因为我的图形可能是巨大的(> 200000 x 200000)。

在下面您可以找到我的代码的一部分:

代码语言:javascript
运行
复制
import org.eclipse.draw2d.SWTGraphics;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.GC;
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;
import org.eclipse.zest.core.widgets.Graph;

public class LensView {
private final Shell shell;
private final Canvas canvas;

private final Point focalPoint;
private float lensScaleFactor;
private float mainScaleFactor;

public LensView(final Graph graph, Display d) {
    this.visualizer = visualizer;
    focalPoint = new Point(0, 0);

    lensScaleFactor     = 1.0f;
    mainScaleFactor = 1.0f;

    shell = new Shell(d, SWT.SHELL_TRIM);
    shell.setSize(640, 480);
    shell.setLayout(new FillLayout());  
    canvas = new Canvas(shell, SWT.NO_REDRAW_RESIZE);
    canvas.setLayout(new FillLayout());

    canvas.addPaintListener(new PaintListener() {   
        @Override
        public void paintControl(PaintEvent e) {
            Rectangle area  = canvas.getClientArea();
            //Scale the origin to 1.0 and apply our scale factor
            float scale = lensScaleFactor/mainScaleFactor; 

            Point viewLocation = graph.getViewport().getViewLocation();
            int tmpx = normaliseDec(Math.round((focalPoint.x + viewLocation.x) * scale) - Math.round(area.width/2.0f));
            int tmpy = normaliseDec(Math.round((focalPoint.y + viewLocation.y) * scale) - Math.round(area.height/2.0f));

            GC gc = new GC(canvas);
            SWTGraphics graphics = new SWTGraphics(gc);
            graphics.scale(scale);  
            graph.getContents().paint(graphics);                

            e.gc.copyArea(tmpx, tmpy, area.width, area.height, 0, 0);

            graphics.dispose();
            gc.dispose();
        }
    });
}

public void redraw (int x, int y) {
    focalPoint.x = normaliseDec(x);
    focalPoint.y = normaliseDec(y);
    canvas.redraw();
    canvas.update();
}

private int normaliseDec (int i) {
    return i < 0 ? 0 : i;
}

}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-07-05 01:51:48

认为您所需要做的就是从GC中创建ScaledGraphics对象,这些GC随PaintEvent而来,为图形设置缩放因子,设置裁剪矩形,然后绘制您的图形。类似的东西(没有测试,大致类似):

代码语言:javascript
运行
复制
ScaledGraphics graphics = new ScaledGraphics(e.gc);
graphics.scale(scale);
graphics.setClip(new Rectangle(focalPoint, getClientArea().getSize()));
graph.getContents().paint(graphics);
graphics.dispose();

几点:- ScaledGraphics应该为您完成裁剪矩形的缩放-应该在控件GC上绘制,而不是创建一个新的

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24555303

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档