1 package rero.gui.background; 2 3 import java.awt.*; 4 import java.awt.image.*; 5 6 import java.io.*; 7 8 import javax.swing.*; 9 import java.util.*; 10 11 public class BackgroundUtil 12 { 13 public static final int STYLE_TILE = 0; public static final int STYLE_CENTER = 1; public static final int STYLE_FILL = 2; public static final int STYLE_STRETCHED = 3; 18 public static final int BG_DEFAULT = 0; 19 public static final int BG_SOLID = 1; 20 public static final int BG_TRANSPARENT = 2; 21 public static final int BG_IMAGE = 3; 22 23 24 public static void drawBackground(Component source, Graphics g, BackgroundProperties bgConfig) 25 { 26 if (bgConfig.getImage(source) == null) 27 { 28 drawSafeBackground(source, g, bgConfig); return; 30 } 31 32 switch(bgConfig.getStyle()) 33 { 34 case STYLE_TILE: 35 drawImageTiled(source, g, bgConfig); 36 break; 37 case STYLE_FILL: 38 drawImageFill(source, g, bgConfig); 39 break; 40 case STYLE_STRETCHED: 41 drawImageStretched(source, g, bgConfig); 42 break; 43 case STYLE_CENTER: 44 drawImageCentered(source, g, bgConfig); 45 break; 46 default: 47 drawImageTiled(source, g, bgConfig); 48 break; 49 } 50 } 51 52 public static void drawTintedTransparency(Component source, Graphics g, BackgroundProperties bgProperties) 53 { 54 if (bgProperties.getTint() > 0) 55 { 56 int x, y, width, height; 57 58 x = g.getClipBounds().x; 59 y = g.getClipBounds().y; 60 width = g.getClipBounds().width; 61 height = g.getClipBounds().height; 62 63 g.setColor(bgProperties.getColor()); 64 65 Graphics2D g2 = (Graphics2D)g; 66 Composite oc = g2.getComposite(); 67 AlphaComposite ac = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, bgProperties.getTint()); 68 69 g2.setComposite(ac); 70 g2.fillRect(x, y, width, height); 71 72 g2.setComposite(oc); 73 } 74 } 75 76 public static void drawSafeBackground(Component source, Graphics g, BackgroundProperties bgProperties) 77 { 78 int x, y, width, height; 79 80 x = g.getClipBounds().x; 81 y = g.getClipBounds().y; 82 width = g.getClipBounds().width; 83 height = g.getClipBounds().height; 84 85 g.setColor(bgProperties.getColor()); 86 87 g.fillRect(x, y, width, height); 88 } 89 90 91 public static void drawImageFill(Component source, Graphics g, BackgroundProperties properties) 92 { 93 Image temp = properties.getTransformedImage(); 94 95 Dimension compare = null; 96 int tx = 0, ty = 0; 97 98 if (properties.isRelative()) 99 { 100 compare = Toolkit.getDefaultToolkit().getScreenSize(); 101 tx = Math.abs(source.getLocationOnScreen().x) % Toolkit.getDefaultToolkit().getScreenSize().width; 102 ty = Math.abs(source.getLocationOnScreen().y) % Toolkit.getDefaultToolkit().getScreenSize().height; 103 104 } 107 else 108 { 109 compare = source.getSize(); 110 } 111 112 if (temp == null || temp.getWidth(null) != (int)compare.getWidth() || temp.getHeight(null) != (int)compare.getHeight()) 113 { 114 Image imageData = properties.getImage(source); 115 116 Image cached = source.createImage((int)compare.getWidth(), (int)compare.getHeight()); 117 118 Graphics2D g2 = (Graphics2D)cached.getGraphics(); 119 g2.drawImage(imageData, 0, 0, (int)compare.getWidth(), (int)compare.getHeight(), null); 120 g2.dispose(); 121 122 properties.setTransformedImage(cached); 123 124 g.drawImage(cached, 0 - tx, 0 - ty, null); 125 return; 126 } 127 128 g.drawImage(temp, 0 - tx, 0 - ty, null); 129 } 130 131 public static void drawImageCentered(Component source, Graphics g, BackgroundProperties properties) 132 { 133 Image temp = properties.getTransformedImage(); 134 135 Dimension compare = null; 136 int tx = 0, ty = 0; 137 138 if (properties.isRelative()) 139 { 140 compare = Toolkit.getDefaultToolkit().getScreenSize(); 141 tx = Math.abs(source.getLocationOnScreen().x) % Toolkit.getDefaultToolkit().getScreenSize().width; 142 ty = Math.abs(source.getLocationOnScreen().y) % Toolkit.getDefaultToolkit().getScreenSize().height; 143 } 144 else 145 { 146 compare = source.getSize(); 147 } 148 149 if (temp == null || temp.getWidth(null) != (int)compare.getWidth() || temp.getHeight(null) != (int)compare.getHeight()) 150 { 151 Image imageData = properties.getImage(source); 152 153 Image cached = source.createImage((int)compare.getWidth(), (int)compare.getHeight()); 154 155 int x_offset = ((int)compare.getWidth() - imageData.getWidth(null)) / 2; 156 int y_offset = ((int)compare.getHeight() - imageData.getHeight(null)) / 2; 157 158 Graphics2D g2 = (Graphics2D)cached.getGraphics(); 159 160 g2.setColor(properties.getColor()); 161 g2.fillRect(0, 0, (int)compare.getWidth(), (int)compare.getHeight()); 162 163 g2.drawImage(imageData, x_offset, y_offset, imageData.getWidth(null), imageData.getHeight(null), null); 164 165 g2.dispose(); 166 167 properties.setTransformedImage(cached); 168 169 g.drawImage(cached, 0 - tx, 0 - ty, null); 170 return; 171 } 172 173 g.drawImage(temp, 0 - tx, 0 - ty, null); 174 } 175 176 public static void drawImageStretched(Component source, Graphics g, BackgroundProperties properties) 177 { 178 Image temp = properties.getTransformedImage(); 179 180 Dimension compare = null; 181 int tx = 0, ty = 0; 182 183 if (properties.isRelative()) 184 { 185 compare = Toolkit.getDefaultToolkit().getScreenSize(); 186 tx = Math.abs(source.getLocationOnScreen().x) % Toolkit.getDefaultToolkit().getScreenSize().width; 187 ty = Math.abs(source.getLocationOnScreen().y) % Toolkit.getDefaultToolkit().getScreenSize().height; 188 } 189 else 190 { 191 compare = source.getSize(); 192 } 193 194 if (temp == null || temp.getWidth(null) != (int)compare.getWidth() || temp.getHeight(null) != (int)compare.getHeight()) 195 { 196 Image imageData = properties.getImage(source); 197 198 Image cached = source.createImage((int)compare.getWidth(), (int)compare.getHeight()); 199 200 202 float ratio; 203 204 float x_diff = ((float)compare.getWidth() / (float)imageData.getWidth(null)); 205 float y_diff = ((float)compare.getHeight() / (float)imageData.getHeight(null)); 206 207 if (x_diff > y_diff) 209 { 210 ratio = x_diff; 211 } 212 else 213 { 214 ratio = y_diff; 215 } 216 217 int width = (int)(imageData.getWidth(null) * ratio); 218 int height = (int)(imageData.getHeight(null) * ratio); 219 220 int x_offset = ((int)compare.getWidth() - width) / 2; 221 int y_offset = ((int)compare.getHeight() - height) / 2; 222 223 Graphics2D g2 = (Graphics2D)cached.getGraphics(); 224 225 g2.setColor(properties.getColor()); 226 g2.fillRect(0, 0, (int)compare.getWidth(), (int)compare.getHeight()); 227 228 g2.drawImage(imageData, x_offset, y_offset, width, height, null); 229 230 g2.dispose(); 231 232 properties.setTransformedImage(cached); 233 234 g.drawImage(cached, 0 - tx, 0 - ty, null); 235 return; 236 } 237 238 g.drawImage(temp, 0 - tx, 0 - ty, null); 239 } 240 241 242 public static void drawImageTiled(Component source, Graphics g, BackgroundProperties properties) 243 { 244 Image imageData = properties.getImage(source); 245 246 int checkY = g.getClipBounds().y; 247 int checkH = g.getClipBounds().height+checkY; 248 checkY -= imageData.getHeight(null); 249 250 int checkX = g.getClipBounds().x; 251 int checkL = g.getClipBounds().width+checkX; 252 checkX -= imageData.getWidth(null); 253 254 int tx = 0; 255 int ty = 0; 256 257 if (properties.isRelative()) 258 { 259 tx = Math.abs(source.getLocationOnScreen().x) % Toolkit.getDefaultToolkit().getScreenSize().width; 260 ty = Math.abs(source.getLocationOnScreen().y) % Toolkit.getDefaultToolkit().getScreenSize().height; 261 } 262 263 int x = source.getWidth(); 264 int y = source.getHeight(); 265 266 int x1 = 0-tx; 267 int y1 = 0-ty; 268 while (x1 < x) 269 { 270 while (y1 < y) 271 { 272 if ((x1 >= checkX) && (x1 <= checkL) && (y1 >= checkY) && (y1 <= checkH)) 273 { 274 g.drawImage(imageData, x1, y1, null); 275 } 276 y1 += imageData.getHeight(null); 277 } 278 x1 += imageData.getWidth(null); 279 y1 = 0-ty; 280 } 281 } 282 283 284 public static Image getManagedImage(Component source, String file, float tint, Color solid) 285 { 286 if (!(new File(file)).exists()) 287 { 288 return null; 289 } 290 291 Image imageData = new javax.swing.ImageIcon (file).getImage(); 292 293 if (tint > 0) 294 { 295 Image imageDataTinted = source.createImage(imageData.getWidth(null), imageData.getHeight(null)); 296 297 Graphics2D g2 = (Graphics2D)imageDataTinted.getGraphics(); 298 Composite oc = g2.getComposite(); 299 300 g2.drawImage(imageData, 0, 0, null); 301 302 AlphaComposite ac = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, tint); 303 g2.setComposite(ac); 304 g2.setColor(solid); 305 g2.fillRect(0, 0, imageDataTinted.getWidth(null), imageDataTinted.getHeight(null)); 306 307 g2.dispose(); 308 309 return imageDataTinted; 310 } 311 312 return imageData; 313 } 314 } 315 316 | Popular Tags |