1 7 8 package java.awt; 9 10 import java.awt.geom.Point2D ; 11 import java.awt.geom.Rectangle2D ; 12 import java.awt.geom.AffineTransform ; 13 import java.awt.image.ColorModel ; 14 15 40 41 public class GradientPaint implements Paint { 42 Point2D.Float p1; 43 Point2D.Float p2; 44 Color color1; 45 Color color2; 46 boolean cyclic; 47 48 64 public GradientPaint(float x1, 65 float y1, 66 Color color1, 67 float x2, 68 float y2, 69 Color color2) { 70 if ((color1 == null) || (color2 == null)) { 71 throw new NullPointerException ("Colors cannot be null"); 72 } 73 74 p1 = new Point2D.Float (x1, y1); 75 p2 = new Point2D.Float (x2, y2); 76 this.color1 = color1; 77 this.color2 = color2; 78 } 79 80 91 public GradientPaint(Point2D pt1, 92 Color color1, 93 Point2D pt2, 94 Color color2) { 95 if ((color1 == null) || (color2 == null) || 96 (pt1 == null) || (pt2 == null)) { 97 throw new NullPointerException ("Colors and points should be non-null"); 98 } 99 100 p1 = new Point2D.Float ((float)pt1.getX(), (float)pt1.getY()); 101 p2 = new Point2D.Float ((float)pt2.getX(), (float)pt2.getY()); 102 this.color1 = color1; 103 this.color2 = color2; 104 } 105 106 124 public GradientPaint(float x1, 125 float y1, 126 Color color1, 127 float x2, 128 float y2, 129 Color color2, 130 boolean cyclic) { 131 this (x1, y1, color1, x2, y2, color2); 132 this.cyclic = cyclic; 133 } 134 135 151 public GradientPaint(Point2D pt1, 152 Color color1, 153 Point2D pt2, 154 Color color2, 155 boolean cyclic) { 156 this (pt1, color1, pt2, color2); 157 this.cyclic = cyclic; 158 } 159 160 166 public Point2D getPoint1() { 167 return new Point2D.Float (p1.x, p1.y); 168 } 169 170 175 public Color getColor1() { 176 return color1; 177 } 178 179 185 public Point2D getPoint2() { 186 return new Point2D.Float (p2.x, p2.y); 187 } 188 189 194 public Color getColor2() { 195 return color2; 196 } 197 198 204 public boolean isCyclic() { 205 return cyclic; 206 } 207 208 223 public PaintContext createContext(ColorModel cm, 224 Rectangle deviceBounds, 225 Rectangle2D userBounds, 226 AffineTransform xform, 227 RenderingHints hints) { 228 229 return new GradientPaintContext (cm, p1, p2, xform, 230 color1, color2, cyclic); 231 } 232 233 239 public int getTransparency() { 240 int a1 = color1.getAlpha(); 241 int a2 = color2.getAlpha(); 242 return (((a1 & a2) == 0xff) ? OPAQUE : TRANSLUCENT); 243 } 244 245 } 246 | Popular Tags |