|
Posted on 2007-07-25 19:47 kooyee 阅读(3453) 评论(0) 编辑 收藏 所属分类: GUI骨衣
scroll(int destX, int destY, int x, int y, int width, int height, boolean all)
//Scrolls a rectangular area of the receiver by first copying the source area to the destination and then causing the area of the source which is not covered by the destination to be repainted.
以origin(0,0)作为原点滚动,否则在滚动中会变形
public static void main (String [] args) { > Display display = new Display (); > Shell shell = new Shell (display); > shell.setLayout(new FillLayout()); > final Point origin = new Point (0, 0); > final Canvas canvas = new Canvas (shell,SWT.V_SCROLL | SWT.H_SCROLL); > final int width = 500; > final ScrollBar hBar = canvas.getHorizontalBar (); > final Color red = new Color(null,255,0,0); > hBar.setMaximum(width); > hBar.addListener (SWT.Selection, new Listener () { > > public void handleEvent (Event e) { > System.out.println("HBar listener"); > int hSelection = hBar.getSelection (); > int destX = -hSelection - origin.x; > canvas.scroll (destX, 0, 0, 0, width, 100, true); > origin.x = -hSelection; > System.out.println("HBar listener exit"); > } > }); > final ScrollBar vBar = canvas.getVerticalBar (); > vBar.setMaximum(100); > vBar.addListener (SWT.Selection, new Listener () { > public void handleEvent (Event e) { > int vSelection = vBar.getSelection (); > int destY = -vSelection - origin.y; > canvas.scroll (0, destY, 0, 0, width, 100, true); > origin.y = -vSelection; > } > }); > canvas.addListener (SWT.Paint, new Listener () { > > boolean init = true; > public void handleEvent (Event e) { > System.out.println("Paint listener"); > > GC gc = e.gc; > gc.setBackground(red); > gc.fillRectangle(origin.x,origin.y,width,100); > gc.drawText ("test", origin.x, origin.y); > > > } > }); > shell.setSize (500, 150); > shell.open (); > while (!shell.isDisposed ()) { > if (!display.readAndDispatch ()) display.sleep (); > } > display.dispose (); > }
要是用GC画图的话,一定要加上origin.x和origin.y. 这样才会在新的redraw()的图形中把矩形显示在相应位置
//x,y 矩形的起点坐标 gc.fillRectangle(origin.x+x,origin.y+y,595,842);
例如 设矩形起点(50,50)和origin(0,0), 滚动后origin点的坐标变为(0,-10)。这样origin.x +x,origin.y+y 后矩形的新的起点为(50,40),然后GC在新的起点(50,40)画出图形, 这样显示出来的矩形就向上移动了10个像素。(如果对起点的x,y的值进行运算,没有必要改动origin.x和origin.y,分开考虑。 )
比如画3个上下间隔为100的矩形
gc.fillRectangle(origin.x+x,origin.y+y,595,842); y += 100; gc.fillRectangle(origin.x+x,origin.y+y,595,842); y += 100; gc.fillRectangle(origin.x+x,origin.y+y,595,842);
|