Scrolling canvas content

对着背影说爱祢 提交于 2019-12-12 13:21:11

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/15133999/scrolling-canvas-content

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!