1 45 46 package org.jfree.chart; 47 48 import java.awt.Graphics2D ; 49 import java.awt.Image ; 50 import java.awt.geom.Rectangle2D ; 51 52 import org.jfree.chart.event.TitleChangeEvent; 53 import org.jfree.ui.Size2D; 54 55 69 public class ImageTitle extends AbstractTitle { 70 71 72 private Image image; 73 74 75 private int height; 76 77 78 private int width; 79 80 85 public ImageTitle(Image image) { 86 87 this(image, 88 image.getHeight(null), 89 image.getWidth(null), 90 AbstractTitle.DEFAULT_POSITION, 91 AbstractTitle.DEFAULT_HORIZONTAL_ALIGNMENT, 92 AbstractTitle.DEFAULT_VERTICAL_ALIGNMENT, 93 AbstractTitle.DEFAULT_SPACER); 94 95 } 96 97 108 public ImageTitle(Image image, int position, int horizontalAlignment, int verticalAlignment) { 109 110 this(image, 111 image.getHeight(null), 112 image.getWidth(null), 113 position, 114 horizontalAlignment, 115 verticalAlignment, 116 AbstractTitle.DEFAULT_SPACER); 117 118 } 119 120 135 public ImageTitle(Image image, int height, int width, int position, 136 int horizontalAlignment, int verticalAlignment, Spacer spacer) { 137 138 super(position, horizontalAlignment, verticalAlignment, spacer); 139 if (image == null) { 140 throw new NullPointerException ("ImageTitle(..): Image argument is null."); 141 } 142 this.image = image; 143 this.height = height; 144 this.width = width; 145 146 } 147 148 153 public Image getImage() { 154 return this.image; 155 } 156 157 163 public void setImage(Image image) { 164 165 if (image == null) { 166 throw new NullPointerException ("ImageTitle.setImage (..): Image must not be null."); 167 } 168 this.image = image; 169 notifyListeners(new TitleChangeEvent(this)); 170 171 } 172 173 179 public void draw(Graphics2D g2, Rectangle2D titleArea) { 180 181 int position = getPosition(); 182 if (position == TOP || position == BOTTOM) { 183 drawHorizontal(g2, titleArea); 184 } 185 else if (position == LEFT || position == RIGHT) { 186 drawVertical(g2, titleArea); 187 } 188 else { 189 throw new RuntimeException ("ImageTitle.draw(...) - invalid title position."); 190 } 191 } 192 193 200 public boolean isValidPosition(int position) { 201 202 switch (position) { 203 case TOP: 204 case BOTTOM: 205 case RIGHT: 206 case LEFT: 207 return true; 208 default : 209 return false; 210 } 211 212 } 213 214 221 public double getPreferredWidth(Graphics2D g2) { 222 223 double result = this.width; 224 225 Spacer spacer = getSpacer(); 226 result = spacer.getAdjustedWidth(result); 228 230 return result; 231 232 } 233 234 241 public double getPreferredHeight(Graphics2D g2) { 242 243 double result = this.height; 244 245 Spacer spacer = getSpacer(); 246 result = spacer.getAdjustedHeight(result); 249 251 return result; 252 253 } 254 255 263 protected Size2D drawHorizontal(Graphics2D g2, Rectangle2D chartArea) { 264 265 double startY = 0.0; 266 double topSpace = 0.0; 267 double bottomSpace = 0.0; 268 double leftSpace = 0.0; 269 double rightSpace = 0.0; 270 271 Spacer spacer = getSpacer(); 272 topSpace = spacer.getTopSpace(this.height); 275 bottomSpace = spacer.getBottomSpace(this.height); 276 leftSpace = spacer.getLeftSpace(this.width); 277 rightSpace = spacer.getRightSpace(this.width); 278 280 if (getPosition() == TOP) { 281 startY = chartArea.getY() + topSpace; 282 } 283 else { 284 startY = chartArea.getY() + chartArea.getHeight() - bottomSpace - this.height; 285 } 286 287 int horizontalAlignment = getHorizontalAlignment(); 289 double startX = 0.0; 290 if (horizontalAlignment == CENTER) { 291 startX = chartArea.getX() + leftSpace + chartArea.getWidth() / 2 - this.width / 2; 292 } 293 else { 294 if (horizontalAlignment == LEFT) { 295 startX = chartArea.getX() + leftSpace; 296 } 297 else { 298 if (horizontalAlignment == RIGHT) { 299 startX = chartArea.getX() + chartArea.getWidth() - rightSpace - this.width; 300 } 301 } 302 } 303 304 g2.drawImage(image, (int) startX, (int) startY, this.width, this.height, null); 305 306 return new Size2D(chartArea.getWidth() + leftSpace + rightSpace, 307 this.height + topSpace + bottomSpace); 308 309 } 310 311 319 protected Size2D drawVertical(Graphics2D g2, Rectangle2D chartArea) { 320 321 double startX = 0.0; 322 double topSpace = 0.0; 323 double bottomSpace = 0.0; 324 double leftSpace = 0.0; 325 double rightSpace = 0.0; 326 327 Spacer spacer = getSpacer(); 328 if (spacer != null) { 329 topSpace = spacer.getTopSpace(this.height); 330 bottomSpace = spacer.getBottomSpace(this.height); 331 leftSpace = spacer.getLeftSpace(this.width); 332 rightSpace = spacer.getRightSpace(this.width); 333 } 334 335 if (getPosition() == LEFT) { 336 startX = chartArea.getX() + leftSpace; 337 } 338 else { 339 startX = chartArea.getMaxX() - rightSpace - this.width; 340 } 341 342 int alignment = getVerticalAlignment(); 344 double startY = 0.0; 345 if (alignment == MIDDLE) { 346 startY = chartArea.getMinY() + topSpace + chartArea.getHeight() / 2 - this.height / 2; 347 } 348 else { 349 if (alignment == TOP) { 350 startY = chartArea.getMinY() + topSpace; 351 } 352 else { 353 if (alignment == BOTTOM) { 354 startY = chartArea.getMaxY() - bottomSpace - this.height; 355 } 356 } 357 } 358 359 g2.drawImage(image, (int) startX, (int) startY, this.width, this.height, null); 360 361 return new Size2D(chartArea.getWidth() + leftSpace + rightSpace, 362 this.height + topSpace + bottomSpace); 363 364 } 365 366 } 367 | Popular Tags |