奇怪的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 閱讀(3870) 評論(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就知道了

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


          網站導航:
           
          主站蜘蛛池模板: 杂多县| 遵义市| 福清市| 安仁县| 文登市| 扬中市| 邹城市| 墨竹工卡县| 工布江达县| 茶陵县| 白玉县| 宣威市| 镇雄县| 沐川县| 台南市| 武乡县| 浙江省| 开鲁县| 库车县| 铜梁县| 禹城市| 晋中市| 两当县| 邵阳县| 天镇县| 北流市| 余江县| 屏边| 平果县| 财经| 兰考县| 民乐县| 枣庄市| 台南市| 江山市| 微山县| 威海市| 城口县| 彭州市| 济源市| 香格里拉县|