1 40 package org.dspace.app.mediafilter; 41 42 import java.io.File ; 43 import java.io.IOException ; 44 import java.awt.image.BufferedImage ; 45 import java.awt.Color ; 46 import java.awt.Font ; 47 import java.awt.FontMetrics ; 48 import java.awt.Graphics2D ; 49 import java.awt.Rectangle ; 50 import javax.imageio.ImageIO ; 51 import org.apache.log4j.Logger; 52 import org.dspace.core.ConfigurationManager; 53 54 59 60 public class Brand 61 { 62 private int brandWidth; 63 private int brandHeight; 64 private Font font; 65 private int xOffset; 66 67 76 public Brand(int brandWidth, 77 int brandHeight, 78 Font font, 79 int xOffset) 80 { 81 this.brandWidth = brandWidth; 82 this.brandHeight = brandHeight; 83 this.font = font; 84 this.xOffset = xOffset; 85 } 86 87 98 public BufferedImage create(String brandLeftText, 99 String shortLeftText, 100 String brandRightText) 101 { 102 BrandText[] allBrandText = null; 103 104 BufferedImage brandImage = 105 new BufferedImage (brandWidth, brandHeight, BufferedImage.TYPE_INT_RGB); 106 107 if (brandWidth >= 350) 108 { 109 allBrandText = new BrandText[] 110 { 111 new BrandText(BrandText.BL, brandLeftText), 112 new BrandText(BrandText.BR, brandRightText) 113 }; 114 } 115 else if (brandWidth >= 190) 116 { 117 allBrandText = new BrandText[] 118 { 119 new BrandText(BrandText.BL, shortLeftText), 120 new BrandText(BrandText.BR, brandRightText) 121 }; 122 } 123 else 124 { 125 allBrandText = new BrandText[] 126 { 127 new BrandText(BrandText.BR, brandRightText) 128 }; 129 } 130 131 if (allBrandText != null && allBrandText.length > 0) 132 { 133 for (int i = 0; i < allBrandText.length; ++i) 134 { 135 drawImage(brandImage, allBrandText[i]); 136 } 137 } 138 139 return brandImage; 140 } 141 142 143 150 private void drawImage(BufferedImage brandImage, 151 BrandText brandText) 152 { 153 int imgWidth = brandImage.getWidth(); 154 int imgHeight = brandImage.getHeight(); 155 156 int bx, by, tx, ty, bWidth, bHeight; 157 158 Graphics2D g2 = brandImage.createGraphics(); 159 g2.setFont(font); 160 FontMetrics fm = g2.getFontMetrics(); 161 162 163 bWidth = fm.stringWidth(brandText.getText()) + xOffset * 2 + 1; 164 bHeight = fm.getHeight(); 165 166 bx = by = 0; 167 168 if (brandText.getLocation().equals(BrandText.TL)) 169 { 170 bx = 0; 171 by = 0; 172 } 173 else if (brandText.getLocation().equals(BrandText.TR)) 174 { 175 bx = imgWidth - bWidth; 176 by = 0; 177 } 178 else if (brandText.getLocation().equals(BrandText.BL)) 179 { 180 bx = 0; 181 by = imgHeight - bHeight; 182 } 183 else if (brandText.getLocation().equals(BrandText.BR)) 184 { 185 bx = imgWidth - bWidth; 186 by = imgHeight - bHeight; 187 } 188 189 Rectangle box = new Rectangle (bx, by, bWidth, bHeight); 190 tx = bx + xOffset; 191 ty = by + fm.getAscent(); 192 193 g2.setColor(Color.black); 194 g2.fill(box); 195 g2.setColor(Color.white); 196 g2.drawString(brandText.getText(), tx, ty); 197 } 198 } 199 | Popular Tags |