IntegerGraphicsState.java 10 KB

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