1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989
|
/* AbstractGraphics2D.java -- Abstract Graphics2D implementation
Copyright (C) 2006 Free Software Foundation, Inc.
This file is part of GNU Classpath.
GNU Classpath is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
GNU Classpath is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with GNU Classpath; see the file COPYING. If not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA.
Linking this library statically or dynamically with other modules is
making a combined work based on this library. Thus, the terms and
conditions of the GNU General Public License cover the whole
combination.
As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent
modules, and to copy and distribute the resulting executable under
terms of your choice, provided that you also meet, for each linked
independent module, the terms and conditions of the license of that
module. An independent module is a module which is not derived from
or based on this library. If you modify this library, you may extend
this exception to your version of the library, but you are not
obligated to do so. If you do not wish to do so, delete this
exception statement from your version. */
package gnu.java.awt.java2d;
import gnu.java.util.LRUCache;
import java.awt.AWTError;
import java.awt.AlphaComposite;
import java.awt.AWTPermission;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Composite;
import java.awt.CompositeContext;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Paint;
import java.awt.PaintContext;
import java.awt.Point;
import java.awt.Polygon;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.Shape;
import java.awt.Stroke;
import java.awt.Toolkit;
import java.awt.RenderingHints.Key;
import java.awt.font.FontRenderContext;
import java.awt.font.GlyphVector;
import java.awt.geom.AffineTransform;
import java.awt.geom.Arc2D;
import java.awt.geom.Area;
import java.awt.geom.Ellipse2D;
import java.awt.geom.GeneralPath;
import java.awt.geom.Line2D;
import java.awt.geom.NoninvertibleTransformException;
import java.awt.geom.RoundRectangle2D;
import java.awt.image.BufferedImage;
import java.awt.image.BufferedImageOp;
import java.awt.image.ColorModel;
import java.awt.image.DataBuffer;
import java.awt.image.ImageObserver;
import java.awt.image.Raster;
import java.awt.image.RenderedImage;
import java.awt.image.SampleModel;
import java.awt.image.WritableRaster;
import java.awt.image.renderable.RenderableImage;
import java.text.AttributedCharacterIterator;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
/**
* This is a 100% Java implementation of the Java2D rendering pipeline. It is
* meant as a base class for Graphics2D implementations.
*
* <h2>Backend interface</h2>
* <p>
* The backend must at the very least provide a Raster which the the rendering
* pipeline can paint into. This must be implemented in
* {@link #getDestinationRaster()}. For some backends that might be enough, like
* when the target surface can be directly access via the raster (like in
* BufferedImages). Other targets need some way to synchronize the raster with
* the surface, which can be achieved by implementing the
* {@link #updateRaster(Raster, int, int, int, int)} method, which always gets
* called after a chunk of data got painted into the raster.
* </p>
* <p>Alternativly the backend can provide a method for filling Shapes by
* overriding the protected method fillShape(). This can be accomplished
* by a polygon filling function of the backend. Keep in mind though that
* Shapes can be quite complex (i.e. non-convex and containing holes, etc)
* which is not supported by all polygon fillers. Also it must be noted
* that fillShape() is expected to handle painting and compositing as well as
* clipping and transformation. If your backend can't support this natively,
* then you can fallback to the implementation in this class. You'll need
* to provide a writable Raster then, see above.</p>
* <p>Another alternative is to implement fillScanline() which only requires
* the backend to be able to draw horizontal lines in device space,
* which is usually very cheap.
* The implementation should still handle painting and compositing,
* but no more clipping and transformation is required by the backend.</p>
* <p>The backend is free to provide implementations for the various raw*
* methods for optimized AWT 1.1 style painting of some primitives. This should
* accelerate painting of Swing greatly. When doing so, the backend must also
* keep track of the clip and translation, probably by overriding
* some clip and translate methods. Don't forget to message super in such a
* case.</p>
*
* <h2>Acceleration options</h2>
* <p>
* The fact that it is
* pure Java makes it a little slow. However, there are several ways of
* accelerating the rendering pipeline:
* <ol>
* <li><em>Optimization hooks for AWT 1.1 - like graphics operations.</em>
* The most important methods from the {@link java.awt.Graphics} class
* have a corresponding <code>raw*</code> method, which get called when
* several optimization conditions are fullfilled. These conditions are
* described below. Subclasses can override these methods and delegate
* it directly to a native backend.</li>
* <li><em>Native PaintContexts and CompositeContext.</em> The implementations
* for the 3 PaintContexts and AlphaCompositeContext can be accelerated
* using native code. These have proved to two of the most performance
* critical points in the rendering pipeline and cannot really be done quickly
* in plain Java because they involve lots of shuffling around with large
* arrays. In fact, you really would want to let the graphics card to the
* work, they are made for this.</li>
* <li>Provide an accelerated implementation for fillShape(). For instance,
* OpenGL can fill shapes very efficiently. There are some considerations
* to be made though, see above for details.</li>
* </ol>
* </p>
*
* @author Roman Kennke (kennke@aicas.com)
*/
public abstract class AbstractGraphics2D
extends Graphics2D
implements Cloneable, Pixelizer
{
/**
* Wether we use anti aliasing for rendering text by default or not.
*/
private static final boolean DEFAULT_TEXT_AA =
Boolean.getBoolean("gnu.java2d.default_text_aa");
/**
* The default font to use on the graphics object.
*/
private static final Font FONT = new Font("SansSerif", Font.PLAIN, 12);
/**
* The size of the LRU cache used for caching GlyphVectors.
*/
private static final int GV_CACHE_SIZE = 50;
/**
* Caches certain shapes to avoid massive creation of such Shapes in
* the various draw* and fill* methods.
*/
private static final ShapeCache shapeCache = new ShapeCache();
/**
* A pool of scanline converters. It is important to reuse scanline
* converters because they keep their datastructures in place. We pool them
* for use in multiple threads.
*/
private static final LinkedList<ScanlineConverter> scanlineConverters =
new LinkedList<ScanlineConverter>();
/**
* Caches glyph vectors for better drawing performance.
*/
private static final Map<TextCacheKey,GlyphVector> gvCache =
Collections.synchronizedMap(new LRUCache<TextCacheKey,GlyphVector>(GV_CACHE_SIZE));
/**
* This key is used to search in the gvCache without allocating a new
* key each time.
*/
private static final TextCacheKey searchTextKey = new TextCacheKey();
/**
* The transformation for this Graphics2D instance
*/
protected AffineTransform transform;
/**
* The foreground.
*/
private Paint paint;
/**
* The paint context during rendering.
*/
private PaintContext paintContext;
/**
* The background.
*/
private Color background;
/**
* The current font.
*/
private Font font;
/**
* The current composite setting.
*/
private Composite composite;
/**
* The current stroke setting.
*/
private Stroke stroke;
/**
* The current clip. This clip is in user coordinate space.
*/
private Shape clip;
/**
* The rendering hints.
*/
private RenderingHints renderingHints;
/**
* The raster of the destination surface. This is where the painting is
* performed.
*/
private WritableRaster destinationRaster;
/**
* Indicates if certain graphics primitives can be rendered in an optimized
* fashion. This will be the case if the following conditions are met:
* - The transform may only be a translation, no rotation, shearing or
* scaling.
* - The paint must be a solid color.
* - The composite must be an AlphaComposite.SrcOver.
* - The clip must be a Rectangle.
* - The stroke must be a plain BasicStroke().
*
* These conditions represent the standard settings of a new
* AbstractGraphics2D object and will be the most commonly used setting
* in Swing rendering and should therefore be optimized as much as possible.
*/
private boolean isOptimized = true;
private static final BasicStroke STANDARD_STROKE = new BasicStroke();
private static final HashMap STANDARD_HINTS;
static {
HashMap hints = new HashMap();
hints.put(RenderingHints.KEY_TEXT_ANTIALIASING,
RenderingHints.VALUE_TEXT_ANTIALIAS_DEFAULT);
hints.put(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_DEFAULT);
STANDARD_HINTS = hints;
}
/**
* Creates a new AbstractGraphics2D instance.
*/
protected AbstractGraphics2D()
{
transform = new AffineTransform();
background = Color.WHITE;
composite = AlphaComposite.SrcOver;
stroke = STANDARD_STROKE;
renderingHints = new RenderingHints(STANDARD_HINTS);
}
/**
* Draws the specified shape. The shape is passed through the current stroke
* and is then forwarded to {@link #fillShape}.
*
* @param shape the shape to draw
*/
public void draw(Shape shape)
{
// Stroke the shape.
Shape strokedShape = stroke.createStrokedShape(shape);
// Fill the stroked shape.
fillShape(strokedShape, false);
}
/**
* Draws the specified image and apply the transform for image space ->
* user space conversion.
*
* This method is implemented to special case RenderableImages and
* RenderedImages and delegate to
* {@link #drawRenderableImage(RenderableImage, AffineTransform)} and
* {@link #drawRenderedImage(RenderedImage, AffineTransform)} accordingly.
* Other image types are not yet handled.
*
* @param image the image to be rendered
* @param xform the transform from image space to user space
* @param obs the image observer to be notified
*/
public boolean drawImage(Image image, AffineTransform xform,
ImageObserver obs)
{
Rectangle areaOfInterest = new Rectangle(0, 0, image.getWidth(obs),
image.getHeight(obs));
return drawImageImpl(image, xform, obs, areaOfInterest);
}
/**
* Draws the specified image and apply the transform for image space ->
* user space conversion. This method only draw the part of the image
* specified by <code>areaOfInterest</code>.
*
* This method is implemented to special case RenderableImages and
* RenderedImages and delegate to
* {@link #drawRenderableImage(RenderableImage, AffineTransform)} and
* {@link #drawRenderedImage(RenderedImage, AffineTransform)} accordingly.
* Other image types are not yet handled.
*
* @param image the image to be rendered
* @param xform the transform from image space to user space
* @param obs the image observer to be notified
* @param areaOfInterest the area in image space that is rendered
*/
private boolean drawImageImpl(Image image, AffineTransform xform,
ImageObserver obs, Rectangle areaOfInterest)
{
boolean ret;
if (image == null)
{
ret = true;
}
else if (image instanceof RenderedImage)
{
// FIXME: Handle the ImageObserver.
drawRenderedImageImpl((RenderedImage) image, xform, areaOfInterest);
ret = true;
}
else if (image instanceof RenderableImage)
{
// FIXME: Handle the ImageObserver.
drawRenderableImageImpl((RenderableImage) image, xform, areaOfInterest);
ret = true;
}
else
{
// FIXME: Implement rendering of other Image types.
ret = false;
}
return ret;
}
/**
* Renders a BufferedImage and applies the specified BufferedImageOp before
* to filter the BufferedImage somehow. The resulting BufferedImage is then
* passed on to {@link #drawRenderedImage(RenderedImage, AffineTransform)}
* to perform the final rendering.
*
* @param image the source buffered image
* @param op the filter to apply to the buffered image before rendering
* @param x the x coordinate to render the image to
* @param y the y coordinate to render the image to
*/
public void drawImage(BufferedImage image, BufferedImageOp op, int x, int y)
{
BufferedImage filtered =
op.createCompatibleDestImage(image, image.getColorModel());
AffineTransform t = new AffineTransform();
t.translate(x, y);
drawRenderedImage(filtered, t);
}
/**
* Renders the specified image to the destination raster. The specified
* transform is used to convert the image into user space. The transform
* of this AbstractGraphics2D object is used to transform from user space
* to device space.
*
* The rendering is performed using the scanline algorithm that performs the
* rendering of other shapes and a custom Paint implementation, that supplies
* the pixel values of the rendered image.
*
* @param image the image to render to the destination raster
* @param xform the transform from image space to user space
*/
public void drawRenderedImage(RenderedImage image, AffineTransform xform)
{
Rectangle areaOfInterest = new Rectangle(image.getMinX(),
image.getHeight(),
image.getWidth(),
image.getHeight());
drawRenderedImageImpl(image, xform, areaOfInterest);
}
/**
* Renders the specified image to the destination raster. The specified
* transform is used to convert the image into user space. The transform
* of this AbstractGraphics2D object is used to transform from user space
* to device space. Only the area specified by <code>areaOfInterest</code>
* is finally rendered to the target.
*
* The rendering is performed using the scanline algorithm that performs the
* rendering of other shapes and a custom Paint implementation, that supplies
* the pixel values of the rendered image.
*
* @param image the image to render to the destination raster
* @param xform the transform from image space to user space
*/
private void drawRenderedImageImpl(RenderedImage image,
AffineTransform xform,
Rectangle areaOfInterest)
{
// First we compute the transformation. This is made up of 3 parts:
// 1. The areaOfInterest -> image space transform.
// 2. The image space -> user space transform.
// 3. The user space -> device space transform.
AffineTransform t = new AffineTransform();
t.translate(- areaOfInterest.x - image.getMinX(),
- areaOfInterest.y - image.getMinY());
t.concatenate(xform);
t.concatenate(transform);
AffineTransform it = null;
try
{
it = t.createInverse();
}
catch (NoninvertibleTransformException ex)
{
// Ignore -- we return if the transform is not invertible.
}
if (it != null)
{
// Transform the area of interest into user space.
GeneralPath aoi = new GeneralPath(areaOfInterest);
aoi.transform(xform);
// Render the shape using the standard renderer, but with a temporary
// ImagePaint.
ImagePaint p = new ImagePaint(image, it);
Paint savedPaint = paint;
try
{
paint = p;
fillShape(aoi, false);
}
finally
{
paint = savedPaint;
}
}
}
/**
* Renders a renderable image. This produces a RenderedImage, which is
* then passed to {@link #drawRenderedImage(RenderedImage, AffineTransform)}
* to perform the final rendering.
*
* @param image the renderable image to be rendered
* @param xform the transform from image space to user space
*/
public void drawRenderableImage(RenderableImage image, AffineTransform xform)
{
Rectangle areaOfInterest = new Rectangle((int) image.getMinX(),
(int) image.getHeight(),
(int) image.getWidth(),
(int) image.getHeight());
drawRenderableImageImpl(image, xform, areaOfInterest);
}
/**
* Renders a renderable image. This produces a RenderedImage, which is
* then passed to {@link #drawRenderedImage(RenderedImage, AffineTransform)}
* to perform the final rendering. Only the area of the image specified
* by <code>areaOfInterest</code> is rendered.
*
* @param image the renderable image to be rendered
* @param xform the transform from image space to user space
*/
private void drawRenderableImageImpl(RenderableImage image,
AffineTransform xform,
Rectangle areaOfInterest)
{
// TODO: Maybe make more clever usage of a RenderContext here.
RenderedImage rendered = image.createDefaultRendering();
drawRenderedImageImpl(rendered, xform, areaOfInterest);
}
/**
* Draws the specified string at the specified location.
*
* @param text the string to draw
* @param x the x location, relative to the bounding rectangle of the text
* @param y the y location, relative to the bounding rectangle of the text
*/
public void drawString(String text, int x, int y)
{
GlyphVector gv;
synchronized (searchTextKey)
{
TextCacheKey tck = searchTextKey;
FontRenderContext frc = getFontRenderContext();
tck.setString(text);
tck.setFont(font);
tck.setFontRenderContext(frc);
if (gvCache.containsKey(tck))
{
gv = gvCache.get(tck);
}
else
{
gv = font.createGlyphVector(frc, text.toCharArray());
gvCache.put(new TextCacheKey(text, font, frc), gv);
}
}
drawGlyphVector(gv, x, y);
}
/**
* Draws the specified string at the specified location.
*
* @param text the string to draw
* @param x the x location, relative to the bounding rectangle of the text
* @param y the y location, relative to the bounding rectangle of the text
*/
public void drawString(String text, float x, float y)
{
FontRenderContext ctx = getFontRenderContext();
GlyphVector gv = font.createGlyphVector(ctx, text.toCharArray());
drawGlyphVector(gv, x, y);
}
/**
* Draws the specified string (as AttributedCharacterIterator) at the
* specified location.
*
* @param iterator the string to draw
* @param x the x location, relative to the bounding rectangle of the text
* @param y the y location, relative to the bounding rectangle of the text
*/
public void drawString(AttributedCharacterIterator iterator, int x, int y)
{
FontRenderContext ctx = getFontRenderContext();
GlyphVector gv = font.createGlyphVector(ctx, iterator);
drawGlyphVector(gv, x, y);
}
/**
* Draws the specified string (as AttributedCharacterIterator) at the
* specified location.
*
* @param iterator the string to draw
* @param x the x location, relative to the bounding rectangle of the text
* @param y the y location, relative to the bounding rectangle of the text
*/
public void drawString(AttributedCharacterIterator iterator, float x, float y)
{
FontRenderContext ctx = getFontRenderContext();
GlyphVector gv = font.createGlyphVector(ctx, iterator);
drawGlyphVector(gv, x, y);
}
/**
* Fills the specified shape with the current foreground.
*
* @param shape the shape to fill
*/
public void fill(Shape shape)
{
fillShape(shape, false);
}
public boolean hit(Rectangle rect, Shape text, boolean onStroke)
{
// FIXME: Implement this.
throw new UnsupportedOperationException("Not yet implemented");
}
/**
* Sets the composite.
*
* @param comp the composite to set
*/
public void setComposite(Composite comp)
{
if (! (comp instanceof AlphaComposite))
{
// FIXME: this check is only required "if this Graphics2D
// context is drawing to a Component on the display screen".
SecurityManager sm = System.getSecurityManager();
if (sm != null)
sm.checkPermission(new AWTPermission("readDisplayPixels"));
}
composite = comp;
if (! (comp.equals(AlphaComposite.SrcOver)))
isOptimized = false;
else
updateOptimization();
}
/**
* Sets the current foreground.
*
* @param p the foreground to set.
*/
public void setPaint(Paint p)
{
if (p != null)
{
paint = p;
if (! (paint instanceof Color))
isOptimized = false;
else
{
updateOptimization();
}
}
}
/**
* Sets the stroke for this graphics object.
*
* @param s the stroke to set
*/
public void setStroke(Stroke s)
{
stroke = s;
if (! stroke.equals(new BasicStroke()))
isOptimized = false;
else
updateOptimization();
}
/**
* Sets the specified rendering hint.
*
* @param hintKey the key of the rendering hint
* @param hintValue the value
*/
public void setRenderingHint(Key hintKey, Object hintValue)
{
renderingHints.put(hintKey, hintValue);
}
/**
* Returns the rendering hint for the specified key.
*
* @param hintKey the rendering hint key
*
* @return the rendering hint for the specified key
*/
public Object getRenderingHint(Key hintKey)
{
return renderingHints.get(hintKey);
}
/**
* Sets the specified rendering hints.
*
* @param hints the rendering hints to set
*/
public void setRenderingHints(Map hints)
{
renderingHints.clear();
renderingHints.putAll(hints);
}
/**
* Adds the specified rendering hints.
*
* @param hints the rendering hints to add
*/
public void addRenderingHints(Map hints)
{
renderingHints.putAll(hints);
}
/**
* Returns the current rendering hints.
*
* @return the current rendering hints
*/
public RenderingHints getRenderingHints()
{
return (RenderingHints) renderingHints.clone();
}
/**
* Translates the coordinate system by (x, y).
*
* @param x the translation X coordinate
* @param y the translation Y coordinate
*/
public void translate(int x, int y)
{
transform.translate(x, y);
// Update the clip. We special-case rectangular clips here, because they
// are so common (e.g. in Swing).
if (clip != null)
{
if (clip instanceof Rectangle)
{
Rectangle r = (Rectangle) clip;
r.x -= x;
r.y -= y;
setClip(r);
}
else
{
AffineTransform clipTransform = new AffineTransform();
clipTransform.translate(-x, -y);
updateClip(clipTransform);
}
}
}
/**
* Translates the coordinate system by (tx, ty).
*
* @param tx the translation X coordinate
* @param ty the translation Y coordinate
*/
public void translate(double tx, double ty)
{
transform.translate(tx, ty);
// Update the clip. We special-case rectangular clips here, because they
// are so common (e.g. in Swing).
if (clip != null)
{
if (clip instanceof Rectangle)
{
Rectangle r = (Rectangle) clip;
r.x -= tx;
r.y -= ty;
}
else
{
AffineTransform clipTransform = new AffineTransform();
clipTransform.translate(-tx, -ty);
updateClip(clipTransform);
}
}
}
/**
* Rotates the coordinate system by <code>theta</code> degrees.
*
* @param theta the angle be which to rotate the coordinate system
*/
public void rotate(double theta)
{
transform.rotate(theta);
if (clip != null)
{
AffineTransform clipTransform = new AffineTransform();
clipTransform.rotate(-theta);
updateClip(clipTransform);
}
updateOptimization();
}
/**
* Rotates the coordinate system by <code>theta</code> around the point
* (x, y).
*
* @param theta the angle by which to rotate the coordinate system
* @param x the point around which to rotate, X coordinate
* @param y the point around which to rotate, Y coordinate
*/
public void rotate(double theta, double x, double y)
{
transform.rotate(theta, x, y);
if (clip != null)
{
AffineTransform clipTransform = new AffineTransform();
clipTransform.rotate(-theta, x, y);
updateClip(clipTransform);
}
updateOptimization();
}
/**
* Scales the coordinate system by the factors <code>scaleX</code> and
* <code>scaleY</code>.
*
* @param scaleX the factor by which to scale the X axis
* @param scaleY the factor by which to scale the Y axis
*/
public void scale(double scaleX, double scaleY)
{
transform.scale(scaleX, scaleY);
if (clip != null)
{
AffineTransform clipTransform = new AffineTransform();
clipTransform.scale(1 / scaleX, 1 / scaleY);
updateClip(clipTransform);
}
updateOptimization();
}
/**
* Shears the coordinate system by <code>shearX</code> and
* <code>shearY</code>.
*
* @param shearX the X shearing
* @param shearY the Y shearing
*/
public void shear(double shearX, double shearY)
{
transform.shear(shearX, shearY);
if (clip != null)
{
AffineTransform clipTransform = new AffineTransform();
clipTransform.shear(-shearX, -shearY);
updateClip(clipTransform);
}
updateOptimization();
}
/**
* Transforms the coordinate system using the specified transform
* <code>t</code>.
*
* @param t the transform
*/
public void transform(AffineTransform t)
{
transform.concatenate(t);
try
{
AffineTransform clipTransform = t.createInverse();
updateClip(clipTransform);
}
catch (NoninvertibleTransformException ex)
{
// TODO: How can we deal properly with this?
ex.printStackTrace();
}
updateOptimization();
}
/**
* Sets the transformation for this Graphics object.
*
* @param t the transformation to set
*/
public void setTransform(AffineTransform t)
{
// Transform clip into target space using the old transform.
updateClip(transform);
transform.setTransform(t);
// Transform the clip back into user space using the inverse new transform.
try
{
updateClip(transform.createInverse());
}
catch (NoninvertibleTransformException ex)
{
// TODO: How can we deal properly with this?
ex.printStackTrace();
}
updateOptimization();
}
/**
* Returns the transformation of this coordinate system.
*
* @return the transformation of this coordinate system
*/
public AffineTransform getTransform()
{
return (AffineTransform) transform.clone();
}
/**
* Returns the current foreground.
*
* @return the current foreground
*/
public Paint getPaint()
{
return paint;
}
/**
* Returns the current composite.
*
* @return the current composite
*/
public Composite getComposite()
{
return composite;
}
/**
* Sets the current background.
*
* @param color the background to set.
*/
public void setBackground(Color color)
{
background = color;
}
/**
* Returns the current background.
*
* @return the current background
*/
public Color getBackground()
{
return background;
}
/**
* Returns the current stroke.
*
* @return the current stroke
*/
public Stroke getStroke()
{
return stroke;
}
/**
* Intersects the clip of this graphics object with the specified clip.
*
* @param s the clip with which the current clip should be intersected
*/
public void clip(Shape s)
{
// Initialize clip if not already present.
if (clip == null)
setClip(s);
// This is so common, let's optimize this.
else if (clip instanceof Rectangle && s instanceof Rectangle)
{
Rectangle clipRect = (Rectangle) clip;
Rectangle r = (Rectangle) s;
computeIntersection(r.x, r.y, r.width, r.height, clipRect);
// Call setClip so that subclasses get notified.
setClip(clipRect);
}
else
{
Area current;
if (clip instanceof Area)
current = (Area) clip;
else
current = new Area(clip);
Area intersect;
if (s instanceof Area)
intersect = (Area) s;
else
intersect = new Area(s);
current.intersect(intersect);
clip = current;
isOptimized = false;
// Call setClip so that subclasses get notified.
setClip(clip);
}
}
public FontRenderContext getFontRenderContext()
{
// Protect our own transform from beeing modified.
AffineTransform tf = new AffineTransform(transform);
// TODO: Determine antialias and fractionalmetrics parameters correctly.
return new FontRenderContext(tf, false, true);
}
/**
* Draws the specified glyph vector at the specified location.
*
* @param gv the glyph vector to draw
* @param x the location, x coordinate
* @param y the location, y coordinate
*/
public void drawGlyphVector(GlyphVector gv, float x, float y)
{
translate(x, y);
fillShape(gv.getOutline(), true);
translate(-x, -y);
}
/**
* Creates a copy of this graphics object.
*
* @return a copy of this graphics object
*/
public Graphics create()
{
AbstractGraphics2D copy = (AbstractGraphics2D) clone();
return copy;
}
/**
* Creates and returns a copy of this Graphics object. This should
* be overridden by subclasses if additional state must be handled when
* cloning. This is called by {@link #create()}.
*
* @return a copy of this Graphics object
*/
protected Object clone()
{
try
{
AbstractGraphics2D copy = (AbstractGraphics2D) super.clone();
// Copy the clip. If it's a Rectangle, preserve that for optimization.
if (clip instanceof Rectangle)
copy.clip = new Rectangle((Rectangle) clip);
else if (clip != null)
copy.clip = new GeneralPath(clip);
else
copy.clip = null;
copy.renderingHints = new RenderingHints(null);
copy.renderingHints.putAll(renderingHints);
copy.transform = new AffineTransform(transform);
// The remaining state is inmmutable and doesn't need to be copied.
return copy;
}
catch (CloneNotSupportedException ex)
{
AWTError err = new AWTError("Unexpected exception while cloning");
err.initCause(ex);
throw err;
}
}
/**
* Returns the current foreground.
*/
public Color getColor()
{
Color c = null;
if (paint instanceof Color)
c = (Color) paint;
return c;
}
/**
* Sets the current foreground.
*
* @param color the foreground to set
*/
public void setColor(Color color)
{
setPaint(color);
}
public void setPaintMode()
{
// FIXME: Implement this.
throw new UnsupportedOperationException("Not yet implemented");
}
public void setXORMode(Color color)
{
// FIXME: Implement this.
throw new UnsupportedOperationException("Not yet implemented");
}
/**
* Returns the current font.
*
* @return the current font
*/
public Font getFont()
{
return font;
}
/**
* Sets the font on this graphics object. When <code>f == null</code>, the
* current setting is not changed.
*
* @param f the font to set
*/
public void setFont(Font f)
{
if (f != null)
font = f;
}
/**
* Returns the font metrics for the specified font.
*
* @param font the font for which to fetch the font metrics
*
* @return the font metrics for the specified font
*/
public FontMetrics getFontMetrics(Font font)
{
return Toolkit.getDefaultToolkit().getFontMetrics(font);
}
/**
* Returns the bounds of the current clip.
*
* @return the bounds of the current clip
*/
public Rectangle getClipBounds()
{
Rectangle b = null;
if (clip != null)
b = clip.getBounds();
return b;
}
/**
* Intersects the current clipping region with the specified rectangle.
*
* @param x the x coordinate of the rectangle
* @param y the y coordinate of the rectangle
* @param width the width of the rectangle
* @param height the height of the rectangle
*/
public void clipRect(int x, int y, int width, int height)
{
clip(new Rectangle(x, y, width, height));
}
/**
* Sets the clip to the specified rectangle.
*
* @param x the x coordinate of the clip rectangle
* @param y the y coordinate of the clip rectangle
* @param width the width of the clip rectangle
* @param height the height of the clip rectangle
*/
public void setClip(int x, int y, int width, int height)
{
setClip(new Rectangle(x, y, width, height));
}
/**
* Returns the current clip.
*
* @return the current clip
*/
public Shape getClip()
{
return clip;
}
/**
* Sets the current clipping area to <code>clip</code>.
*
* @param c the clip to set
*/
public void setClip(Shape c)
{
clip = c;
if (! (clip instanceof Rectangle))
isOptimized = false;
else
updateOptimization();
}
public void copyArea(int x, int y, int width, int height, int dx, int dy)
{
if (isOptimized)
rawCopyArea(x, y, width, height, dx, dy);
else
copyAreaImpl(x, y, width, height, dx, dy);
}
/**
* Draws a line from (x1, y1) to (x2, y2).
*
* This implementation transforms the coordinates and forwards the call to
* {@link #rawDrawLine}.
*/
public void drawLine(int x1, int y1, int x2, int y2)
{
if (isOptimized)
{
int tx = (int) transform.getTranslateX();
int ty = (int) transform.getTranslateY();
rawDrawLine(x1 + tx, y1 + ty, x2 + tx, y2 + ty);
}
else
{
ShapeCache sc = shapeCache;
if (sc.line == null)
sc.line = new Line2D.Float();
sc.line.setLine(x1, y1, x2, y2);
draw(sc.line);
}
}
public void drawRect(int x, int y, int w, int h)
{
if (isOptimized)
{
int tx = (int) transform.getTranslateX();
int ty = (int) transform.getTranslateY();
rawDrawRect(x + tx, y + ty, w, h);
}
else
{
ShapeCache sc = shapeCache;
if (sc.rect == null)
sc.rect = new Rectangle();
sc.rect.setBounds(x, y, w, h);
draw(sc.rect);
}
}
/**
* Fills a rectangle with the current paint.
*
* @param x the upper left corner, X coordinate
* @param y the upper left corner, Y coordinate
* @param width the width of the rectangle
* @param height the height of the rectangle
*/
public void fillRect(int x, int y, int width, int height)
{
if (isOptimized)
{
rawFillRect(x + (int) transform.getTranslateX(),
y + (int) transform.getTranslateY(), width, height);
}
else
{
ShapeCache sc = shapeCache;
if (sc.rect == null)
sc.rect = new Rectangle();
sc.rect.setBounds(x, y, width, height);
fill(sc.rect);
}
}
/**
* Fills a rectangle with the current background color.
*
* This implementation temporarily sets the foreground color to the
* background and forwards the call to {@link #fillRect(int, int, int, int)}.
*
* @param x the upper left corner, X coordinate
* @param y the upper left corner, Y coordinate
* @param width the width of the rectangle
* @param height the height of the rectangle
*/
public void clearRect(int x, int y, int width, int height)
{
if (isOptimized)
rawClearRect(x, y, width, height);
else
{
Paint savedForeground = getPaint();
setPaint(getBackground());
fillRect(x, y, width, height);
setPaint(savedForeground);
}
}
/**
* Draws a rounded rectangle.
*
* @param x the x coordinate of the rectangle
* @param y the y coordinate of the rectangle
* @param width the width of the rectangle
* @param height the height of the rectangle
* @param arcWidth the width of the arcs
* @param arcHeight the height of the arcs
*/
public void drawRoundRect(int x, int y, int width, int height, int arcWidth,
int arcHeight)
{
ShapeCache sc = shapeCache;
if (sc.roundRect == null)
sc.roundRect = new RoundRectangle2D.Float();
sc.roundRect.setRoundRect(x, y, width, height, arcWidth, arcHeight);
draw(sc.roundRect);
}
/**
* Fills a rounded rectangle.
*
* @param x the x coordinate of the rectangle
* @param y the y coordinate of the rectangle
* @param width the width of the rectangle
* @param height the height of the rectangle
* @param arcWidth the width of the arcs
* @param arcHeight the height of the arcs
*/
public void fillRoundRect(int x, int y, int width, int height, int arcWidth,
int arcHeight)
{
ShapeCache sc = shapeCache;
if (sc.roundRect == null)
sc.roundRect = new RoundRectangle2D.Float();
sc.roundRect.setRoundRect(x, y, width, height, arcWidth, arcHeight);
fill(sc.roundRect);
}
/**
* Draws the outline of an oval.
*
* @param x the upper left corner of the bounding rectangle of the ellipse
* @param y the upper left corner of the bounding rectangle of the ellipse
* @param width the width of the ellipse
* @param height the height of the ellipse
*/
public void drawOval(int x, int y, int width, int height)
{
ShapeCache sc = shapeCache;
if (sc.ellipse == null)
sc.ellipse = new Ellipse2D.Float();
sc.ellipse.setFrame(x, y, width, height);
draw(sc.ellipse);
}
/**
* Fills an oval.
*
* @param x the upper left corner of the bounding rectangle of the ellipse
* @param y the upper left corner of the bounding rectangle of the ellipse
* @param width the width of the ellipse
* @param height the height of the ellipse
*/
public void fillOval(int x, int y, int width, int height)
{
ShapeCache sc = shapeCache;
if (sc.ellipse == null)
sc.ellipse = new Ellipse2D.Float();
sc.ellipse.setFrame(x, y, width, height);
fill(sc.ellipse);
}
/**
* Draws an arc.
*/
public void drawArc(int x, int y, int width, int height, int arcStart,
int arcAngle)
{
ShapeCache sc = shapeCache;
if (sc.arc == null)
sc.arc = new Arc2D.Float();
sc.arc.setArc(x, y, width, height, arcStart, arcAngle, Arc2D.OPEN);
draw(sc.arc);
}
/**
* Fills an arc.
*/
public void fillArc(int x, int y, int width, int height, int arcStart,
int arcAngle)
{
ShapeCache sc = shapeCache;
if (sc.arc == null)
sc.arc = new Arc2D.Float();
sc.arc.setArc(x, y, width, height, arcStart, arcAngle, Arc2D.PIE);
draw(sc.arc);
}
public void drawPolyline(int[] xPoints, int[] yPoints, int npoints)
{
ShapeCache sc = shapeCache;
if (sc.polyline == null)
sc.polyline = new GeneralPath();
GeneralPath p = sc.polyline;
p.reset();
if (npoints > 0)
p.moveTo(xPoints[0], yPoints[0]);
for (int i = 1; i < npoints; i++)
p.lineTo(xPoints[i], yPoints[i]);
fill(p);
}
/**
* Draws the outline of a polygon.
*/
public void drawPolygon(int[] xPoints, int[] yPoints, int npoints)
{
ShapeCache sc = shapeCache;
if (sc.polygon == null)
sc.polygon = new Polygon();
sc.polygon.reset();
sc.polygon.xpoints = xPoints;
sc.polygon.ypoints = yPoints;
sc.polygon.npoints = npoints;
draw(sc.polygon);
}
/**
* Fills the outline of a polygon.
*/
public void fillPolygon(int[] xPoints, int[] yPoints, int npoints)
{
ShapeCache sc = shapeCache;
if (sc.polygon == null)
sc.polygon = new Polygon();
sc.polygon.reset();
sc.polygon.xpoints = xPoints;
sc.polygon.ypoints = yPoints;
sc.polygon.npoints = npoints;
fill(sc.polygon);
}
/**
* Draws the specified image at the specified location. This forwards
* to {@link #drawImage(Image, AffineTransform, ImageObserver)}.
*
* @param image the image to render
* @param x the x location to render to
* @param y the y location to render to
* @param observer the image observer to receive notification
*/
public boolean drawImage(Image image, int x, int y, ImageObserver observer)
{
boolean ret;
if (isOptimized)
{
ret = rawDrawImage(image, x + (int) transform.getTranslateX(),
y + (int) transform.getTranslateY(), observer);
}
else
{
AffineTransform t = new AffineTransform();
t.translate(x, y);
ret = drawImage(image, t, observer);
}
return ret;
}
/**
* Draws the specified image at the specified location. The image
* is scaled to the specified width and height. This forwards
* to {@link #drawImage(Image, AffineTransform, ImageObserver)}.
*
* @param image the image to render
* @param x the x location to render to
* @param y the y location to render to
* @param width the target width of the image
* @param height the target height of the image
* @param observer the image observer to receive notification
*/
public boolean drawImage(Image image, int x, int y, int width, int height,
ImageObserver observer)
{
AffineTransform t = new AffineTransform();
t.translate(x, y);
double scaleX = (double) width / (double) image.getWidth(observer);
double scaleY = (double) height / (double) image.getHeight(observer);
t.scale(scaleX, scaleY);
return drawImage(image, t, observer);
}
/**
* Draws the specified image at the specified location. This forwards
* to {@link #drawImage(Image, AffineTransform, ImageObserver)}.
*
* @param image the image to render
* @param x the x location to render to
* @param y the y location to render to
* @param bgcolor the background color to use for transparent pixels
* @param observer the image observer to receive notification
*/
public boolean drawImage(Image image, int x, int y, Color bgcolor,
ImageObserver observer)
{
AffineTransform t = new AffineTransform();
t.translate(x, y);
// TODO: Somehow implement the background option.
return drawImage(image, t, observer);
}
/**
* Draws the specified image at the specified location. The image
* is scaled to the specified width and height. This forwards
* to {@link #drawImage(Image, AffineTransform, ImageObserver)}.
*
* @param image the image to render
* @param x the x location to render to
* @param y the y location to render to
* @param width the target width of the image
* @param height the target height of the image
* @param bgcolor the background color to use for transparent pixels
* @param observer the image observer to receive notification
*/
public boolean drawImage(Image image, int x, int y, int width, int height,
Color bgcolor, ImageObserver observer)
{
AffineTransform t = new AffineTransform();
t.translate(x, y);
double scaleX = (double) image.getWidth(observer) / (double) width;
double scaleY = (double) image.getHeight(observer) / (double) height;
t.scale(scaleX, scaleY);
// TODO: Somehow implement the background option.
return drawImage(image, t, observer);
}
/**
* Draws an image fragment to a rectangular area of the target.
*
* @param image the image to render
* @param dx1 the first corner of the destination rectangle
* @param dy1 the first corner of the destination rectangle
* @param dx2 the second corner of the destination rectangle
* @param dy2 the second corner of the destination rectangle
* @param sx1 the first corner of the source rectangle
* @param sy1 the first corner of the source rectangle
* @param sx2 the second corner of the source rectangle
* @param sy2 the second corner of the source rectangle
* @param observer the image observer to be notified
*/
public boolean drawImage(Image image, int dx1, int dy1, int dx2, int dy2,
int sx1, int sy1, int sx2, int sy2,
ImageObserver observer)
{
int sx = Math.min(sx1, sx1);
int sy = Math.min(sy1, sy2);
int sw = Math.abs(sx1 - sx2);
int sh = Math.abs(sy1 - sy2);
int dx = Math.min(dx1, dx1);
int dy = Math.min(dy1, dy2);
int dw = Math.abs(dx1 - dx2);
int dh = Math.abs(dy1 - dy2);
AffineTransform t = new AffineTransform();
t.translate(sx - dx, sy - dy);
double scaleX = (double) sw / (double) dw;
double scaleY = (double) sh / (double) dh;
t.scale(scaleX, scaleY);
Rectangle areaOfInterest = new Rectangle(sx, sy, sw, sh);
return drawImageImpl(image, t, observer, areaOfInterest);
}
/**
* Draws an image fragment to a rectangular area of the target.
*
* @param image the image to render
* @param dx1 the first corner of the destination rectangle
* @param dy1 the first corner of the destination rectangle
* @param dx2 the second corner of the destination rectangle
* @param dy2 the second corner of the destination rectangle
* @param sx1 the first corner of the source rectangle
* @param sy1 the first corner of the source rectangle
* @param sx2 the second corner of the source rectangle
* @param sy2 the second corner of the source rectangle
* @param bgcolor the background color to use for transparent pixels
* @param observer the image observer to be notified
*/
public boolean drawImage(Image image, int dx1, int dy1, int dx2, int dy2,
int sx1, int sy1, int sx2, int sy2, Color bgcolor,
ImageObserver observer)
{
// FIXME: Do something with bgcolor.
return drawImage(image, dx1, dy1, dx2, dy2, sx1, sy1, sx2, sy2, observer);
}
/**
* Disposes this graphics object.
*/
public void dispose()
{
// Nothing special to do here.
}
/**
* Fills the specified shape. Override this if your backend can efficiently
* fill shapes. This is possible on many systems via a polygon fill
* method or something similar. But keep in mind that Shapes can be quite
* complex (non-convex, with holes etc), which is not necessarily supported
* by all polygon fillers. Also note that you must perform clipping
* before filling the shape.
*
* @param s the shape to fill
* @param isFont <code>true</code> if the shape is a font outline
*/
protected void fillShape(Shape s, boolean isFont)
{
// Determine if we need to antialias stuff.
boolean antialias = false;
if (isFont)
{
Object v = renderingHints.get(RenderingHints.KEY_TEXT_ANTIALIASING);
// We default to antialiasing for text rendering.
antialias = v == RenderingHints.VALUE_TEXT_ANTIALIAS_ON
|| (v == RenderingHints.VALUE_TEXT_ANTIALIAS_DEFAULT
&& DEFAULT_TEXT_AA);
}
else
{
Object v = renderingHints.get(RenderingHints.KEY_ANTIALIASING);
antialias = (v == RenderingHints.VALUE_ANTIALIAS_ON);
}
ScanlineConverter sc = getScanlineConverter();
int resolution = 0;
int yRes = 0;
if (antialias)
{
// Adjust resolution according to rendering hints.
resolution = 2;
yRes = 4;
}
sc.renderShape(this, s, clip, transform, resolution, yRes, renderingHints);
freeScanlineConverter(sc);
}
/**
* Returns the color model of this Graphics object.
*
* @return the color model of this Graphics object
*/
protected abstract ColorModel getColorModel();
/**
* Returns the bounds of the target.
*
* @return the bounds of the target
*/
protected Rectangle getDeviceBounds()
{
return destinationRaster.getBounds();
}
/**
* Draws a line in optimization mode. The implementation should respect the
* clip and translation. It can assume that the clip is a rectangle and that
* the transform is only a translating transform.
*
* @param x0 the starting point, X coordinate
* @param y0 the starting point, Y coordinate
* @param x1 the end point, X coordinate
* @param y1 the end point, Y coordinate
*/
protected void rawDrawLine(int x0, int y0, int x1, int y1)
{
ShapeCache sc = shapeCache;
if (sc.line == null)
sc.line = new Line2D.Float();
sc.line.setLine(x0, y0, x1, y1);
draw(sc.line);
}
protected void rawDrawRect(int x, int y, int w, int h)
{
ShapeCache sc = shapeCache;
if (sc.rect == null)
sc.rect = new Rectangle();
sc.rect.setBounds(x, y, w, h);
draw(sc.rect);
}
/**
* Clears a rectangle in optimization mode. The implementation should respect the
* clip and translation. It can assume that the clip is a rectangle and that
* the transform is only a translating transform.
*
* @param x the upper left corner, X coordinate
* @param y the upper left corner, Y coordinate
* @param w the width
* @param h the height
*/
protected void rawClearRect(int x, int y, int w, int h)
{
Paint savedForeground = getPaint();
setPaint(getBackground());
rawFillRect(x, y, w, h);
setPaint(savedForeground);
}
/**
* Fills a rectangle in optimization mode. The implementation should respect
* the clip but can assume that it is a rectangle.
*
* @param x the upper left corner, X coordinate
* @param y the upper left corner, Y coordinate
* @param w the width
* @param h the height
*/
protected void rawFillRect(int x, int y, int w, int h)
{
ShapeCache sc = shapeCache;
if (sc.rect == null)
sc.rect = new Rectangle();
sc.rect.setBounds(x, y, w, h);
fill(sc.rect);
}
/**
* Draws an image in optimization mode. The implementation should respect
* the clip but can assume that it is a rectangle.
*
* @param image the image to be painted
* @param x the location, X coordinate
* @param y the location, Y coordinate
* @param obs the image observer to be notified
*
* @return <code>true</code> when the image is painted completely,
* <code>false</code> if it is still rendered
*/
protected boolean rawDrawImage(Image image, int x, int y, ImageObserver obs)
{
AffineTransform t = new AffineTransform();
t.translate(x, y);
return drawImage(image, t, obs);
}
/**
* Copies a rectangular region to another location.
*
* @param x the upper left corner, X coordinate
* @param y the upper left corner, Y coordinate
* @param w the width
* @param h the height
* @param dx
* @param dy
*/
protected void rawCopyArea(int x, int y, int w, int h, int dx, int dy)
{
copyAreaImpl(x, y, w, h, dx, dy);
}
// Private implementation methods.
/**
* Copies a rectangular area of the target raster to a different location.
*/
private void copyAreaImpl(int x, int y, int w, int h, int dx, int dy)
{
// FIXME: Implement this properly.
throw new UnsupportedOperationException("Not implemented yet.");
}
/**
* Paints a scanline between x0 and x1. Override this when your backend
* can efficiently draw/fill horizontal lines.
*
* @param x0 the left offset
* @param x1 the right offset
* @param y the scanline
*/
public void renderScanline(int y, ScanlineCoverage c)
{
PaintContext pCtx = paintContext;
int x0 = c.getMinX();
int x1 = c.getMaxX();
Raster paintRaster = pCtx.getRaster(x0, y, x1 - x0, 1);
// Do the anti aliasing thing.
float coverageAlpha = 0;
float maxCoverage = c.getMaxCoverage();
ColorModel cm = pCtx.getColorModel();
DataBuffer db = paintRaster.getDataBuffer();
Point loc = new Point(paintRaster.getMinX(), paintRaster.getMinY());
SampleModel sm = paintRaster.getSampleModel();
WritableRaster writeRaster = Raster.createWritableRaster(sm, db, loc);
WritableRaster alphaRaster = cm.getAlphaRaster(writeRaster);
int pixel;
ScanlineCoverage.Iterator iter = c.iterate();
while (iter.hasNext())
{
ScanlineCoverage.Range range = iter.next();
coverageAlpha = range.getCoverage() / maxCoverage;
if (coverageAlpha < 1.0)
{
for (int x = range.getXPos(); x < range.getXPosEnd(); x++)
{
pixel = alphaRaster.getSample(x, y, 0);
pixel = (int) (pixel * coverageAlpha);
alphaRaster.setSample(x, y, 0, pixel);
}
}
}
ColorModel paintColorModel = pCtx.getColorModel();
CompositeContext cCtx = composite.createContext(paintColorModel,
getColorModel(),
renderingHints);
WritableRaster targetChild = destinationRaster.createWritableTranslatedChild(-x0,- y);
cCtx.compose(paintRaster, targetChild, targetChild);
updateRaster(destinationRaster, x0, y, x1 - x0, 1);
cCtx.dispose();
}
/**
* Initializes this graphics object. This must be called by subclasses in
* order to correctly initialize the state of this object.
*/
protected void init()
{
setPaint(Color.BLACK);
setFont(FONT);
isOptimized = true;
}
/**
* Returns a WritableRaster that is used by this class to perform the
* rendering in. It is not necessary that the target surface immediately
* reflects changes in the raster. Updates to the raster are notified via
* {@link #updateRaster}.
*
* @return the destination raster
*/
protected WritableRaster getDestinationRaster()
{
// TODO: Ideally we would fetch the xdrawable's surface pixels for
// initialization of the raster.
Rectangle db = getDeviceBounds();
if (destinationRaster == null)
{
int[] bandMasks = new int[]{ 0xFF0000, 0xFF00, 0xFF };
destinationRaster = Raster.createPackedRaster(DataBuffer.TYPE_INT,
db.width, db.height,
bandMasks, null);
// Initialize raster with white.
int x0 = destinationRaster.getMinX();
int x1 = destinationRaster.getWidth() + x0;
int y0 = destinationRaster.getMinY();
int y1 = destinationRaster.getHeight() + y0;
int numBands = destinationRaster.getNumBands();
for (int y = y0; y < y1; y++)
{
for (int x = x0; x < x1; x++)
{
for (int b = 0; b < numBands; b++)
destinationRaster.setSample(x, y, b, 255);
}
}
}
return destinationRaster;
}
/**
* Notifies the backend that the raster has changed in the specified
* rectangular area. The raster that is provided in this method is always
* the same as the one returned in {@link #getDestinationRaster}.
* Backends that reflect changes to this raster directly don't need to do
* anything here.
*
* @param raster the updated raster, identical to the raster returned
* by {@link #getDestinationRaster()}
* @param x the upper left corner of the updated region, X coordinate
* @param y the upper lef corner of the updated region, Y coordinate
* @param w the width of the updated region
* @param h the height of the updated region
*/
protected void updateRaster(Raster raster, int x, int y, int w, int h)
{
// Nothing to do here. Backends that need to update their surface
// to reflect the change should override this method.
}
// Some helper methods.
/**
* Helper method to check and update the optimization conditions.
*/
private void updateOptimization()
{
int transformType = transform.getType();
boolean optimizedTransform = false;
if (transformType == AffineTransform.TYPE_IDENTITY
|| transformType == AffineTransform.TYPE_TRANSLATION)
optimizedTransform = true;
boolean optimizedClip = (clip == null || clip instanceof Rectangle);
isOptimized = optimizedClip
&& optimizedTransform && paint instanceof Color
&& composite == AlphaComposite.SrcOver
&& stroke.equals(new BasicStroke());
}
/**
* Calculates the intersection of two rectangles. The result is stored
* in <code>rect</code>. This is basically the same
* like {@link Rectangle#intersection(Rectangle)}, only that it does not
* create new Rectangle instances. The tradeoff is that you loose any data in
* <code>rect</code>.
*
* @param x upper-left x coodinate of first rectangle
* @param y upper-left y coodinate of first rectangle
* @param w width of first rectangle
* @param h height of first rectangle
* @param rect a Rectangle object of the second rectangle
*
* @throws NullPointerException if rect is null
*
* @return a rectangle corresponding to the intersection of the
* two rectangles. An empty rectangle is returned if the rectangles
* do not overlap
*/
private static Rectangle computeIntersection(int x, int y, int w, int h,
Rectangle rect)
{
int x2 = rect.x;
int y2 = rect.y;
int w2 = rect.width;
int h2 = rect.height;
int dx = (x > x2) ? x : x2;
int dy = (y > y2) ? y : y2;
int dw = (x + w < x2 + w2) ? (x + w - dx) : (x2 + w2 - dx);
int dh = (y + h < y2 + h2) ? (y + h - dy) : (y2 + h2 - dy);
if (dw >= 0 && dh >= 0)
rect.setBounds(dx, dy, dw, dh);
else
rect.setBounds(0, 0, 0, 0);
return rect;
}
/**
* Helper method to transform the clip. This is called by the various
* transformation-manipulation methods to update the clip (which is in
* userspace) accordingly.
*
* The transform usually is the inverse transform that was applied to the
* graphics object.
*
* @param t the transform to apply to the clip
*/
private void updateClip(AffineTransform t)
{
if (! (clip instanceof GeneralPath))
clip = new GeneralPath(clip);
GeneralPath p = (GeneralPath) clip;
p.transform(t);
}
/**
* Returns a free scanline converter from the pool.
*
* @return a scanline converter
*/
private ScanlineConverter getScanlineConverter()
{
synchronized (scanlineConverters)
{
ScanlineConverter sc;
if (scanlineConverters.size() > 0)
{
sc = scanlineConverters.removeFirst();
}
else
{
sc = new ScanlineConverter();
}
return sc;
}
}
/**
* Puts a scanline converter back in the pool.
*
* @param sc
*/
private void freeScanlineConverter(ScanlineConverter sc)
{
synchronized (scanlineConverters)
{
scanlineConverters.addLast(sc);
}
}
}
|