XGraphics.java 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. /* Copyright (C) 2000, 2003, 2004 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.xlib;
  7. import java.awt.*;
  8. import java.awt.image.WritableRaster;
  9. import java.awt.image.Raster;
  10. import java.awt.image.DataBuffer;
  11. import java.awt.image.ColorModel;
  12. import java.awt.image.ImageObserver;
  13. import java.awt.image.BufferedImage;
  14. import gnu.gcj.xlib.GC;
  15. import gnu.gcj.xlib.XImage;
  16. import gnu.gcj.xlib.Drawable;
  17. import gnu.gcj.xlib.Window;
  18. import gnu.gcj.xlib.Drawable;
  19. import gnu.gcj.xlib.Pixmap;
  20. import gnu.gcj.xlib.Visual;
  21. import gnu.awt.j2d.DirectRasterGraphics;
  22. import gnu.awt.j2d.MappedRaster;
  23. public class XGraphics implements Cloneable, DirectRasterGraphics
  24. {
  25. static class XRaster extends MappedRaster
  26. {
  27. XImage ximage;
  28. public XRaster(WritableRaster raster, XImage ximage, ColorModel cm)
  29. {
  30. super(raster, cm);
  31. this.ximage = ximage;
  32. }
  33. }
  34. GC context;
  35. XGraphicsConfiguration config;
  36. Rectangle clipBounds;
  37. XFontMetrics metrics;
  38. public Object clone()
  39. {
  40. try
  41. {
  42. XGraphics gfxCopy = (XGraphics) super.clone();
  43. gfxCopy.context = context.create();
  44. return gfxCopy;
  45. }
  46. catch (CloneNotSupportedException ex)
  47. {
  48. // This should never happen.
  49. throw new InternalError ();
  50. }
  51. }
  52. public void dispose()
  53. {
  54. GC lContext = context;
  55. context = null;
  56. config = null;
  57. clipBounds = null;
  58. metrics = null;
  59. if (lContext != null)
  60. {
  61. lContext.dispose();
  62. }
  63. }
  64. public XGraphics(Drawable drawable, XGraphicsConfiguration config)
  65. {
  66. context = GC.create(drawable);
  67. this.config = config;
  68. }
  69. public void setColor(Color color)
  70. {
  71. if (color != null)
  72. context.setForeground(config.getPixel(color));
  73. }
  74. public void setPaintMode()
  75. {
  76. throw new UnsupportedOperationException("not implemented");
  77. }
  78. public void setXORMode(Color c1)
  79. {
  80. throw new UnsupportedOperationException("not implemented");
  81. }
  82. public void setFont(Font font)
  83. {
  84. if (font == null)
  85. return;
  86. if ((metrics != null) && font.equals(metrics.getFont()))
  87. return;
  88. metrics = config.getXFontMetrics(font);
  89. if (metrics != null)
  90. context.setFont(metrics.xfont);
  91. }
  92. public FontMetrics getFontMetrics(Font font)
  93. {
  94. if ((metrics != null) && font.equals(metrics.getFont()))
  95. return metrics;
  96. return config.getXFontMetrics(font);
  97. }
  98. public void setClip(int x, int y, int width, int height)
  99. {
  100. Rectangle[] rects = { new Rectangle(x, y, width, height) };
  101. context.setClipRectangles(rects);
  102. }
  103. public void setClip(Shape clip)
  104. {
  105. /* TODO: create a special RectangleUnion shape that can be
  106. used to draw advantage of the GCs ability to set multiple
  107. rectangles.
  108. */
  109. /* FIXME: creating all these objects is wasteful and can be
  110. costly in the long run, since this code is run at every
  111. expose. */
  112. Rectangle newClipBounds = clip.getBounds();
  113. /* FIXME: decide whether this test code is worth anything
  114. * (as of 2004-01-29, it prints frequently)
  115. if ((clipBounds != null) && !clipBounds.contains(newClipBounds))
  116. {
  117. System.err.println("warning: old clip ("+ clipBounds +") does " +
  118. "not fully contain new clip (" +
  119. newClipBounds + ")");
  120. }
  121. */
  122. clipBounds = newClipBounds;
  123. Rectangle[] rects = { clipBounds };
  124. context.setClipRectangles(rects);
  125. }
  126. public void copyArea(int x, int y, int width, int height, int
  127. dx, int dy)
  128. {
  129. throw new UnsupportedOperationException("not implemented");
  130. }
  131. public void drawLine(int x1, int y1, int x2, int y2)
  132. {
  133. context.drawLine(x1, y1, x2, y2);
  134. }
  135. public void drawRect(int x, int y, int width, int height)
  136. {
  137. throw new UnsupportedOperationException("not implemented yet");
  138. }
  139. public void fillRect(int x, int y, int width, int height)
  140. {
  141. context.fillRectangle(x, y, width, height);
  142. }
  143. public void drawArc(int x, int y, int width, int height, int
  144. startAngle, int arcAngle)
  145. {
  146. context.drawArc (x, y, width, height, startAngle, arcAngle);
  147. }
  148. public void fillArc(int x, int y, int width, int height, int
  149. startAngle, int arcAngle)
  150. {
  151. context.fillArc (x, y, width, height, startAngle, arcAngle);
  152. }
  153. public void drawPolyline(int[] xPoints, int[] yPoints, int
  154. nPoints)
  155. {
  156. throw new UnsupportedOperationException("not implemented");
  157. }
  158. public void drawPolygon(int[] xPoints, int[] yPoints, int
  159. nPoints)
  160. {
  161. throw new UnsupportedOperationException("not implemented");
  162. }
  163. public void fillPolygon(int[] xPoints, int[] yPoints, int nPoints,
  164. int translateX, int translateY)
  165. {
  166. context.fillPolygon(xPoints, yPoints, nPoints, translateX, translateY);
  167. }
  168. public void drawString(String str, int x, int y)
  169. {
  170. context.drawString(str, x, y);
  171. }
  172. public boolean drawImage(Image img, int x, int y,
  173. ImageObserver observer)
  174. {
  175. if (img instanceof XOffScreenImage)
  176. {
  177. // FIXME: have to enforce clip, or is it OK as-is?
  178. XOffScreenImage offScreenImage = (XOffScreenImage) img;
  179. Pixmap pixmap = offScreenImage.getPixmap ();
  180. context.copyArea (pixmap, 0, 0, x, y,
  181. offScreenImage.getWidth (), offScreenImage.getHeight ());
  182. return true;
  183. }
  184. if (clipBounds == null)
  185. return false; // ***FIXME***
  186. if (!(img instanceof BufferedImage))
  187. {
  188. throw new AWTError("unknown image class");
  189. }
  190. BufferedImage bimg = (BufferedImage) img;
  191. XImage ximg = (XImage) bimg.getProperty("gnu.gcj.xlib.XImage");
  192. if (ximg == null)
  193. {
  194. System.err.println("FIXME: skipping null XImage, should " +
  195. "really do on the spot conversion");
  196. return false;
  197. }
  198. /*
  199. +------------------
  200. | clip
  201. | +---------+
  202. | img | |
  203. | +--+-------+ |
  204. | | | | |
  205. | | | | |
  206. | | +-------+-+
  207. | | |
  208. | +----------+
  209. */
  210. int iLeft = Math.max(x, clipBounds.x);
  211. int iTop = Math.max(y, clipBounds.y);
  212. int iRight = Math.min(x + bimg.getWidth(),
  213. clipBounds.x + clipBounds.width);
  214. int iBottom = Math.min(y + bimg.getHeight(),
  215. clipBounds.y + clipBounds.height);
  216. int srcX = iLeft - x;
  217. int srcY = iTop - y;
  218. int width = iRight - iLeft;
  219. int height = iBottom - iTop;
  220. if ((width > 0) && (height > 0))
  221. context.putImage(ximg, srcX, srcY, iLeft, iTop, width, height);
  222. return true;
  223. }
  224. public MappedRaster mapRaster(Rectangle bounds)
  225. {
  226. Visual visual = config.getVisual();
  227. XImage ximage = new XImage(visual, bounds.width, bounds.height,
  228. false // do not auto allocate memory
  229. );
  230. WritableRaster raster =
  231. config.createRasterForXImage(ximage,
  232. new Point(bounds.x, bounds.y));
  233. DataBuffer dataB = raster.getDataBuffer();
  234. XGraphicsConfiguration.attachData(ximage, dataB, 0);
  235. Drawable drawable = context.getDrawable();
  236. // TODO: restrict to clipping
  237. Rectangle mBounds = drawable.copyIntoXImage(ximage, bounds, 0, 0);
  238. return new XRaster(raster, ximage, config.imageCM);
  239. }
  240. public void unmapRaster(MappedRaster mappedRaster)
  241. {
  242. XRaster xraster = (XRaster) mappedRaster;
  243. XImage ximage = xraster.ximage;
  244. Raster raster = xraster.getRaster();
  245. int x = raster.getMinX();
  246. int y = raster.getMinY();
  247. int width = raster.getWidth();
  248. int height = raster.getHeight();
  249. context.putImage(ximage, 0, 0, x, y, width, height);
  250. }
  251. }