Pudgy's World
          posts - 13,  comments - 16,  trackbacks - 0
          一個Custom Border
          可以實現點劃線的Border.
          代碼如下:

          import java.awt.*;
          import javax.swing.
          *;
          import javax.swing.border.
          *;
           
          /**
           * DotDashBorder is a java.swing.border.Border implementation 
           * that draws a dotted and/or dashed line border.  The dot/dash pattern is 
           * defined as an array of ints, with every two indices defining the number 
           * of pixels for each dot/dash and space.  E.g.: 

           * { 1, 1 } == 1-pixel-dot, 1-pixel-space, 1-pixel-dot, 1-pixel-space, etc.

           * { 4, 2 } == 4-pixel-dash, 2-pixel-space, 4-pixel-dash, 2-pixel-space, etc.

           
          */
          public class DotDashBorder extends AbstractBorder {
              
          /**
               * Defines a dotted line pattern:  
          . . . .

               
          */
              
          public static final int[] DOT = { 11 };
           
              
          /**
               * Defines a dashed line pattern:  
          - - - -

               
          */
              
          public static final int[] DASH_SHORT = { 44 };
           
              
          /**
               * Defines a long dashed line pattern:  
          ---   ---   ---

               
          */
              
          public static final int[] DASH_LONG = { 1010 };
           
              
          /**
               * Defines a long/short dashed line pattern:  
          -- - -- - -- -

               
          */
              
          public static final int[] DASH_LONG_SHORT = { 6333 };
           
              
          /**
               * Defines a long/short/short dashed line pattern:  
          -- - - -- - - -- - -

               
          */
              
          public static final int[] DASH_LONG_SHORT_SHORT = { 633333 };
           
              
          /**
               * The dot/dash pattern.
               
          */
              
          private int[] pattern = DOT;
           
              
          /**
               * The border thickness
               
          */
              
          private int thickness = 1;
           
              
          /**
               * The foreground color.  If not specified, the component's foreground 
               * color is used.  
               
          */
              
          private Color colorFG = null;
           
              
          /**
               * The background color.  If not specified, the component's background 
               * color is used.  
               
          */
              
          private Color colorBG = null;
           
              
          /**
               * Thicken border pattern relative to thickness flag.  
               
          */
              
          private boolean thicken = false;
           
              
          /**
               * Create a new 1-pixel thick DotDashBorder using the component's 
               * background color.
               * @param  pattern  int[]: the dot/dash-space pattern
               * @param  fg  Color: the foreground (dot/dash) color
               
          */
              
          public DotDashBorder(int[] pattern, Color fg) {
                  
          this(pattern, 1,  fg, null);
              }
           
              
          /**
               * Create a new DotDashBorder using the component's foreground and 
               * background colors.
               * @param  pattern  int[]: the dot/dash-space pattern
               * @param  thickness  int: the border thickness
               
          */
              
          public DotDashBorder(int[] pattern, int thickness) {
                  
          this(pattern, thickness, nullnull);
              }
           
              
          /**
               * Create a new DotDashBorder using the component's 
               * background color.
               * @param  pattern  int[]: the dot/dash-space pattern
               * @param  thickness  int: the border thickness
               * @param  fg  Color: the foreground (dot/dash) color
               
          */
              
          public DotDashBorder(int[] pattern, int thickness, Color fg) {
                  
          this(pattern, thickness,  fg, null);
              }
           
              
          /**
               * Create a new DotDashBorder.
               * @param  pattern  int[]: the dot/dash-space pattern
               * @param  thickness  int: the border thickness
               * @param  fg  Color: the foreground (dot/dash) color
               * @param  bg  Color: the background (space) color
               
          */
              
          public DotDashBorder(int[] pattern, int thickness, Color fg, Color bg) {
                  
          for(int i = 0; i < pattern.length; i++) {
                      
          if(pattern[i] <= 0) {
                          
          throw new IllegalArgumentException("Pattern cannot have values <= 0.");
                      }
                  }
                  
          this.pattern = pattern;
                  
          if(thickness <= 0) {
                      
          throw new IllegalArgumentException("Thickness cannot be <= 0.");
                  }
                  
          this.thickness = thickness;
                  
          this.colorFG = fg;
                  
          this.colorBG = bg;
              }
           
              
          /**
               * Get the insets for the border.
               * @param  Component c: the component the border is for
               * @return  Insets: the insets for the border
               * @see  #getBorderInsets(Component, Insets)
               
          */
              
          public Insets getBorderInsets(Component c) {
                  
          return new Insets(thickness, thickness, thickness, thickness);
              }
           
              
          /**
               * Get the insets for the border.
               * @param  Component c: the component the border is for
               * @param  insets  Insets: the insets
               * @return  Insets: the insets for the border
               * @see  #getBorderInsets(Component)
               
          */
              
          public Insets getBorderInsets(Component c, Insets insets) {
                  
          return new Insets(thickness, thickness, thickness, thickness);
              }
           
              
          /**
               * Check if the border is opaque.
               * @return  boolean: true if the border is opaque
               
          */
              
          public boolean isBorderOpaque() {
                  
          return true;
              }
           
              
          /**
               * Check if pattern will be thickened by thickness.
               * @return  boolean: true if pattern will be thickened by thickness
               * @see  #setThickenPattern(boolean)
               
          */
              
          public boolean isThickenPattern() {
                  
          return this.thicken;
              }
           
              
          /**
               * Set if the pattern should be thickened by thickness.  If true, a 
               * pattern of { 1, 1 } and thickness of 5, the pattern would be expanded 
               * to { 5, 5 }.  This allows setting a pattern based on small pixel 
               * measurements that grows proportionally with the thickness.  
               * @param  boolean t: true if pattern should be thickened by thickness
               * @see  #isThickenPattern()
               
          */
              
          public void setThickenPattern(boolean t) {
                  
          this.thicken = t;
              }
           
              
          /**
               * Paint the border.
               * @param  Component c: the component the border is for
               * @param  Graphics g: the graphics object to draw on
               * @param  x  int: the border y position
               * @param  y  int: the border x position
               * @param  width  int: the border width
               * @param  height  int: the border height
               
          */
              
          public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
                  Color colorFGX 
          = c.getBackground();
                  
          if(colorFG != null) {
                      colorFGX 
          = colorFG;
                  }
                  Color colorBGX 
          = c.getBackground();
                  
          if(colorBG != null) {
                      colorBGX 
          = colorBG;
                  }
                  g.setColor(colorFGX);
                  g.fillRect(x, x, width, thickness);                
          // top
                  g.fillRect(x, y+height-thickness, width, thickness);    // bottom
                  g.fillRect(x, y, thickness, height);                // left
                  g.fillRect(x+width-thickness, y, thickness, height);    // right
                  g.setColor(colorBGX);
                  
          // top/bottom
                  int cx = 0;
                  
          // get real pattern
                  int[] realPattern = new int[pattern.length];
                  
          for(int i = 0; i < pattern.length; i++) {
                      
          if(thicken) {
                          realPattern[i] 
          = pattern[i]*thickness;
                      } 
          else {
                          realPattern[i] 
          = pattern[i];
                      }
                  }
                  
          for(int i = 0, j = 0; i < width; i++, j+=2) {
                      
          if(j >= realPattern.length) {
                          j 
          = 0;
                      }
                      cx 
          += realPattern[j];
                      g.fillRect(cx, y, realPattern[j
          +1], thickness);                // top
                      g.fillRect(cx, y+height-thickness, realPattern[j+1], thickness);    // bottom
                      cx += realPattern[j+1];
                  }
                  
          // left/right
                  int cy = 0;
                  
          for(int i = 0, j = 0; i < height; i++, j+=2) {
                      
          if(j >= realPattern.length) {
                          j 
          = 0;
                      }
                      cy 
          += realPattern[j];
                      g.fillRect(x, cy, thickness, realPattern[j
          +1]);                // left
                      g.fillRect(x+width-thickness, cy, thickness, realPattern[j+1]);    // right
                      cy += realPattern[j+1];
                  }
              }
          }
          posted on 2005-08-27 10:19 Pudgy's World 閱讀(922) 評論(0)  編輯  收藏 所屬分類: Java Newbie

          <2005年8月>
          31123456
          78910111213
          14151617181920
          21222324252627
          28293031123
          45678910

          常用鏈接

          留言簿(1)

          隨筆分類(13)

          隨筆檔案(13)

          文章分類(4)

          文章檔案(5)

          相冊

          Developer

          Favorite blogs

          搜索

          •  

          積分與排名

          • 積分 - 22516
          • 排名 - 1626

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 喜德县| 康定县| 淮南市| 获嘉县| 兴山县| 龙江县| 苗栗市| 湖南省| 贵德县| 庆元县| 虞城县| 康乐县| 临夏县| 贵南县| 永登县| 和静县| 易门县| 永城市| 汤阴县| 鸡西市| 岢岚县| 小金县| 台山市| 丹东市| 田林县| 通化市| 榆中县| 嘉祥县| 和田县| 鹤岗市| 临桂县| 阿鲁科尔沁旗| 浑源县| 微山县| 合作市| 西乌| 金坛市| 和龙市| 渑池县| 江西省| 涡阳县|