1 7 package com.sun.java.swing.plaf.nimbus; 8 9 import java.awt.Graphics ; 10 import java.awt.Image ; 11 import java.awt.Insets ; 12 13 19 class ImageScalingHelper { 20 21 22 enum PaintType { 23 27 CENTER, 28 29 33 TILE, 34 35 39 PAINT9_STRETCH, 40 41 45 PAINT9_TILE 46 } 47 48 ; 49 50 private static final Insets EMPTY_INSETS = new Insets (0, 0, 0, 0); 51 52 static final int PAINT_TOP_LEFT = 1; 53 static final int PAINT_TOP = 2; 54 static final int PAINT_TOP_RIGHT = 4; 55 static final int PAINT_LEFT = 8; 56 static final int PAINT_CENTER = 16; 57 static final int PAINT_RIGHT = 32; 58 static final int PAINT_BOTTOM_RIGHT = 64; 59 static final int PAINT_BOTTOM = 128; 60 static final int PAINT_BOTTOM_LEFT = 256; 61 65 static final int PAINT_ALL = 512; 66 67 85 public static void paint(Graphics g, int x, int y, int w, int h, 86 Image image, Insets sInsets, 87 Insets dInsets, PaintType paintType, int mask) { 88 if (image == null || image.getWidth(null) <= 0 || image.getHeight(null) <= 0) { 89 return; 90 } 91 if (sInsets == null) { 92 sInsets = EMPTY_INSETS; 93 } 94 if (dInsets == null) { 95 dInsets = EMPTY_INSETS; 96 } 97 int iw = image.getWidth(null); 98 int ih = image.getHeight(null); 99 100 if (paintType == PaintType.CENTER) { 101 g.drawImage(image, x + (w - iw) / 2, 103 y + (h - ih) / 2, null); 104 } else if (paintType == PaintType.TILE) { 105 int lastIY = 0; 107 for (int yCounter = y, maxY = y + h; yCounter < maxY; 108 yCounter += (ih - lastIY), lastIY = 0) { 109 int lastIX = 0; 110 for (int xCounter = x, maxX = x + w; xCounter < maxX; 111 xCounter += (iw - lastIX), lastIX = 0) { 112 int dx2 = Math.min(maxX, xCounter + iw - lastIX); 113 int dy2 = Math.min(maxY, yCounter + ih - lastIY); 114 g.drawImage(image, xCounter, yCounter, dx2, dy2, 115 lastIX, lastIY, lastIX + dx2 - xCounter, 116 lastIY + dy2 - yCounter, null); 117 } 118 } 119 } else { 120 int st = sInsets.top; 121 int sl = sInsets.left; 122 int sb = sInsets.bottom; 123 int sr = sInsets.right; 124 125 int dt = dInsets.top; 126 int dl = dInsets.left; 127 int db = dInsets.bottom; 128 int dr = dInsets.right; 129 130 if (st + sb > ih) { 132 db = dt = sb = st = Math.max(0, ih / 2); 133 } 134 if (sl + sr > iw) { 135 dl = dr = sl = sr = Math.max(0, iw / 2); 136 } 137 138 if (dt + db > h) { 141 dt = db = Math.max(0, h / 2 - 1); 142 } 143 if (dl + dr > w) { 144 dl = dr = Math.max(0, w / 2 - 1); 145 } 146 147 boolean stretch = (paintType == PaintType.PAINT9_STRETCH); 148 if ((mask & PAINT_ALL) != 0) { 149 mask = (PAINT_ALL - 1) & ~mask; 150 } 151 152 if ((mask & PAINT_LEFT) != 0) { 153 drawChunk(image, g, stretch, x, y + dt, x + dl, y + h - db, 154 0, st, sl, ih - sb, false); 155 } 156 if ((mask & PAINT_TOP_LEFT) != 0) { 157 drawImage(image, g, x, y, x + dl, y + dt, 158 0, 0, sl, st); 159 } 160 if ((mask & PAINT_TOP) != 0) { 161 drawChunk(image, g, stretch, x + dl, y, x + w - dr, y + dt, 162 sl, 0, iw - sr, st, true); 163 } 164 if ((mask & PAINT_TOP_RIGHT) != 0) { 165 drawImage(image, g, x + w - dr, y, x + w, y + dt, 166 iw - sr, 0, iw, st); 167 } 168 if ((mask & PAINT_RIGHT) != 0) { 169 drawChunk(image, g, stretch, 170 x + w - dr, y + dt, x + w, y + h - db, 171 iw - sr, st, iw, ih - sb, false); 172 } 173 if ((mask & PAINT_BOTTOM_RIGHT) != 0) { 174 drawImage(image, g, x + w - dr, y + h - db, x + w, y + h, 175 iw - sr, ih - sb, iw, ih); 176 } 177 if ((mask & PAINT_BOTTOM) != 0) { 178 drawChunk(image, g, stretch, 179 x + dl, y + h - db, x + w - dr, y + h, 180 sl, ih - sb, iw - sr, ih, true); 181 } 182 if ((mask & PAINT_BOTTOM_LEFT) != 0) { 183 drawImage(image, g, x, y + h - db, x + dl, y + h, 184 0, ih - sb, sl, ih); 185 } 186 if ((mask & PAINT_CENTER) != 0) { 187 drawImage(image, g, x + dl, y + dt, x + w - dr, y + h - db, 188 sl, st, iw - sr, ih - sb); 189 } 190 } 191 } 192 193 211 private static void drawChunk(Image image, Graphics g, boolean stretch, 212 int dx1, int dy1, int dx2, int dy2, int sx1, 213 int sy1, int sx2, int sy2, 214 boolean xDirection) { 215 if (dx2 - dx1 <= 0 || dy2 - dy1 <= 0 || sx2 - sx1 <= 0 || 216 sy2 - sy1 <= 0) { 217 return; 219 } 220 if (stretch) { 221 g.drawImage(image, dx1, dy1, dx2, dy2, sx1, sy1, sx2, sy2, null); 222 } 223 else { 224 int xSize = sx2 - sx1; 225 int ySize = sy2 - sy1; 226 int deltaX; 227 int deltaY; 228 229 if (xDirection) { 230 deltaX = xSize; 231 deltaY = 0; 232 } 233 else { 234 deltaX = 0; 235 deltaY = ySize; 236 } 237 while (dx1 < dx2 && dy1 < dy2) { 238 int newDX2 = Math.min(dx2, dx1 + xSize); 239 int newDY2 = Math.min(dy2, dy1 + ySize); 240 241 g.drawImage(image, dx1, dy1, newDX2, newDY2, 242 sx1, sy1, sx1 + newDX2 - dx1, 243 sy1 + newDY2 - dy1, null); 244 dx1 += deltaX; 245 dy1 += deltaY; 246 } 247 } 248 } 249 250 private static void drawImage(Image image, Graphics g, 251 int dx1, int dy1, int dx2, int dy2, int sx1, 252 int sy1, int sx2, int sy2) { 253 if (dx2 - dx1 <= 0 || dy2 - dy1 <= 0 || sx2 - sx1 <= 0 || 255 sy2 - sy1 <= 0) { 256 return; 258 } 259 g.drawImage(image, dx1, dy1, dx2, dy2, sx1, sy1, sx2, sy2, null); 260 } 261 262 263 } 264 | Popular Tags |