奇怪的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)?



          好好活著,因?yàn)槲覀儠赖暮芫?

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

          評論

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

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

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

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

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

          你把它改成paint就知道了

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


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 浦城县| 白玉县| 渭南市| 阿合奇县| 西青区| 渑池县| 平昌县| 资讯 | 镇江市| 鄯善县| 腾冲县| 永福县| 公安县| 平邑县| 桃江县| 湖州市| 鲜城| 阳山县| 绩溪县| 永年县| 望城县| 娄烦县| 社旗县| 静宁县| 涡阳县| 三台县| 车致| 桦甸市| 柳江县| 专栏| 台东市| 大连市| 漳平市| 南阳市| 福安市| 乌兰察布市| 左贡县| 得荣县| 公主岭市| 沂南县| 吉木萨尔县|