Graphics2DImpl.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546
  1. /* Copyright (C) 2000, 2002, 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.Composite;
  9. import java.awt.Image;
  10. import java.awt.Shape;
  11. import java.awt.Rectangle;
  12. import java.awt.Graphics;
  13. import java.awt.Graphics2D;
  14. import java.awt.GraphicsConfiguration;
  15. import java.awt.Font;
  16. import java.awt.FontMetrics;
  17. import java.awt.Paint;
  18. import java.awt.RenderingHints;
  19. import java.awt.Stroke;
  20. import java.awt.font.FontRenderContext;
  21. import java.awt.font.GlyphVector;
  22. import java.awt.geom.AffineTransform;
  23. import java.awt.image.ImageObserver;
  24. import java.awt.image.BufferedImage;
  25. import java.awt.image.BufferedImageOp;
  26. import java.awt.image.RenderedImage;
  27. import java.awt.image.renderable.RenderableImage;
  28. import java.text.AttributedCharacterIterator;
  29. import java.util.Map;
  30. /**
  31. * Delegates almost all work to a state object, that allows us to
  32. * hot-swap rendering strategies based on state changes inflicted on
  33. * this Graphics object. This class keeps track of properties that are
  34. * not affected by the state, (such as clip shape,
  35. * foreground/background color, font, etc.).
  36. *
  37. * <p>The far front-end of the rendering pipeline consists of the
  38. * Graphics2D API. In the far back-end, lies the native graphics
  39. * libraries. In most cases the native graphics libraries only have
  40. * direct support for a subset of the properties of Graphics2D. To
  41. * make up missing features in the native graphics libraries, the
  42. * pipeline between the front-end and the back-end need to translate
  43. * drawing request to primitive operations that are supported by the
  44. * back-end. E.g. for X11, drawing a straight line will translate to
  45. * an XDrawLine, drawing a bezier curve will trigger flattening of the
  46. * curve and will result in a call to XDrawLines.
  47. *
  48. * <p>This is the basic strategy for the rendering pipeline: Whenever
  49. * a graphics property change occurs, that causes the current pipeline
  50. * to be insufficient, amend or replace parts of the pipeline so that
  51. * the pipeline will once again be able to translate requests to the
  52. * set of primitives supported by the native graphics library.
  53. *
  54. * <p>Most graphics libraries share common subsets of
  55. * functionality. To be able to reuse pieces of the rendering pipeline
  56. * for several backends, we define interfaces that describe subsets of
  57. * characteristics supported by the backends. A wrapper for the native
  58. * library can implement several interfaces to describe its range of
  59. * functionality.
  60. *
  61. * <p>Typically, most painting is done with a graphics object with
  62. * simple properties. Unless one is using a complex Look & Feel, the
  63. * painting of Swing components will never require affine transforms,
  64. * alpha blending, non-rectangular clipping, etc. When graphics
  65. * objects are created, they start off in a state where all the
  66. * properties are simple. Most graphics objects experience only
  67. * trivial property changes, and never leave this simple state. It is
  68. * therefore wise to ensure that the rendering pipeline for this
  69. * initial state is lean and as much as possible plugs directly into
  70. * the backend.
  71. *
  72. * <p>The initial state for graphics object of most raster displays
  73. * would call for two levels of indirection:
  74. *
  75. * <pre>
  76. * Graphics2D object ---> IntegerGraphicsState ---> DirectRasterGraphics
  77. * </pre>
  78. */
  79. public class Graphics2DImpl extends Graphics2D implements Cloneable
  80. {
  81. GraphicsConfiguration config;
  82. AbstractGraphicsState state;
  83. Color fg;
  84. Color bg;
  85. Font font;
  86. public Graphics2DImpl(GraphicsConfiguration config)
  87. {
  88. this.config = config;
  89. }
  90. public void setState(AbstractGraphicsState state)
  91. {
  92. this.state = state;
  93. this.state.setFrontend(this);
  94. }
  95. public Object clone()
  96. {
  97. try
  98. {
  99. Graphics2DImpl gfxCopy = (Graphics2DImpl) super.clone();
  100. AbstractGraphicsState stateCopy =
  101. (AbstractGraphicsState) state.clone();
  102. gfxCopy.setState(stateCopy);
  103. return gfxCopy;
  104. }
  105. catch (CloneNotSupportedException ex)
  106. {
  107. // This should never happen.
  108. throw new InternalError ();
  109. }
  110. }
  111. // -------- Graphics methods:
  112. public Graphics create()
  113. {
  114. Graphics2DImpl gfxCopy = (Graphics2DImpl) clone();
  115. return gfxCopy;
  116. }
  117. public Color getColor()
  118. {
  119. return fg;
  120. }
  121. public void setColor(Color color)
  122. {
  123. fg = color;
  124. state.setColor(color);
  125. }
  126. public void setPaintMode()
  127. {
  128. state.setPaintMode();
  129. }
  130. public void setXORMode(Color altColor)
  131. {
  132. state.setXORMode(altColor);
  133. }
  134. public Font getFont()
  135. {
  136. return font;
  137. }
  138. public void setFont(Font font)
  139. {
  140. this.font = font;
  141. state.setFont(font);
  142. }
  143. public FontMetrics getFontMetrics(Font font)
  144. {
  145. return state.getFontMetrics(font);
  146. }
  147. public Rectangle getClipBounds()
  148. {
  149. return state.getClipBounds();
  150. }
  151. public void clipRect(int x, int y, int width, int height)
  152. {
  153. Shape clip = state.getClip();
  154. if (clip == null)
  155. {
  156. clip = new Rectangle (x,y,width,height);
  157. setClip (clip);
  158. return;
  159. }
  160. if (clip instanceof Rectangle)
  161. {
  162. Rectangle clipRect = (Rectangle) clip;
  163. clip = clipRect.intersection(new Rectangle(x, y, width, height));
  164. setClip(clip);
  165. return;
  166. }
  167. String msg =
  168. "intersecting current clip shape " + clip + " with new rectangle " +
  169. "has not been implemented yet";
  170. throw new UnsupportedOperationException(msg);
  171. }
  172. public void setClip(int x, int y, int width, int height)
  173. {
  174. Rectangle clip = new Rectangle(x, y, width, height);
  175. setClip(clip);
  176. }
  177. public Shape getClip()
  178. {
  179. return state.getClip();
  180. }
  181. public void setClip(Shape clip)
  182. {
  183. state.setClip(clip);
  184. }
  185. public void copyArea(int x, int y, int width, int height,
  186. int dx, int dy)
  187. {
  188. state.copyArea(x, y, width, height, dx, dy);
  189. }
  190. public void drawLine(int x1, int y1, int x2, int y2)
  191. {
  192. state.drawLine(x1, y1, x2, y2);
  193. }
  194. public void fillRect(int x, int y, int width, int height)
  195. {
  196. state.fillRect(x, y, width, height);
  197. }
  198. public void clearRect(int x, int y, int width, int height)
  199. {
  200. state.clearRect(x, y, width, height);
  201. }
  202. public void drawRoundRect(int x, int y, int width, int height,
  203. int arcWidth, int arcHeight)
  204. {
  205. state.drawRoundRect(x, y, width, height, arcWidth, arcHeight);
  206. }
  207. public void fillRoundRect(int x, int y, int width, int height,
  208. int arcWidth, int arcHeight)
  209. {
  210. state.fillRoundRect(x, y, width, height, arcWidth, arcHeight);
  211. }
  212. public void drawOval(int x, int y, int width, int height)
  213. {
  214. state.drawOval(x, y, width, height);
  215. }
  216. public void fillOval(int x, int y, int width, int height)
  217. {
  218. state.fillOval(x, y, width, height);
  219. }
  220. public void drawArc(int x, int y, int width, int height,
  221. int startAngle, int arcAngle)
  222. {
  223. state.drawArc(x, y, width, height, startAngle, arcAngle);
  224. }
  225. public void fillArc(int x, int y, int width, int height,
  226. int startAngle, int arcAngle)
  227. {
  228. state.fillArc(x, y, width, height, startAngle, arcAngle);
  229. }
  230. public void drawPolyline(int[] xPoints, int[] yPoints, int nPoints)
  231. {
  232. state.drawPolyline(xPoints, yPoints, nPoints);
  233. }
  234. public void drawPolygon(int[] xPoints, int[] yPoints, int nPoints)
  235. {
  236. state.drawPolygon(xPoints, yPoints, nPoints);
  237. }
  238. public void fillPolygon(int[] xPoints, int[] yPoints, int nPoints)
  239. {
  240. state.fillPolygon(xPoints, yPoints, nPoints);
  241. }
  242. public boolean drawImage(Image image, int x, int y,
  243. ImageObserver observer)
  244. {
  245. return state.drawImage(image, x, y, observer);
  246. }
  247. public boolean drawImage(Image img, int x, int y,
  248. int width, int height,
  249. ImageObserver observer)
  250. {
  251. throw new UnsupportedOperationException("not implemented yet");
  252. }
  253. public boolean drawImage(Image img, int x, int y, Color bgcolor,
  254. ImageObserver observer)
  255. {
  256. throw new UnsupportedOperationException("not implemented yet");
  257. }
  258. public boolean drawImage(Image img, int x, int y,
  259. int width, int height, Color bgcolor,
  260. ImageObserver observer)
  261. {
  262. throw new UnsupportedOperationException("not implemented yet");
  263. }
  264. public boolean drawImage(Image img,
  265. int dx1, int dy1, int dx2, int dy2,
  266. int sx1, int sy1, int sx2, int sy2,
  267. ImageObserver observer)
  268. {
  269. throw new UnsupportedOperationException("not implemented yet");
  270. }
  271. public boolean drawImage(Image img,
  272. int dx1, int dy1, int dx2, int dy2,
  273. int sx1, int sy1, int sx2, int sy2,
  274. Color bgcolor, ImageObserver observer)
  275. {
  276. throw new UnsupportedOperationException("not implemented yet");
  277. }
  278. public void dispose()
  279. {
  280. AbstractGraphicsState lState = state;
  281. state = null;
  282. config = null;
  283. font = null;
  284. fg = null;
  285. bg = null;
  286. if (lState != null)
  287. lState.dispose();
  288. }
  289. // -------- Graphics2D methods:
  290. public void draw(Shape shape)
  291. {
  292. state.draw(shape);
  293. }
  294. public boolean drawImage(Image image, AffineTransform xform,
  295. ImageObserver obs)
  296. {
  297. throw new UnsupportedOperationException("not implemented yet");
  298. }
  299. public void drawString(String text, int x, int y)
  300. {
  301. state.drawString(text, x, y);
  302. }
  303. public void drawString(String text, float x, float y)
  304. {
  305. state.drawString(text, x, y);
  306. }
  307. public void fill(Shape shape)
  308. {
  309. state.fill(shape);
  310. }
  311. public boolean hit(Rectangle rect, Shape text, boolean onStroke)
  312. {
  313. return state.hit(rect, text, onStroke);
  314. }
  315. public GraphicsConfiguration getDeviceConfiguration()
  316. {
  317. return config;
  318. }
  319. public void setPaint(Paint paint)
  320. {
  321. throw new UnsupportedOperationException("not implemented yet");
  322. }
  323. public void setRenderingHint(RenderingHints.Key hintKey,
  324. Object hintValue)
  325. {
  326. throw new UnsupportedOperationException("not implemented yet");
  327. }
  328. public Object getRenderingHint(RenderingHints.Key hintKey)
  329. {
  330. throw new UnsupportedOperationException("not implemented yet");
  331. }
  332. public RenderingHints getRenderingHints()
  333. {
  334. throw new UnsupportedOperationException("not implemented yet");
  335. }
  336. public void translate(int x, int y)
  337. {
  338. state.translate(x, y);
  339. }
  340. public void translate(double tx, double ty)
  341. {
  342. state.translate(tx, ty);
  343. }
  344. public void rotate(double theta)
  345. {
  346. state.rotate(theta);
  347. }
  348. public void rotate(double theta, double x, double y)
  349. {
  350. state.rotate(theta, x, y);
  351. }
  352. public void scale(double scaleX, double scaleY)
  353. {
  354. state.scale(scaleX, scaleY);
  355. }
  356. public void shear(double shearX, double shearY)
  357. {
  358. state.shear(shearX, shearY);
  359. }
  360. public void transform(AffineTransform Tx)
  361. {
  362. throw new UnsupportedOperationException("not implemented yet");
  363. }
  364. public void setTransform(AffineTransform Tx)
  365. {
  366. throw new UnsupportedOperationException("not implemented yet");
  367. }
  368. public AffineTransform getTransform()
  369. {
  370. throw new UnsupportedOperationException("not implemented yet");
  371. }
  372. public Paint getPaint()
  373. {
  374. throw new UnsupportedOperationException("not implemented yet");
  375. }
  376. public void setBackground(Color color)
  377. {
  378. bg = color;
  379. }
  380. public Color getBackground()
  381. {
  382. return bg;
  383. }
  384. public void clip(Shape shape)
  385. {
  386. Shape clip = state.getClip();
  387. if ((shape instanceof Rectangle) && (clip instanceof Rectangle))
  388. {
  389. clip = ((Rectangle) clip).intersection((Rectangle) shape);
  390. state.setClip(clip);
  391. return;
  392. }
  393. String msg =
  394. "intersecting current clip shape " + clip + " with new shape " + shape +
  395. "has not been implemented yet";
  396. throw new UnsupportedOperationException(msg);
  397. }
  398. public void drawImage(BufferedImage image, BufferedImageOp op, int x, int y)
  399. {
  400. throw new UnsupportedOperationException("not implemented yet");
  401. }
  402. public void drawRenderedImage(RenderedImage image, AffineTransform xform)
  403. {
  404. throw new UnsupportedOperationException("not implemented yet");
  405. }
  406. public void drawRenderableImage(RenderableImage image, AffineTransform xform)
  407. {
  408. throw new UnsupportedOperationException("not implemented yet");
  409. }
  410. public void drawString(AttributedCharacterIterator iterator,
  411. int x, int y)
  412. {
  413. throw new UnsupportedOperationException("not implemented yet");
  414. }
  415. public void drawString(AttributedCharacterIterator iterator, float x,
  416. float y)
  417. {
  418. throw new UnsupportedOperationException("not implemented yet");
  419. }
  420. public void setComposite(Composite comp)
  421. {
  422. throw new UnsupportedOperationException("not implemented yet");
  423. }
  424. public void setStroke(Stroke stroke)
  425. {
  426. throw new UnsupportedOperationException("not implemented yet");
  427. }
  428. public void setRenderingHints(Map hints)
  429. {
  430. throw new UnsupportedOperationException("not implemented yet");
  431. }
  432. public void addRenderingHints(Map hints)
  433. {
  434. throw new UnsupportedOperationException("not implemented yet");
  435. }
  436. public Composite getComposite()
  437. {
  438. throw new UnsupportedOperationException("not implemented yet");
  439. }
  440. public Stroke getStroke()
  441. {
  442. throw new UnsupportedOperationException("not implemented yet");
  443. }
  444. public FontRenderContext getFontRenderContext ()
  445. {
  446. throw new UnsupportedOperationException("not implemented yet");
  447. }
  448. public void drawGlyphVector (GlyphVector g, float x, float y)
  449. {
  450. throw new UnsupportedOperationException("not implemented yet");
  451. }
  452. }