kooyee ‘s blog

          開源軟件, 眾人努力的結(jié)晶, 全人類的共同財(cái)富
          posts - 103, comments - 55, trackbacks - 0, articles - 66
             :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理

          [SWT] Canvas scroll 滾動(dòng)條

          Posted on 2007-07-25 19:47 kooyee 閱讀(3485) 評論(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)作為原點(diǎn)滾動(dòng),否則在滾動(dòng)中會(huì)變形

          public static void main (String [] args) {
          >     Display display = new Display ();
          >     Shell shell = new Shell (display);
          >     shell.setLayout(new FillLayout());
          >     final Point origin = new Point (00);
          >     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, 000, width, 100true);
          >             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, 00, width, 100true);
          >             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 (500150);
          >     shell.open ();
          >     while (!shell.isDisposed ()) {
          >         if (!display.readAndDispatch ()) display.sleep ();
          >     }

          >     display.dispose ();
          > }


          要是用GC畫圖的話,一定要加上origin.x和origin.y. 這樣才會(huì)在新的redraw()的圖形中把矩形顯示在相應(yīng)位置
          //x,y 矩形的起點(diǎn)坐標(biāo)
          gc.fillRectangle(origin.x+x,origin.y+y,595,842);

          例如 設(shè)矩形起點(diǎn)(50,50)和origin(0,0), 滾動(dòng)后origin點(diǎn)的坐標(biāo)變?yōu)?0,-10)。這樣origin.x+x,origin.y+y 后矩形的新的起點(diǎn)為(50,40),然后GC在新的起點(diǎn)(50,40)畫出圖形, 這樣顯示出來的矩形就向上移動(dòng)了10個(gè)像素。(如果對起點(diǎn)的x,y的值進(jìn)行運(yùn)算,沒有必要改動(dòng)origin.x和origin.y,分開考慮。 )
          比如畫3個(gè)上下間隔為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);

          主站蜘蛛池模板: 太康县| 新乡市| 乐清市| 运城市| 云龙县| 如东县| 东阿县| 涿鹿县| 涟水县| 炎陵县| 上思县| 淳化县| 大同市| 清徐县| 斗六市| 广丰县| 武汉市| 浦县| 盐城市| 开化县| 彩票| 武定县| 莱西市| 乌拉特中旗| 阜南县| 合阳县| 清丰县| 两当县| 泌阳县| 敦煌市| 东台市| 古丈县| 永济市| 桓台县| 隆昌县| 乌鲁木齐市| 陆川县| 色达县| 息烽县| 安龙县| 治多县|