1 55 56 package org.jfree.chart.title; 57 58 import java.awt.Graphics2D ; 59 import java.awt.Image ; 60 import java.awt.geom.Rectangle2D ; 61 62 import org.jfree.chart.event.TitleChangeEvent; 63 import org.jfree.ui.HorizontalAlignment; 64 import org.jfree.ui.RectangleEdge; 65 import org.jfree.ui.RectangleInsets; 66 import org.jfree.ui.Size2D; 67 import org.jfree.ui.VerticalAlignment; 68 69 83 public class ImageTitle extends Title { 84 85 86 private Image image; 87 88 93 public ImageTitle(Image image) { 94 this(image, image.getHeight(null), image.getWidth(null), 95 Title.DEFAULT_POSITION, Title.DEFAULT_HORIZONTAL_ALIGNMENT, 96 Title.DEFAULT_VERTICAL_ALIGNMENT, Title.DEFAULT_PADDING); 97 } 98 99 107 public ImageTitle(Image image, RectangleEdge position, 108 HorizontalAlignment horizontalAlignment, 109 VerticalAlignment verticalAlignment) { 110 111 this(image, image.getHeight(null), image.getWidth(null), 112 position, horizontalAlignment, verticalAlignment, 113 Title.DEFAULT_PADDING); 114 } 115 116 129 public ImageTitle(Image image, int height, int width, 130 RectangleEdge position, 131 HorizontalAlignment horizontalAlignment, 132 VerticalAlignment verticalAlignment, 133 RectangleInsets padding) { 134 135 super(position, horizontalAlignment, verticalAlignment, padding); 136 if (image == null) { 137 throw new NullPointerException ("Null 'image' argument."); 138 } 139 this.image = image; 140 setHeight(height); 141 setWidth(width); 142 143 } 144 145 150 public Image getImage() { 151 return this.image; 152 } 153 154 160 public void setImage(Image image) { 161 if (image == null) { 162 throw new NullPointerException ("Null 'image' argument."); 163 } 164 this.image = image; 165 notifyListeners(new TitleChangeEvent(this)); 166 } 167 168 176 public void draw(Graphics2D g2, Rectangle2D titleArea) { 177 178 RectangleEdge position = getPosition(); 179 if (position == RectangleEdge.TOP || position == RectangleEdge.BOTTOM) { 180 drawHorizontal(g2, titleArea); 181 } 182 else if (position == RectangleEdge.LEFT 183 || position == RectangleEdge.RIGHT) { 184 drawVertical(g2, titleArea); 185 } 186 else { 187 throw new RuntimeException ("Invalid title position."); 188 } 189 } 190 191 201 protected Size2D drawHorizontal(Graphics2D g2, Rectangle2D chartArea) { 202 203 double startY = 0.0; 204 double topSpace = 0.0; 205 double bottomSpace = 0.0; 206 double leftSpace = 0.0; 207 double rightSpace = 0.0; 208 209 double w = getWidth(); 210 double h = getHeight(); 211 RectangleInsets padding = getPadding(); 212 topSpace = padding.calculateTopOutset(h); 213 bottomSpace = padding.calculateBottomOutset(h); 214 leftSpace = padding.calculateLeftOutset(w); 215 rightSpace = padding.calculateRightOutset(w); 216 217 if (getPosition() == RectangleEdge.TOP) { 218 startY = chartArea.getY() + topSpace; 219 } 220 else { 221 startY = chartArea.getY() + chartArea.getHeight() - bottomSpace - h; 222 } 223 224 HorizontalAlignment horizontalAlignment = getHorizontalAlignment(); 226 double startX = 0.0; 227 if (horizontalAlignment == HorizontalAlignment.CENTER) { 228 startX = chartArea.getX() + leftSpace + chartArea.getWidth() / 2.0 229 - w / 2.0; 230 } 231 else if (horizontalAlignment == HorizontalAlignment.LEFT) { 232 startX = chartArea.getX() + leftSpace; 233 } 234 else if (horizontalAlignment == HorizontalAlignment.RIGHT) { 235 startX = chartArea.getX() + chartArea.getWidth() - rightSpace - w; 236 } 237 g2.drawImage(this.image, (int) startX, (int) startY, (int) w, (int) h, 238 null); 239 240 return new Size2D(chartArea.getWidth() + leftSpace + rightSpace, 241 h + topSpace + bottomSpace); 242 243 } 244 245 255 protected Size2D drawVertical(Graphics2D g2, Rectangle2D chartArea) { 256 257 double startX = 0.0; 258 double topSpace = 0.0; 259 double bottomSpace = 0.0; 260 double leftSpace = 0.0; 261 double rightSpace = 0.0; 262 263 double w = getWidth(); 264 double h = getHeight(); 265 266 RectangleInsets padding = getPadding(); 267 if (padding != null) { 268 topSpace = padding.calculateTopOutset(h); 269 bottomSpace = padding.calculateBottomOutset(h); 270 leftSpace = padding.calculateLeftOutset(w); 271 rightSpace = padding.calculateRightOutset(w); 272 } 273 274 if (getPosition() == RectangleEdge.LEFT) { 275 startX = chartArea.getX() + leftSpace; 276 } 277 else { 278 startX = chartArea.getMaxX() - rightSpace - w; 279 } 280 281 VerticalAlignment alignment = getVerticalAlignment(); 283 double startY = 0.0; 284 if (alignment == VerticalAlignment.CENTER) { 285 startY = chartArea.getMinY() + topSpace 286 + chartArea.getHeight() / 2.0 - h / 2.0; 287 } 288 else if (alignment == VerticalAlignment.TOP) { 289 startY = chartArea.getMinY() + topSpace; 290 } 291 else if (alignment == VerticalAlignment.BOTTOM) { 292 startY = chartArea.getMaxY() - bottomSpace - h; 293 } 294 295 g2.drawImage(this.image, (int) startX, (int) startY, (int) w, (int) h, 296 null); 297 298 return new Size2D(chartArea.getWidth() + leftSpace + rightSpace, 299 h + topSpace + bottomSpace); 300 301 } 302 303 312 public Object draw(Graphics2D g2, Rectangle2D area, Object params) { 313 draw(g2, area); 314 return null; 315 } 316 317 } 318 | Popular Tags |