简体   繁体   English

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

[英]Scroll not working with Canvas & GC in Java SWT

I am trying to draw some shapes and lines on a SWT Canvas object using GC[Graphics Context ]. 我正在尝试使用GC [Graphics Context]在SWT Canvas对象上绘制一些形状和线条。 The Canvas object is initialized with fixed size & V_SCROLL|H_SCROLL. Canvas对象使用固定大小&V_SCROLL | H_SCROLL初始化。 I want the Canvas to be scroll-able once GC exceeds the Canvas boundaries. 我希望一旦GC超出Canvas边界,Canvas就可以滚动。 Though the scroll bars are getting appeared they are not working and the last part of lines are getting truncated. 尽管滚动条出现了,但它们无法正常工作,并且最后一行被截断了。

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");

I just got scroll-able Group with an Image drawn using GC in SWT. 我刚得到一个可滚动的组,其中包含在SWT中使用GC绘制的图像。 You need to create and image using GC and then set to a Group which is created inside a ScrollableComposite. 您需要使用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");

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM