|
Posted on 2007-07-25 19:47 kooyee 閱讀(3480) 評論(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點的坐標變?yōu)?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);
|