滚动画布内容 [英] Scrolling canvas content

查看:167
本文介绍了滚动画布内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在画布上画了一些文字和矩形。

  package com.cavium.test.views; 

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.Point;
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.ScrollBar;
import org.eclipse.swt.widgets.Shell;

public class Test2 {
protected static final int Y_STEP = 20;
static int shellStyle = SWT.NO_REDRAW_RESIZE | SWT.NO_BACKGROUND
| SWT.H_SCROLL | SWT.CLOSE | SWT.V_SCROLL | SWT.RESIZE;
static int canvasStyle = SWT.NO_REDRAW_RESIZE; // | SWT.H_SCROLL |

// SWT.V_SCROLL;

public static void main(String [] args){
final显示display = new Display();
final Shell shell = new Shell(display,shellStyle);
shell.setLayout(new FillLayout());
shell.setBackground(display.getSystemColor((SWT.COLOR_CYAN)));
shell.setText(Canvas Test);
shell.setSize(300,200);

final Canvas canvas = new Canvas(shell,canvasStyle);
canvas.setLayout(new FillLayout());
canvas.setBackground(display.getSystemColor(SWT.COLOR_WHITE));点b
$ b最终点起点=新点(10,20);
final ScrollBar hBar = shell.getHorizo​​ntalBar();
Rectangle size = canvas.getBounds();
hBar.setMaximum(size.width);
hBar.setMinimum(0);

final ScrollBar vBar = shell.getVerticalBar();
hBar.setMaximum(size.height);
hBar.setMinimum(0);

//为画布创建一个油漆处理程序
canvas.addPaintListener(new PaintListener(){
@Override
public void paintControl(PaintEvent e){
//做一些绘图
egc.drawString(Rows,origin.x,origin.y);
egc.drawString(Data,120,20); $ (int i = 0; i <10; i ++){
egc.drawString(Row Header+(i + 1),origin.x,origin.y
+(i + 1)* Y_STEP);
for(int j = 0; j< 10; j ++){
egc.drawRectangle(origin.x + 110 +(j * ,origin.y
+(i + 1)* Y_STEP,20,20);
egc.drawString(C+(j + 1),origin.x + 110 + 2
+(j * 20),origin.y +(i + 1)* Y_STEP + 1);
}
}

}

});

shell.open();
while(!shell.isDisposed()){
if(!display.readAndDispatch()){
display.sleep();
}
}
display.dispose();

}
}

是否可以只滚动单元格(C1,C2 ...)拖动水平滚动条保持行标题1,行标题2 ...等等。
请参阅下面的快照





还让我知道如何在滚动条上检测到鼠标事件,即当用户点击上/下或左箭头/向右箭头按钮时,单击并拖动拇指,点击拇指和右箭头或左箭头按钮之间的区域?

解决方案

是的,你可以做到这一点,但就像完全自定义绘制表一样,你必须管理滚动效果好。您可以向滚动条添加侦听器,并使用 hBar.addSelectionListener()收听用户对滚动位置所做的任何更改。您将需要在滚动条(垂直和水平)上设置最小,最大,页面增量和缩略图大小,以确保实际体验。


I have drawn some text and rectangles on canvas.

    package com.cavium.test.views;

    import org.eclipse.swt.SWT;
    import org.eclipse.swt.events.PaintEvent;
    import org.eclipse.swt.events.PaintListener;
    import org.eclipse.swt.graphics.Point;
    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.ScrollBar;
    import org.eclipse.swt.widgets.Shell;

    public class Test2 {
        protected static final int Y_STEP = 20;
        static int shellStyle = SWT.NO_REDRAW_RESIZE | SWT.NO_BACKGROUND
                | SWT.H_SCROLL | SWT.CLOSE | SWT.V_SCROLL | SWT.RESIZE;
        static int canvasStyle = SWT.NO_REDRAW_RESIZE;// | SWT.H_SCROLL |

        // SWT.V_SCROLL;

        public static void main(String[] args) {
            final Display display = new Display();
            final Shell shell = new Shell(display, shellStyle);
            shell.setLayout(new FillLayout());
            shell.setBackground(display.getSystemColor((SWT.COLOR_CYAN)));
            shell.setText("Canvas Test");
            shell.setSize(300, 200);

            final Canvas canvas = new Canvas(shell, canvasStyle);
            canvas.setLayout(new FillLayout());
            canvas.setBackground(display.getSystemColor(SWT.COLOR_WHITE));

            final Point origin = new Point(10, 20);
            final ScrollBar hBar = shell.getHorizontalBar();
            Rectangle size = canvas.getBounds();
            hBar.setMaximum(size.width);
            hBar.setMinimum(0);

            final ScrollBar vBar = shell.getVerticalBar();
            hBar.setMaximum(size.height);
            hBar.setMinimum(0);

            // Create a paint handler for the canvas
            canvas.addPaintListener(new PaintListener() {
                @Override
                public void paintControl(PaintEvent e) {
                    // Do some drawing
                    e.gc.drawString("Rows", origin.x, origin.y);
                    e.gc.drawString("Data", 120, 20);
                    for (int i = 0; i < 10; i++) {
                        e.gc.drawString("Row Header" + (i + 1), origin.x, origin.y
                                + (i + 1) * Y_STEP);
                        for (int j = 0; j < 10; j++) {
                            e.gc.drawRectangle(origin.x + 110 + (j * 20), origin.y
                                    + (i + 1) * Y_STEP, 20, 20);
                            e.gc.drawString("C" + (j + 1), origin.x + 110 + 2
                                    + (j * 20), origin.y + (i + 1) * Y_STEP + 1);
                        }
                    }

                }

            });

            shell.open();
            while (!shell.isDisposed()) {
                if (!display.readAndDispatch()) {
                    display.sleep();
                }
            }
            display.dispose();

        }
    }

Is it possible to scroll only cells(C1, C2...) on dragging the horizontal scrollbar keeping the Row Header 1, Row Header 2 ...etc..unchanged. See snapshot below

Also let me know how we can detected the mouse events on scrollbars i.e when user clicks on up/down or left arrow/right arrow buttons, click and drag on thumb, clicks on the area between thumb and right arrow or left arrow button?

解决方案

Yes you can do this but just as you are completely custom drawing the table, you will have to manage the scroll effect as well. You can add listeners to the scroll bars and listen to any changes that the user makes to the scroll positions using hBar.addSelectionListener(). You will need to set the min, max, page increment and thumb sizes on the scroll bars (both vertical and horizontal) to ensure a realistic experience.

这篇关于滚动画布内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆