在 Java SWT 中滚动不使用 Canvas & GC

Scroll not working with Canvas & GC in Java SWT

我正在尝试使用 GC[Graphics Context] 在 SWT Canvas 对象上绘制一些形状和线条。 Canvas 对象初始化为固定大小 & V_SCROLL|H_SCROLL。一旦 GC 超过 Canvas 边界,我希望 Canvas 可以滚动。 尽管滚动条出现了,但它们不起作用,行的最后一部分被截断了。

Group grpSchema = new Group(shell, SWT.NONE);
    grpSchema.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false, 1, 1));
    grpSchema.setText("Picture");

    Button btnPaint = new Button(shell, SWT.NONE);
    btnPaint.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, true, false, 1, 1));
    btnPaint.addSelectionListener(new SelectionAdapter() {
        @Override
        public void widgetSelected(SelectionEvent e) {
            if(null != canvas){
                canvas.dispose();
            }
            canvas = new Canvas(grpSchema, SWT.V_SCROLL|SWT.H_SCROLL);
            canvas.setBounds(10, 20, 200, 200);
            canvas.addPaintListener(new PaintListener() {
                @Override
                public void paintControl(PaintEvent arg0) {
                    GC gc = arg0.gc;
                    gc.drawLine(0, 0, 200, 500);
                }
            });
        }
    });
    btnPaint.setText("paint");

我刚刚获得了可滚动组,其中包含在 SWT 中使用 GC 绘制的图像。 您需要使用 GC 创建和映像,然后设置为在 ScrollableComposite 中创建的组。

ScrolledComposite scroll = new ScrolledComposite(shell, SWT.V_SCROLL|SWT.H_SCROLL);
scroll.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false, 1, 1));

Group grpDraw = new Group(scroll, SWT.V_SCROLL|SWT.H_SCROLL);
grpDraw.setText("Picture");
grpDraw.setBounds(0, 0, 200, 200);

Button btnPaint = new Button(shell, SWT.NONE);
btnPaint.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, true, false, 1, 1));
btnPaint.addSelectionListener(new SelectionAdapter() {
        @Override
        public void widgetSelected(SelectionEvent e) {
            Image image = new Image(display, 1000, 1000);
            GC gc = new GC(image);
            gc.setBackground(display.getSystemColor(SWT.COLOR_YELLOW));
            gc.fillOval(50,50,100,100);
            gc.setForeground(display.getSystemColor(SWT.COLOR_DARK_GREEN));
            gc.dispose();
            grpDraw.setBackgroundImage(image);
            scroll.setContent(grpDraw);
        }
    });
btnPaint.setText("paint");