Source for java.awt.BasicStroke

   1: /* BasicStroke.java -- 
   2:    Copyright (C) 2002, 2003, 2004, 2005  Free Software Foundation, Inc.
   3: 
   4: This file is part of GNU Classpath.
   5: 
   6: GNU Classpath is free software; you can redistribute it and/or modify
   7: it under the terms of the GNU General Public License as published by
   8: the Free Software Foundation; either version 2, or (at your option)
   9: any later version.
  10: 
  11: GNU Classpath is distributed in the hope that it will be useful, but
  12: WITHOUT ANY WARRANTY; without even the implied warranty of
  13: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14: General Public License for more details.
  15: 
  16: You should have received a copy of the GNU General Public License
  17: along with GNU Classpath; see the file COPYING.  If not, write to the
  18: Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  19: 02110-1301 USA.
  20: 
  21: Linking this library statically or dynamically with other modules is
  22: making a combined work based on this library.  Thus, the terms and
  23: conditions of the GNU General Public License cover the whole
  24: combination.
  25: 
  26: As a special exception, the copyright holders of this library give you
  27: permission to link this library with independent modules to produce an
  28: executable, regardless of the license terms of these independent
  29: modules, and to copy and distribute the resulting executable under
  30: terms of your choice, provided that you also meet, for each linked
  31: independent module, the terms and conditions of the license of that
  32: module.  An independent module is a module which is not derived from
  33: or based on this library.  If you modify this library, you may extend
  34: this exception to your version of the library, but you are not
  35: obligated to do so.  If you do not wish to do so, delete this
  36: exception statement from your version. */
  37: 
  38: 
  39: package java.awt;
  40: 
  41: import java.util.Arrays;
  42: 
  43: /**
  44:  * STUB CLASS ONLY
  45:  */
  46: public class BasicStroke implements Stroke
  47: {
  48:   public static final int JOIN_MITER = 0;
  49:   public static final int JOIN_ROUND = 1;
  50:   public static final int JOIN_BEVEL = 2;
  51: 
  52:   public static final int CAP_BUTT = 0;
  53:   public static final int CAP_ROUND = 1;
  54:   public static final int CAP_SQUARE = 2;
  55: 
  56:   private final float width;
  57:   private final int cap;
  58:   private final int join;
  59:   private final float limit;
  60:   private final float[] dash;
  61:   private final float phase;
  62: 
  63:   /**
  64:    * Creates a basic stroke.
  65:    *
  66:    * @param width May not be negative .
  67:    * @param cap May be either CAP_BUTT, CAP_ROUND or CAP_SQUARE.
  68:    * @param join May be either JOIN_ROUND, JOIN_BEVEL, or JOIN_MITER.
  69:    * @param miterlimit the limit to trim the miter join. The miterlimit must be
  70:    * greater than or equal to 1.0f.
  71:    * @param dash The array representing the dashing pattern. There must be at
  72:    * least one non-zero entry.
  73:    * @param dashPhase is negative and dash is not null.
  74:    *
  75:    * @exception IllegalArgumentException If one input parameter doesn't meet
  76:    * its needs.
  77:    */
  78:   public BasicStroke(float width, int cap, int join, float miterlimit,
  79:                      float[] dash, float dashPhase)
  80:   {
  81:     if (width < 0.0f )
  82:       throw new IllegalArgumentException("width " + width + " < 0");
  83:     else if (cap < CAP_BUTT || cap > CAP_SQUARE)
  84:       throw new IllegalArgumentException("cap " + cap + " out of range ["
  85:                      + CAP_BUTT + ".." + CAP_SQUARE + "]");
  86:     else if (miterlimit < 1.0f && join == JOIN_MITER)
  87:       throw new IllegalArgumentException("miterlimit " + miterlimit
  88:                      + " < 1.0f while join == JOIN_MITER");
  89:     else if (join < JOIN_MITER || join > JOIN_BEVEL)
  90:       throw new IllegalArgumentException("join " + join + " out of range ["
  91:                      + JOIN_MITER + ".." + JOIN_BEVEL
  92:                      + "]");
  93:     else if (dashPhase < 0.0f && dash != null)
  94:       throw new IllegalArgumentException("dashPhase " + dashPhase
  95:                      + " < 0.0f while dash != null");
  96:     else if (dash != null)
  97:       if (dash.length == 0)
  98:     throw new IllegalArgumentException("dash.length is 0");
  99:       else
 100:     {
 101:       boolean allZero = true;
 102:       
 103:       for ( int i = 0; i < dash.length; ++i)
 104:         {
 105:           if (dash[i] != 0.0f)
 106:         {
 107:           allZero = false;
 108:           break;
 109:         }
 110:         }
 111:       
 112:       if (allZero)
 113:         throw new IllegalArgumentException("all dashes are 0.0f");
 114:     }
 115: 
 116:     this.width = width;
 117:     this.cap = cap;
 118:     this.join = join;
 119:     limit = miterlimit;
 120:     this.dash = dash == null ? null : (float[]) dash.clone();
 121:     phase = dashPhase;
 122:   }
 123: 
 124:   /**
 125:    * Creates a basic stroke.
 126:    *
 127:    * @param width The width of the BasicStroke. May not be negative .
 128:    * @param cap May be either CAP_BUTT, CAP_ROUND or CAP_SQUARE.
 129:    * @param join May be either JOIN_ROUND, JOIN_BEVEL, or JOIN_MITER.
 130:    * @param miterlimit the limit to trim the miter join. The miterlimit must be
 131:    * greater than or equal to 1.0f.
 132:    * 
 133:    * @exception IllegalArgumentException If one input parameter doesn't meet
 134:    * its needs.
 135:    */
 136:   public BasicStroke(float width, int cap, int join, float miterlimit)
 137:   {
 138:     this(width, cap, join, miterlimit, null, 0);
 139:   }
 140: 
 141:   /**
 142:    * Creates a basic stroke.
 143:    *
 144:    * @param width The width of the BasicStroke. May not be nehative.
 145:    * @param cap May be either CAP_BUTT, CAP_ROUND or CAP_SQUARE.
 146:    * @param join May be either JOIN_ROUND, JOIN_BEVEL, or JOIN_MITER.
 147:    * 
 148:    * @exception IllegalArgumentException If one input parameter doesn't meet
 149:    * its needs.
 150:    * @exception IllegalArgumentException FIXME
 151:    */
 152:   public BasicStroke(float width, int cap, int join)
 153:   {
 154:     this(width, cap, join, 10, null, 0);
 155:   }
 156: 
 157:   /**
 158:    * Creates a basic stroke.
 159:    *
 160:    * @param width The width of the BasicStroke.
 161:    * 
 162:    * @exception IllegalArgumentException If width is negative.
 163:    */
 164:   public BasicStroke(float width)
 165:   {
 166:     this(width, CAP_SQUARE, JOIN_MITER, 10, null, 0);
 167:   }
 168: 
 169:   /**
 170:    * Creates a basic stroke.
 171:    */
 172:   public BasicStroke()
 173:   {
 174:     this(1, CAP_SQUARE, JOIN_MITER, 10, null, 0);
 175:   }
 176:   
 177:   public Shape createStrokedShape(Shape s)
 178:   {
 179:     throw new Error("not implemented");
 180:   }
 181: 
 182:   public float getLineWidth()
 183:   {
 184:     return width;
 185:   }
 186: 
 187:   public int getEndCap()
 188:   {
 189:     return cap;
 190:   }
 191: 
 192:   public int getLineJoin()
 193:   {
 194:     return join;
 195:   }
 196: 
 197:   public float getMiterLimit()
 198:   {
 199:     return limit;
 200:   }
 201: 
 202:   public float[] getDashArray()
 203:   {
 204:     return dash;
 205:   }
 206: 
 207:   public float getDashPhase()
 208:   {
 209:     return phase;
 210:   }
 211: 
 212:   /**
 213:    * Returns the hash code for this object. The hash is calculated by
 214:    * xoring the hash, cap, join, limit, dash array and phase values
 215:    * (converted to <code>int</code> first with
 216:    * <code>Float.floatToIntBits()</code> if the value is a
 217:    * <code>float</code>).
 218:    */
 219:   public int hashCode()
 220:   {
 221:     int hash = Float.floatToIntBits(width);
 222:     hash ^= cap;
 223:     hash ^= join;
 224:     hash ^= Float.floatToIntBits(limit);
 225:    
 226:     if (dash != null)
 227:       for (int i = 0; i < dash.length; i++)
 228:     hash ^=  Float.floatToIntBits(dash[i]);
 229: 
 230:     hash ^= Float.floatToIntBits(phase);
 231: 
 232:     return hash;
 233:   }
 234: 
 235:   /**
 236:    * Returns true if the given Object is an instance of BasicStroke
 237:    * and the width, cap, join, limit, dash array and phase are all
 238:    * equal.
 239:    */
 240:   public boolean equals(Object o)
 241:   {
 242:     if (! (o instanceof BasicStroke))
 243:       return false;
 244:     BasicStroke s = (BasicStroke) o;
 245:     return width == s.width && cap == s.cap && join == s.join
 246:       && limit == s.limit && Arrays.equals(dash, s.dash) && phase == s.phase;
 247:   }
 248: } // class BasicStroke