kooyee ‘s blog

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

          [SWT] Canvas scroll 滾動條

          Posted on 2007-07-25 19:47 kooyee 閱讀(3479) 評論(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 (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. 這樣才會在新的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);

          主站蜘蛛池模板: 永清县| 牡丹江市| 措勤县| 东兰县| 沈丘县| 大邑县| 蓝田县| 思南县| 修水县| 白城市| 无极县| 博湖县| 梅州市| 金门县| 文安县| 长葛市| 青冈县| 平安县| 即墨市| 阿坝县| 蒙自县| 天津市| 福鼎市| 涪陵区| 贵德县| 乌鲁木齐市| 赤水市| 炉霍县| 科技| 图木舒克市| 新田县| 共和县| 米泉市| 峨眉山市| 长沙市| 肥东县| 呼图壁县| 普安县| 青州市| 微博| 东源县|