| 1 | /* Copyright (C) 2000, 2003 Free Software Foundation
|
|---|
| 2 |
|
|---|
| 3 | This file is part of libgcj.
|
|---|
| 4 |
|
|---|
| 5 | This software is copyrighted work licensed under the terms of the
|
|---|
| 6 | Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
|
|---|
| 7 | details. */
|
|---|
| 8 |
|
|---|
| 9 | package gnu.awt.j2d;
|
|---|
| 10 |
|
|---|
| 11 | import java.awt.Color;
|
|---|
| 12 | import java.awt.Image;
|
|---|
| 13 | import java.awt.Shape;
|
|---|
| 14 | import java.awt.Rectangle;
|
|---|
| 15 | import java.awt.Graphics;
|
|---|
| 16 | import java.awt.Graphics2D;
|
|---|
| 17 | import java.awt.GraphicsConfiguration;
|
|---|
| 18 | import java.awt.Font;
|
|---|
| 19 | import java.awt.FontMetrics;
|
|---|
| 20 | import java.awt.image.BufferedImage;
|
|---|
| 21 | import java.awt.image.ImageObserver;
|
|---|
| 22 | import java.awt.image.Raster;
|
|---|
| 23 | import java.awt.image.WritableRaster;
|
|---|
| 24 | import java.awt.image.ColorModel;
|
|---|
| 25 |
|
|---|
| 26 | /**
|
|---|
| 27 | * IntegerGraphicsState is one of several graphics state
|
|---|
| 28 | * implementations. This graphics state is used when the graphics
|
|---|
| 29 | * object has simple properties, (coordinate translation only, no
|
|---|
| 30 | * transform) and the backend supports integer coordinates (pixel
|
|---|
| 31 | * based). For primitive paint operations, this object translates the
|
|---|
| 32 | * coordinates and forwards the request to the backend. For requests
|
|---|
| 33 | * to draw arbitrary shapes and paths, this object translates the
|
|---|
| 34 | * requests to primitive drawing operations supported by the
|
|---|
| 35 | * backend. IntegerGraphicsState is meant to support the most common
|
|---|
| 36 | * state of an graphics object. The degree of functionality is roughly
|
|---|
| 37 | * equivalent with the old java.awt.Graphics API.
|
|---|
| 38 | */
|
|---|
| 39 | public class IntegerGraphicsState extends AbstractGraphicsState
|
|---|
| 40 | {
|
|---|
| 41 | int tx;
|
|---|
| 42 | int ty;
|
|---|
| 43 |
|
|---|
| 44 | DirectRasterGraphics directGfx;
|
|---|
| 45 | Shape clip;
|
|---|
| 46 |
|
|---|
| 47 | public IntegerGraphicsState(DirectRasterGraphics directGfx)
|
|---|
| 48 | {
|
|---|
| 49 | this.directGfx = directGfx;
|
|---|
| 50 | }
|
|---|
| 51 |
|
|---|
| 52 | public Object clone()
|
|---|
| 53 | {
|
|---|
| 54 | IntegerGraphicsState clone = (IntegerGraphicsState) super.clone();
|
|---|
| 55 | clone.directGfx = (DirectRasterGraphics) directGfx.clone();
|
|---|
| 56 |
|
|---|
| 57 | return clone;
|
|---|
| 58 | }
|
|---|
| 59 |
|
|---|
| 60 | public void dispose()
|
|---|
| 61 | {
|
|---|
| 62 | DirectRasterGraphics lDeviceGfx = directGfx;
|
|---|
| 63 |
|
|---|
| 64 | directGfx = null;
|
|---|
| 65 |
|
|---|
| 66 | if (lDeviceGfx != null)
|
|---|
| 67 | lDeviceGfx.dispose();
|
|---|
| 68 |
|
|---|
| 69 | super.dispose();
|
|---|
| 70 | }
|
|---|
| 71 |
|
|---|
| 72 | // -------- Graphics methods:
|
|---|
| 73 |
|
|---|
| 74 | public void setColor(Color color)
|
|---|
| 75 | {
|
|---|
| 76 | directGfx.setColor(color);
|
|---|
| 77 | }
|
|---|
| 78 |
|
|---|
| 79 | public void setPaintMode()
|
|---|
| 80 | {
|
|---|
| 81 | directGfx.setPaintMode();
|
|---|
| 82 | }
|
|---|
| 83 |
|
|---|
| 84 | public void setXORMode(Color altColor)
|
|---|
| 85 | {
|
|---|
| 86 | directGfx.setXORMode(altColor);
|
|---|
| 87 | }
|
|---|
| 88 |
|
|---|
| 89 | public void setFont(Font font)
|
|---|
| 90 | {
|
|---|
| 91 | directGfx.setFont(font);
|
|---|
| 92 | }
|
|---|
| 93 |
|
|---|
| 94 | public FontMetrics getFontMetrics(Font font)
|
|---|
| 95 | {
|
|---|
| 96 | return directGfx.getFontMetrics(font);
|
|---|
| 97 | }
|
|---|
| 98 |
|
|---|
| 99 | public void setClip(Shape clip)
|
|---|
| 100 | {
|
|---|
| 101 | if (clip instanceof Rectangle)
|
|---|
| 102 | {
|
|---|
| 103 | Rectangle clipRect = (Rectangle) ((Rectangle) clip).clone();
|
|---|
| 104 | clipRect.x += tx;
|
|---|
| 105 | clipRect.y += ty;
|
|---|
| 106 |
|
|---|
| 107 | this.clip = clipRect;
|
|---|
| 108 |
|
|---|
| 109 | directGfx.setClip(clipRect);
|
|---|
| 110 | return;
|
|---|
| 111 | }
|
|---|
| 112 |
|
|---|
| 113 | String msg =
|
|---|
| 114 | "translating clip shape " + clip + " into device " +
|
|---|
| 115 | "coordinate space has not been implemented yet";
|
|---|
| 116 |
|
|---|
| 117 | throw new UnsupportedOperationException(msg);
|
|---|
| 118 | }
|
|---|
| 119 |
|
|---|
| 120 | public Shape getClip()
|
|---|
| 121 | {
|
|---|
| 122 | if (clip instanceof Rectangle)
|
|---|
| 123 | {
|
|---|
| 124 | Rectangle clipRect = (Rectangle) clip;
|
|---|
| 125 | clipRect.x -= tx;
|
|---|
| 126 | clipRect.y -= ty;
|
|---|
| 127 | return clipRect;
|
|---|
| 128 | }
|
|---|
| 129 |
|
|---|
| 130 | String msg =
|
|---|
| 131 | "translating clip shape " + clip + " into user " +
|
|---|
| 132 | "coordinate space has not been implemented yet";
|
|---|
| 133 |
|
|---|
| 134 | throw new UnsupportedOperationException(msg);
|
|---|
| 135 | }
|
|---|
| 136 |
|
|---|
| 137 | public Rectangle getClipBounds()
|
|---|
| 138 | {
|
|---|
| 139 | Rectangle clipRect = clip.getBounds();
|
|---|
| 140 |
|
|---|
| 141 | clipRect.x -= tx;
|
|---|
| 142 | clipRect.y -= ty;
|
|---|
| 143 | return clipRect;
|
|---|
| 144 | }
|
|---|
| 145 |
|
|---|
| 146 | public void copyArea(int x, int y,
|
|---|
| 147 | int width, int height,
|
|---|
| 148 | int dx, int dy)
|
|---|
| 149 | {
|
|---|
| 150 | directGfx.copyArea(x+tx, y+ty, width, height, dx, dy);
|
|---|
| 151 | }
|
|---|
| 152 |
|
|---|
| 153 | public void drawLine(int x1, int y1,
|
|---|
| 154 | int x2, int y2)
|
|---|
| 155 | {
|
|---|
| 156 | directGfx.drawLine(x1+tx, y1+ty, x2+tx, y2+ty);
|
|---|
| 157 | }
|
|---|
| 158 |
|
|---|
| 159 | public void fillRect(int x, int y,
|
|---|
| 160 | int width, int height)
|
|---|
| 161 | {
|
|---|
| 162 | directGfx.fillRect(x+tx, y+ty, width, height);
|
|---|
| 163 | }
|
|---|
| 164 |
|
|---|
| 165 | public void clearRect(int x, int y,
|
|---|
| 166 | int width, int height)
|
|---|
| 167 | {
|
|---|
| 168 | directGfx.setColor(frontend.getBackground());
|
|---|
| 169 | directGfx.fillRect(x+tx, y+ty, width, height);
|
|---|
| 170 | directGfx.setColor(frontend.getColor());
|
|---|
| 171 | }
|
|---|
| 172 |
|
|---|
| 173 | public void drawRoundRect(int x, int y,
|
|---|
| 174 | int width, int height,
|
|---|
| 175 | int arcWidth, int arcHeight)
|
|---|
| 176 | {
|
|---|
| 177 | throw new UnsupportedOperationException("not implemented yet");
|
|---|
| 178 | }
|
|---|
| 179 |
|
|---|
| 180 | public void fillRoundRect(int x, int y,
|
|---|
| 181 | int width, int height,
|
|---|
| 182 | int arcWidth, int arcHeight)
|
|---|
| 183 | {
|
|---|
| 184 | throw new UnsupportedOperationException("not implemented yet");
|
|---|
| 185 | }
|
|---|
| 186 |
|
|---|
| 187 | public void drawOval(int x, int y,
|
|---|
| 188 | int width, int height)
|
|---|
| 189 | {
|
|---|
| 190 | throw new UnsupportedOperationException("not implemented yet");
|
|---|
| 191 | }
|
|---|
| 192 |
|
|---|
| 193 | public void fillOval(int x, int y,
|
|---|
| 194 | int width, int height)
|
|---|
| 195 | {
|
|---|
| 196 | throw new UnsupportedOperationException("not implemented yet");
|
|---|
| 197 | }
|
|---|
| 198 |
|
|---|
| 199 | public void drawArc(int x, int y,
|
|---|
| 200 | int width, int height,
|
|---|
| 201 | int startAngle, int arcAngle)
|
|---|
| 202 | {
|
|---|
| 203 | directGfx.drawArc(x+tx, y+ty, width, height, startAngle, arcAngle);
|
|---|
| 204 | }
|
|---|
| 205 |
|
|---|
| 206 | public void fillArc(int x, int y,
|
|---|
| 207 | int width, int height,
|
|---|
| 208 | int startAngle, int arcAngle)
|
|---|
| 209 | {
|
|---|
| 210 | directGfx.fillArc(x+tx, y+ty, width, height, startAngle, arcAngle);
|
|---|
| 211 | }
|
|---|
| 212 |
|
|---|
| 213 | public void drawPolyline(int[] xPoints, int[] yPoints, int nPoints)
|
|---|
| 214 | {
|
|---|
| 215 | if ((tx == 0) && (ty == 0))
|
|---|
| 216 | {
|
|---|
| 217 | directGfx.drawPolyline(xPoints, yPoints, nPoints);
|
|---|
| 218 | return;
|
|---|
| 219 | }
|
|---|
| 220 |
|
|---|
| 221 | throw new UnsupportedOperationException("translate not implemented");
|
|---|
| 222 | }
|
|---|
| 223 |
|
|---|
| 224 | public void drawPolygon(int[] xPoints, int[] yPoints, int nPoints)
|
|---|
| 225 | {
|
|---|
| 226 | if ((tx == 0) && (ty == 0))
|
|---|
| 227 | {
|
|---|
| 228 | directGfx.drawPolygon(xPoints, yPoints, nPoints);
|
|---|
| 229 | return;
|
|---|
| 230 | }
|
|---|
| 231 |
|
|---|
| 232 | throw new UnsupportedOperationException("translate not implemented");
|
|---|
| 233 | }
|
|---|
| 234 |
|
|---|
| 235 | public void fillPolygon (int[] xPoints, int[] yPoints, int nPoints)
|
|---|
| 236 | {
|
|---|
| 237 | // FIXME: remove tx & ty args once translation via AffineTransform
|
|---|
| 238 | // is implemented.
|
|---|
| 239 | directGfx.fillPolygon (xPoints, yPoints, nPoints, tx, ty);
|
|---|
| 240 | }
|
|---|
| 241 |
|
|---|
| 242 | public boolean drawImage(Image image, int x, int y,
|
|---|
| 243 | ImageObserver observer)
|
|---|
| 244 | {
|
|---|
| 245 | x += tx;
|
|---|
| 246 | y += ty;
|
|---|
| 247 |
|
|---|
| 248 | if (image instanceof BufferedImage)
|
|---|
| 249 | {
|
|---|
| 250 | BufferedImage bImage = (BufferedImage) image;
|
|---|
| 251 | Object config =
|
|---|
| 252 | bImage.getProperty("java.awt.GraphicsConfiguration");
|
|---|
| 253 |
|
|---|
| 254 | if (config == frontend.config)
|
|---|
| 255 | return directGfx.drawImage(image, x, y, observer);
|
|---|
| 256 |
|
|---|
| 257 | int width = image.getWidth(null);
|
|---|
| 258 | int height = image.getHeight(null);
|
|---|
| 259 |
|
|---|
| 260 | Rectangle bounds = new Rectangle(x, y, width, height);
|
|---|
| 261 |
|
|---|
| 262 | MappedRaster mr = directGfx.mapRaster(bounds);
|
|---|
| 263 |
|
|---|
| 264 | // manipulate raster here...
|
|---|
| 265 | ColorModel colorModel = mr.getColorModel();
|
|---|
| 266 | WritableRaster raster = mr.getRaster();
|
|---|
| 267 |
|
|---|
| 268 | int xEnd = x + width;
|
|---|
| 269 | int yEnd = y + height;
|
|---|
| 270 |
|
|---|
| 271 | // FIXME: Use the following code only as a fallback. It's SLOW!
|
|---|
| 272 |
|
|---|
| 273 | Object rgbElem = null;
|
|---|
| 274 | for (int yy=0; yy<height; yy++)
|
|---|
| 275 | {
|
|---|
| 276 | for (int xx=0; xx<width; xx++)
|
|---|
| 277 | {
|
|---|
| 278 | int srgb = bImage.getRGB(xx, yy);
|
|---|
| 279 | int sa = ((srgb >>> 24) & 0xff) + 1;
|
|---|
| 280 | int sr = ((srgb >>> 16) & 0xff) + 1;
|
|---|
| 281 | int sg = ((srgb >>> 8) & 0xff) + 1;
|
|---|
| 282 | int sb = (srgb & 0xff) + 1;
|
|---|
| 283 |
|
|---|
| 284 | rgbElem = raster.getDataElements(xx+x, yy+y, rgbElem);
|
|---|
| 285 | int drgb = colorModel.getRGB(rgbElem);
|
|---|
| 286 | int dr = ((drgb >>> 16) & 0xff) + 1;
|
|---|
| 287 | int dg = ((drgb >>> 8) & 0xff) + 1;
|
|---|
| 288 | int db = (drgb & 0xff) + 1;
|
|---|
| 289 | int da = 256 - sa;
|
|---|
| 290 |
|
|---|
| 291 | dr = ((sr*sa + dr*da) >>> 8) - 1;
|
|---|
| 292 | dg = ((sg*sa + dg*da) >>> 8) - 1;
|
|---|
| 293 | db = ((sb*sa + db*da) >>> 8) - 1;
|
|---|
| 294 |
|
|---|
| 295 | drgb = (dr<<16) | (dg<<8) | db;
|
|---|
| 296 |
|
|---|
| 297 | rgbElem = colorModel.getDataElements(drgb, rgbElem);
|
|---|
| 298 |
|
|---|
| 299 | raster.setDataElements(xx+x, yy+y, rgbElem);
|
|---|
| 300 | }
|
|---|
| 301 | }
|
|---|
| 302 | directGfx.unmapRaster(mr);
|
|---|
| 303 | return true;
|
|---|
| 304 |
|
|---|
| 305 | }
|
|---|
| 306 | throw new UnsupportedOperationException("drawing image " + image +
|
|---|
| 307 | "not implemented");
|
|---|
| 308 | }
|
|---|
| 309 |
|
|---|
| 310 |
|
|---|
| 311 | // -------- Graphics2D methods:
|
|---|
| 312 |
|
|---|
| 313 | public void draw(Shape shape)
|
|---|
| 314 | {
|
|---|
| 315 | if (shape instanceof Rectangle)
|
|---|
| 316 | {
|
|---|
| 317 | Rectangle rect = (Rectangle) shape;
|
|---|
| 318 | directGfx.drawRect(rect.x+tx, rect.y+ty, rect.width, rect.height);
|
|---|
| 319 | return;
|
|---|
| 320 | }
|
|---|
| 321 |
|
|---|
| 322 | throw new UnsupportedOperationException("shape not implemented");
|
|---|
| 323 | }
|
|---|
| 324 |
|
|---|
| 325 | public void fill(Shape shape)
|
|---|
| 326 | {
|
|---|
| 327 | if (shape instanceof Rectangle)
|
|---|
| 328 | {
|
|---|
| 329 | Rectangle rect = (Rectangle) shape;
|
|---|
| 330 | directGfx.fillRect(rect.x+tx, rect.y+ty, rect.width, rect.height);
|
|---|
| 331 | return;
|
|---|
| 332 | }
|
|---|
| 333 |
|
|---|
| 334 | throw new UnsupportedOperationException("not implemented");
|
|---|
| 335 | }
|
|---|
| 336 |
|
|---|
| 337 | public boolean hit(Rectangle rect, Shape text,
|
|---|
| 338 | boolean onStroke)
|
|---|
| 339 | {
|
|---|
| 340 | throw new UnsupportedOperationException("not implemented");
|
|---|
| 341 | }
|
|---|
| 342 |
|
|---|
| 343 | public void drawString(String text, int x, int y)
|
|---|
| 344 | {
|
|---|
| 345 | directGfx.drawString(text, x+tx, y+ty);
|
|---|
| 346 | }
|
|---|
| 347 |
|
|---|
| 348 | public void drawString(String text, float x, float y)
|
|---|
| 349 | {
|
|---|
| 350 | drawString(text, (int) x, (int) y);
|
|---|
| 351 | }
|
|---|
| 352 |
|
|---|
| 353 | public void translate(int x, int y)
|
|---|
| 354 | {
|
|---|
| 355 | tx += x;
|
|---|
| 356 | ty += y;
|
|---|
| 357 | }
|
|---|
| 358 |
|
|---|
| 359 | public void translate(double tx, double ty)
|
|---|
| 360 | {
|
|---|
| 361 | if ((tx == 0) && (ty == 0))
|
|---|
| 362 | return;
|
|---|
| 363 |
|
|---|
| 364 | needAffineTransform();
|
|---|
| 365 | }
|
|---|
| 366 |
|
|---|
| 367 | public void rotate(double theta)
|
|---|
| 368 | {
|
|---|
| 369 | if (theta == 0)
|
|---|
| 370 | return;
|
|---|
| 371 |
|
|---|
| 372 | needAffineTransform();
|
|---|
| 373 | }
|
|---|
| 374 |
|
|---|
| 375 | public void rotate(double theta, double x, double y)
|
|---|
| 376 | {
|
|---|
| 377 | if (theta == 0)
|
|---|
| 378 | return;
|
|---|
| 379 |
|
|---|
| 380 | needAffineTransform();
|
|---|
| 381 | }
|
|---|
| 382 |
|
|---|
| 383 | public void scale(double scaleX, double scaleY)
|
|---|
| 384 | {
|
|---|
| 385 | if ((scaleX == 1) && (scaleY == 1))
|
|---|
| 386 | return;
|
|---|
| 387 |
|
|---|
| 388 | needAffineTransform();
|
|---|
| 389 | }
|
|---|
| 390 |
|
|---|
| 391 | public void shear(double shearX, double shearY)
|
|---|
| 392 | {
|
|---|
| 393 | if ((shearX == 0) && (shearY == 0))
|
|---|
| 394 | return;
|
|---|
| 395 |
|
|---|
| 396 | needAffineTransform();
|
|---|
| 397 | }
|
|---|
| 398 |
|
|---|
| 399 | private void needAffineTransform()
|
|---|
| 400 | {
|
|---|
| 401 | throw new UnsupportedOperationException("state with affine " +
|
|---|
| 402 | "transform not implemented");
|
|---|
| 403 | }
|
|---|
| 404 | }
|
|---|