Save This Page
Home » openjdk-7 » net.jbeans » ui » border » [javadoc | source]
    1   /* -------------------------------------------------------------------
    2    * Java source file for the class TiledMatteBorder
    3    * 
    4    * Copyright (c), 2002, Masahiro Takatsuka.
    5    * All Rights Researved.
    6    * 
    7    * Original Author: Masahiro Takatsuka (masa@jbeans.net)
    8    * $Author: takatsukam $
    9    * 
   10    * $Date: 2003/07/25 04:51:46 $
   11    * 
   12    * $Id: TiledMatteBorder.java,v 1.1.1.1 2003/07/25 04:51:46 takatsukam Exp $
   13    * 
   14    * Reference:		Document no:
   15    * ___				___
   16    * 
   17    * To Do:
   18    * ___
   19    * 
   20   ------------------------------------------------------------------- */
   21   
   22   /* --------------------------- Package ---------------------------- */
   23   package net.jbeans.ui.border;
   24   
   25   /* ------------------ Import classes (packages) ------------------- */
   26   import java.awt;
   27   import java.io;
   28   import javax.swing;
   29   import javax.swing.border;
   30   
   31   /*====================================================================
   32                  Implementation of class TiledMatteBorder               
   33   ====================================================================*/
   34   /**
   35    * A class which is similar to the javax.swing.border.MatteBorder.
   36    * This border, however, can alternate two tiled icons to create border.
   37    * 
   38    * @version $Revision: 1.1.1.1 $
   39    * @author Masahiro Takatsuka (masa@jbeans.net)
   40    * @see MatteBorder
   41    */
   42   
   43   public class TiledMatteBorder extends MatteBorder implements Serializable {
   44       protected Icon tileIconAlt;	
   45   
   46       /**
   47        * Creates a matte border with the specified insets and tile icon.
   48   	 * The two tile icons have to be the same size.
   49   	 *
   50        * @param top the top inset of the border
   51        * @param left the left inset of the border
   52        * @param bottom the bottom inset of the border
   53        * @param right the right inset of the border
   54        * @param tileIcon the icon to be used for tiling the border
   55        * @param tileIconAlt the alternate icon to be used for tiling the border
   56        */
   57       public TiledMatteBorder(int top, int left, int bottom, int right, Icon tileIcon, Icon tileIconAlt)   {
   58           super(top, left, bottom, right, tileIcon);
   59   		if (tileIcon.getIconWidth() == tileIconAlt.getIconWidth() &&
   60   			tileIcon.getIconHeight() == tileIconAlt.getIconHeight()) {
   61   			this.tileIconAlt = tileIconAlt;
   62   		} else {
   63   			this.tileIconAlt = tileIcon;
   64   		}
   65       }
   66   
   67       /**
   68        * Creates a matte border with the specified tile icon.  The
   69        * insets will be calculated dynamically based on the size of
   70        * the tile icon, where the top and bottom will be equal to the
   71        * tile icon's height, and the left and right will be equal to
   72        * the tile icon's width.
   73        * @param tileIcon the icon to be used for tiling the border
   74        * @param tileIconAlt the alternate icon to be used for tiling the border
   75        */
   76       public TiledMatteBorder(Icon tileIcon, Icon tileIconAlt)   {
   77           this(-1,-1,-1,-1, tileIcon, tileIconAlt);
   78       }
   79   
   80       /**
   81        * Paints the matte border.
   82        */
   83       public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
   84           Insets insets = getBorderInsets(c);
   85           Color oldColor = g.getColor();
   86           g.translate(x, y);
   87   
   88           // If the tileIcon failed loading, paint as gray.
   89           if (this.tileIcon != null) {
   90               this.color = (this.tileIcon.getIconWidth() == -1) ?
   91   				Color.gray : null;
   92           }
   93   
   94           if (this.color != null) {
   95               g.setColor(this.color);
   96               g.fillRect(0, 0, width - insets.right, insets.top);
   97               g.fillRect(0, insets.top, insets.left, height - insets.top);
   98               g.fillRect(insets.left, height - insets.bottom, width - insets.left, insets.bottom);
   99               g.fillRect(width - insets.right, 0, insets.right, height - insets.bottom);
  100           } else if (this.tileIcon != null) {
  101   
  102               int tileW = tileIcon.getIconWidth();
  103               int tileH = tileIcon.getIconHeight();
  104   		
  105               int xpos, ypos, startx, starty;
  106               Graphics cg;
  107   			
  108               // Paint top matte edge
  109   			boolean alt = false;			
  110               cg = g.create();
  111               cg.setClip(0, 0, width, insets.top);
  112               for (ypos = 0; insets.top - ypos > 0; ypos += tileH) {
  113                   for (xpos = 0; width - xpos > 0; xpos += tileW) {
  114   					if (alt) {	// alt image
  115   						this.tileIconAlt.paintIcon(c, cg, xpos, ypos);
  116   						alt = false;
  117   					} else {	// normal
  118   						this.tileIcon.paintIcon(c, cg, xpos, ypos);
  119   						alt = true;						
  120   					}
  121                   }
  122               }
  123               cg.dispose();
  124   
  125               // Paint left matte edge
  126               cg = g.create();
  127               cg.setClip(0, insets.top, insets.left, height - insets.top);
  128               starty = insets.top - (insets.top%tileH);
  129               startx = 0;
  130               for (ypos = starty; height - ypos > 0; ypos += tileH) {
  131                   for (xpos = startx; insets.left - xpos > 0; xpos += tileW) {
  132   					if (alt) {	// alt image
  133   						this.tileIconAlt.paintIcon(c, cg, xpos, ypos);
  134   						alt = false;
  135   					} else {	// normal
  136   						this.tileIcon.paintIcon(c, cg, xpos, ypos);
  137   						alt = true;						
  138   					}
  139                   }
  140               }
  141               cg.dispose();
  142   
  143               // Paint bottom matte edge
  144               cg = g.create();
  145               cg.setClip(insets.left, height - insets.bottom, width - insets.left, insets.bottom);
  146               starty = (height - insets.bottom) - ((height - insets.bottom)%tileH);
  147               startx = insets.left - (insets.left%tileW);
  148               for (ypos = starty; height - ypos > 0; ypos += tileH) {
  149                   for (xpos = startx; width - xpos > 0; xpos += tileW) {
  150   					if (alt) {	// alt image
  151   						this.tileIconAlt.paintIcon(c, cg, xpos, ypos);
  152   						alt = false;
  153   					} else {	// normal
  154   						this.tileIcon.paintIcon(c, cg, xpos, ypos);
  155   						alt = true;						
  156   					}
  157                   }
  158               }
  159               cg.dispose();
  160   
  161               // Paint right matte edge
  162               cg = g.create();
  163               cg.setClip(width - insets.right, insets.top, insets.right, height - insets.top - insets.bottom);
  164               starty = insets.top - (insets.top%tileH);
  165               startx = width - insets.right - ((width - insets.right)%tileW);
  166               for (ypos = starty; height - ypos > 0; ypos += tileH) {
  167                   for (xpos = startx; width - xpos > 0; xpos += tileW) {
  168   					if (alt) {	// alt image
  169   						this.tileIconAlt.paintIcon(c, cg, xpos, ypos);
  170   						alt = false;
  171   					} else {	// normal
  172   						this.tileIcon.paintIcon(c, cg, xpos, ypos);
  173   						alt = true;						
  174   					}
  175                   }
  176               }
  177               cg.dispose();
  178           }
  179           g.translate(-x, -y);
  180           g.setColor(oldColor);
  181       }
  182   }

Save This Page
Home » openjdk-7 » net.jbeans » ui » border » [javadoc | source]