滚动不适用于Canvas& Java SWT中的GC

时间:2016-02-19 07:46:02

标签: java swt uigraphicscontext

我正在尝试使用GC [图形上下文]在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");

1 个答案:

答案 0 :(得分:0)

我刚刚使用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");

enter image description here