奇怪的paintComponent()

            paintComponent定義在JComponent中,這個類是所有非窗口Swing組件的超類,只要窗口需要重新繪圖,事件處理器就會通告組件,從而引發執行所有組件的paintComponent方法。一定不要自己調用paintComponent方法。--摘自《Java核心技術》 

          以下是部份源碼:

          protected void paintComponent(Graphics g) {
                     
          //注意這里
          // super.paintComponent(g);
                  Graphics2D g2 = (Graphics2D) g;

                  
          //draw all rectangles
                  for (Rectangle2D rect : squares) {
                      g2.draw(rect);
                  }

          }

           
          public void add(Point2D p) {
                  
          double x = p.getX();
                  
          double y = p.getY();

                  current 
          = new Rectangle2D.Double(
                          x 
          - SIDELENGTH / 2,
                          y 
          - SIDELENGTH / 2,
                          SIDELENGTH,
                          SIDELENGTH);
                  squares.add(current);
                  repaint();
          }

            
          public void remove(Rectangle2D p) {
                  
          if (p == null{
                      
          return;
                  }

                  
          if (p == current) {
                      current 
          = null;
                  }

                  squares.remove(p);
                  repaint();
              }

              
          private static final int SIDELENGTH = 10;
              
          private ArrayList<Rectangle2D> squares;
          private Rectangle2D current;

          當鼠標單擊panel的時候,就會調用add方法,Panel面板上立刻出現了一個小方塊。當鼠標在小方塊上雙擊的時候,就會調用remove方法,但是Panel面板上的小方塊并不能立刻消失,必須將整個面板最小化,或者改變面板大小的時候,面板上的小方塊才會消失。也就是說,remove方法中repaint()并沒有立刻生效。

          然而把 super.paintComponent(g)前的//去掉之后不管是addremove中調用repaint()都立刻生效。

          JComponent中的paintComponent方法:

          protected void paintComponent(Graphics g) {
                  
          if (ui != null{
                      Graphics scratchGraphics 
          = (g == null? null : g.create();
                      
          try {
                          ui.update(scratchGraphics, 
          this);
                      }

                      
          finally {
                          scratchGraphics.dispose();
                      }

                  }

          }

             
          protected transient ComponentUI ui;

          是什么原因導致執行了上面一段代碼之后,面板就可以立即更新了呢?難道跟dispose釋放資源有關?



          好好活著,因為我們會死的很久!

          posted on 2008-08-14 17:18 RaviLi 閱讀(3862) 評論(3)  編輯  收藏 所屬分類: Java相關

          評論

          # re: 奇怪的paintComponent() 2008-08-14 19:44 Matthew Chen  回復  更多評論   

          remove(Rectangle2D r)的參數是如何獲得的?是通過點擊生成的吧,這個能夠和List里面的匹配嗎?用==看起來怪怪的,是不是該用equals,代碼不夠全,看不透。

          # re: 奇怪的paintComponent() 2008-08-14 21:19 zht  回復  更多評論   

          super.paintComponent(g);的作用就是清除以前繪制的內容,實際上就是使用backgroundcolor,fill一下整個區域,如果不調用這個的話自然不會清除。
          而當最小化又最大化的時候,我理解的是,會根據層次結構由paintmanger負責重畫所有的界面,就相當于整個界面都是新的,自然會清除掉以前繪制的內容

          # re: 奇怪的paintComponent() 2008-08-15 22:46 千里冰封  回復  更多評論   

          你把它改成paint就知道了

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 额济纳旗| 台北县| 东源县| 嘉荫县| 健康| 满洲里市| 汕尾市| 搜索| 马公市| 高尔夫| 江川县| 和政县| 察隅县| 茌平县| 登封市| 呼和浩特市| 全椒县| 景东| 大新县| 二连浩特市| 化隆| 汉寿县| 精河县| 广饶县| 大荔县| 五台县| 睢宁县| 兴宁市| 巍山| 格尔木市| 颍上县| 杨浦区| 曲阳县| 尚志市| 八宿县| 合山市| 永顺县| 余干县| 扬州市| 库伦旗| 兴城市|