奇怪的paintComponent()

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

          以下是部份源碼:

          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;

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

          然而把 super.paintComponent(g)前的//去掉之后不管是addremove中調(diào)用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;

          是什么原因?qū)е聢?zhí)行了上面一段代碼之后,面板就可以立即更新了呢?難道跟dispose釋放資源有關(guān)?



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

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

          評論

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

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

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

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

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

          你把它改成paint就知道了

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


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 辽阳市| 定州市| 丹江口市| 青州市| 内丘县| 阿鲁科尔沁旗| 改则县| 馆陶县| 乌拉特中旗| 磐石市| 抚宁县| 慈利县| 义马市| 凌海市| 临泽县| 柘荣县| 宿迁市| 伊宁县| 普陀区| 德阳市| 延寿县| 堆龙德庆县| 宜丰县| 织金县| 高尔夫| 寿光市| 攀枝花市| 和平区| 印江| 嘉善县| 淮南市| 徐州市| 平原县| 无锡市| 二连浩特市| 阿巴嘎旗| 新化县| 即墨市| 满洲里市| 西吉县| 高雄县|