1 39 40 package org.jfree.chart; 41 42 import java.awt.geom.Rectangle2D ; 43 import java.io.Serializable ; 44 45 55 public class Spacer implements Serializable { 56 57 58 public static final int RELATIVE = 0; 59 60 61 public static final int ABSOLUTE = 1; 62 63 64 private int type; 65 66 67 private double left; 68 69 70 private double right; 71 72 73 private double top; 74 75 76 private double bottom; 77 78 92 public Spacer(int type, double left, double top, double right, double bottom) { 93 94 this.type = type; 95 this.left = left; 96 this.top = top; 97 this.right = right; 98 this.bottom = bottom; 99 100 } 101 102 111 public double getLeftSpace(double width) { 112 113 double result = 0.0; 114 115 if (type == ABSOLUTE) { 116 result = left; 117 } 118 else if (type == RELATIVE) { 119 result = left * width; 120 } 121 122 return result; 123 124 } 125 126 135 public double getRightSpace(double width) { 136 137 double result = 0.0; 138 139 if (type == ABSOLUTE) { 140 result = right; 141 } 142 else if (type == RELATIVE) { 143 result = right * width; 144 } 145 146 return result; 147 148 } 149 150 159 public double getTopSpace(double height) { 160 161 double result = 0.0; 162 163 if (type == ABSOLUTE) { 164 result = top; 165 } 166 else if (type == RELATIVE) { 167 result = top * height; 168 } 169 170 return result; 171 172 } 173 174 183 public double getBottomSpace(double height) { 184 185 double result = 0.0; 186 187 if (type == ABSOLUTE) { 188 result = bottom; 189 } 190 else if (type == RELATIVE) { 191 result = bottom * height; 192 } 193 194 return result; 195 196 } 197 198 205 public double getAdjustedWidth(double width) { 206 207 double result = width; 208 209 if (type == ABSOLUTE) { 210 result = result + left + right; 211 } 212 else if (type == RELATIVE) { 213 result = result + (left * width) + (right * width); 214 } 215 216 return result; 217 218 } 219 220 227 public double getAdjustedHeight(double height) { 228 229 double result = height; 230 231 if (type == ABSOLUTE) { 232 result = result + top + bottom; 233 } 234 else if (type == RELATIVE) { 235 result = result + (top * height) + (bottom * height); 236 } 237 238 return result; 239 240 } 241 242 247 public void trim(Rectangle2D area) { 248 double x = area.getX(); 249 double y = area.getY(); 250 double h = area.getHeight(); 251 double w = area.getWidth(); 252 double l = getLeftSpace(w); 253 double r = getRightSpace(w); 254 double t = getTopSpace(h); 255 double b = getBottomSpace(h); 256 area.setRect(x + l, y + t, w - l - r, h - t - b); 257 } 258 259 266 public boolean equals(Object obj) { 267 268 if (obj == null) { 269 return false; 270 } 271 272 if (obj == this) { 273 return true; 274 } 275 276 if (obj instanceof Spacer) { 277 Spacer s = (Spacer) obj; 278 boolean b0 = (this.type == s.type); 279 boolean b1 = (this.left == s.left); 280 boolean b2 = (this.right == s.right); 281 boolean b3 = (this.top == s.top); 282 boolean b4 = (this.bottom == s.bottom); 283 return b0 && b1 && b2 && b3 && b4; 284 } 285 286 return false; 287 } 288 289 } 290 | Popular Tags |