home *** CD-ROM | disk | FTP | other *** search
/ Java 1.2 How-To / JavaHowTo.iso / 3rdParty / jbuilder / unsupported / JDK1.2beta3 / SOURCE / SRC.ZIP / java / awt / print / PeekGraphics.java < prev    next >
Encoding:
Java Source  |  1998-03-20  |  42.8 KB  |  1,181 lines

  1. /*
  2.  * @(#)PeekGraphics.java    1.3 98/03/18
  3.  *
  4.  * Copyright 1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  *
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15. package java.awt.print;
  16.  
  17. import java.util.Hashtable;
  18.  
  19. import java.awt.Color;
  20. import java.awt.Graphics2D;
  21. import java.awt.Image;
  22. import java.awt.Shape;
  23.  
  24. import java.awt.font.GlyphSet; 
  25. import java.awt.font.StyledString;
  26. import java.awt.font.TextLayout;
  27.  
  28. import java.awt.geom.AffineTransform;
  29. import java.awt.geom.Area;
  30. import java.awt.geom.Point2D;
  31. import java.awt.geom.Rectangle2D;
  32.  
  33. import java.awt.image.BufferedImage;
  34. import java.awt.image.BufferedImageOp;
  35. import java.awt.image.ImageObserver;
  36. import java.awt.image.RenderedImage;
  37.  
  38. import java.awt.image.renderable.RenderableImage;
  39.  
  40. public class PeekGraphics extends ProxyGraphics2D implements ImageObserver {
  41.  
  42.     /**
  43.      * This is the area containing all of the graphics
  44.      * drawn on the page. If it is null then we ran were
  45.      * unable to calculate a bounding box and do not know
  46.      * the area containing all of the drawing.
  47.      */
  48.     private Area mDrawingArea = new Area();
  49.  
  50.     public PeekGraphics(Graphics2D graphics, PageContext pageContext) {
  51.     super(graphics, pageContext);
  52.     }
  53.  
  54.     public Area getDrawingArea() {
  55.     return mDrawingArea;
  56.     }
  57.  
  58. /* Graphics Drawing */
  59.  
  60.    /** 
  61.      * Draws a line, using the current color, between the points 
  62.      * <code>(x1, y1)</code> and <code>(x2, y2)</code> 
  63.      * in this graphics context's coordinate system. 
  64.      * @param   x1  the first point's <i>x</i> coordinate.
  65.      * @param   y1  the first point's <i>y</i> coordinate.
  66.      * @param   x2  the second point's <i>x</i> coordinate.
  67.      * @param   y2  the second point's <i>y</i> coordinate.
  68.      * @since   JDK1.0
  69.      */
  70.     public void drawLine(int x1, int y1, int x2, int y2) {
  71.     int minX = x1;
  72.     int minY = y1;
  73.  
  74.     int width = Math.abs(x2 - x1) + 1;
  75.     int height = Math.abs(y2 - y1) + 1;
  76.  
  77.     if (minX > x2) {
  78.         minX = x2;
  79.     }
  80.  
  81.     if (minY > y2) {
  82.         minY = y2;
  83.     }
  84.  
  85.     addDrawingRect(minX, minY, width, height);
  86.     }
  87.  
  88.  
  89.     /** 
  90.      * Fills the specified rectangle. 
  91.      * The left and right edges of the rectangle are at 
  92.      * <code>x</code> and <code>x + width - 1</code>. 
  93.      * The top and bottom edges are at 
  94.      * <code>y</code> and <code>y + height - 1</code>. 
  95.      * The resulting rectangle covers an area 
  96.      * <code>width</code> pixels wide by 
  97.      * <code>height</code> pixels tall.
  98.      * The rectangle is filled using the graphics context's current color. 
  99.      * @param         x   the <i>x</i> coordinate 
  100.      *                         of the rectangle to be filled.
  101.      * @param         y   the <i>y</i> coordinate 
  102.      *                         of the rectangle to be filled.
  103.      * @param         width   the width of the rectangle to be filled.
  104.      * @param         height   the height of the rectangle to be filled.
  105.      * @see           java.awt.Graphics#fillRect
  106.      * @see           java.awt.Graphics#clearRect
  107.      * @since         JDK1.0
  108.      */
  109.     public void fillRect(int x, int y, int width, int height) {
  110.     Rectangle2D.Float rect = new Rectangle2D.Float(x, y,width, height);
  111.     addDrawingRect(rect);
  112.     }
  113.  
  114.     /** 
  115.      * Clears the specified rectangle by filling it with the background
  116.      * color of the current drawing surface. This operation does not 
  117.      * use the current paint mode. 
  118.      * <p>
  119.      * Beginning with Java 1.1, the background color 
  120.      * of offscreen images may be system dependent. Applications should 
  121.      * use <code>setColor</code> followed by <code>fillRect</code> to 
  122.      * ensure that an offscreen image is cleared to a specific color. 
  123.      * @param       x the <i>x</i> coordinate of the rectangle to clear.
  124.      * @param       y the <i>y</i> coordinate of the rectangle to clear.
  125.      * @param       width the width of the rectangle to clear.
  126.      * @param       height the height of the rectangle to clear.
  127.      * @see         java.awt.Graphics#fillRect(int, int, int, int)
  128.      * @see         java.awt.Graphics#drawRect
  129.      * @see         java.awt.Graphics#setColor(java.awt.Color)
  130.      * @see         java.awt.Graphics#setPaintMode
  131.      * @see         java.awt.Graphics#setXORMode(java.awt.Color)
  132.      * @since       JDK1.0
  133.      */
  134.     public void clearRect(int x, int y, int width, int height) {
  135.     Rectangle2D.Float rect = new Rectangle2D.Float(x, y, width, height);
  136.     addDrawingRect(rect);
  137.     }
  138.  
  139.     /** 
  140.      * Draws an outlined round-cornered rectangle using this graphics 
  141.      * context's current color. The left and right edges of the rectangle 
  142.      * are at <code>x</code> and <code>x + width</code>, 
  143.      * respectively. The top and bottom edges of the rectangle are at 
  144.      * <code>y</code> and <code>y + height</code>. 
  145.      * @param      x the <i>x</i> coordinate of the rectangle to be drawn.
  146.      * @param      y the <i>y</i> coordinate of the rectangle to be drawn.
  147.      * @param      width the width of the rectangle to be drawn.
  148.      * @param      height the height of the rectangle to be drawn.
  149.      * @param      arcWidth the horizontal diameter of the arc 
  150.      *                    at the four corners.
  151.      * @param      arcHeight the vertical diameter of the arc 
  152.      *                    at the four corners.
  153.      * @see        java.awt.Graphics#fillRoundRect
  154.      * @since      JDK1.0
  155.      */
  156.     public void drawRoundRect(int x, int y, int width, int height,
  157.                   int arcWidth, int arcHeight) {
  158.     Rectangle2D.Float rect = new Rectangle2D.Float(x, y,width, height);
  159.     addDrawingRect(rect);
  160.     }
  161.  
  162.     /** 
  163.      * Fills the specified rounded corner rectangle with the current color.
  164.      * The left and right edges of the rectangle 
  165.      * are at <code>x</code> and <code>x + width - 1</code>, 
  166.      * respectively. The top and bottom edges of the rectangle are at 
  167.      * <code>y</code> and <code>y + height - 1</code>. 
  168.      * @param       x the <i>x</i> coordinate of the rectangle to be filled.
  169.      * @param       y the <i>y</i> coordinate of the rectangle to be filled.
  170.      * @param       width the width of the rectangle to be filled.
  171.      * @param       height the height of the rectangle to be filled.
  172.      * @param       arcWidth the horizontal diameter 
  173.      *                     of the arc at the four corners.
  174.      * @param       arcHeight the vertical diameter 
  175.      *                     of the arc at the four corners.
  176.      * @see         java.awt.Graphics#drawRoundRect
  177.      * @since       JDK1.0
  178.      */
  179.     public void fillRoundRect(int x, int y, int width, int height,
  180.                        int arcWidth, int arcHeight) {
  181.     Rectangle2D.Float rect = new Rectangle2D.Float(x, y,width, height);
  182.     addDrawingRect(rect);
  183.     }
  184.  
  185.     /** 
  186.      * Draws the outline of an oval.
  187.      * The result is a circle or ellipse that fits within the 
  188.      * rectangle specified by the <code>x</code>, <code>y</code>, 
  189.      * <code>width</code>, and <code>height</code> arguments. 
  190.      * <p> 
  191.      * The oval covers an area that is 
  192.      * <code>width + 1</code> pixels wide 
  193.      * and <code>height + 1<code> pixels tall. 
  194.      * @param       x the <i>x</i> coordinate of the upper left 
  195.      *                     corner of the oval to be drawn.
  196.      * @param       y the <i>y</i> coordinate of the upper left 
  197.      *                     corner of the oval to be drawn.
  198.      * @param       width the width of the oval to be drawn.
  199.      * @param       height the height of the oval to be drawn.
  200.      * @see         java.awt.Graphics#fillOval
  201.      * @since       JDK1.0
  202.      */
  203.     public void drawOval(int x, int y, int width, int height) {
  204.     Rectangle2D.Float rect = new Rectangle2D.Float(x, y,  width, height);
  205.     addDrawingRect(rect);
  206.     }
  207.  
  208.  
  209.     /** 
  210.      * Fills an oval bounded by the specified rectangle with the
  211.      * current color.
  212.      * @param       x the <i>x</i> coordinate of the upper left corner 
  213.      *                     of the oval to be filled.
  214.      * @param       y the <i>y</i> coordinate of the upper left corner 
  215.      *                     of the oval to be filled.
  216.      * @param       width the width of the oval to be filled.
  217.      * @param       height the height of the oval to be filled.
  218.      * @see         java.awt.Graphics#drawOval
  219.      * @since       JDK1.0
  220.      */
  221.     public void fillOval(int x, int y, int width, int height) {
  222.     Rectangle2D.Float rect = new Rectangle2D.Float(x, y, width, height);
  223.     addDrawingRect(rect);
  224.     }
  225.  
  226.  
  227.     /**
  228.      * Draws the outline of a circular or elliptical arc 
  229.      * covering the specified rectangle.
  230.      * <p>
  231.      * The resulting arc begins at <code>startAngle</code> and extends  
  232.      * for <code>arcAngle</code> degrees, using the current color.
  233.      * Angles are interpreted such that 0 degrees 
  234.      * is at the 3 o'clock position. 
  235.      * A positive value indicates a counter-clockwise rotation
  236.      * while a negative value indicates a clockwise rotation.
  237.      * <p>
  238.      * The center of the arc is the center of the rectangle whose origin 
  239.      * is (<i>x</i>, <i>y</i>) and whose size is specified by the 
  240.      * <code>width</code> and <code>height</code> arguments. 
  241.      * <p>
  242.      * The resulting arc covers an area 
  243.      * <code>width + 1</code> pixels wide
  244.      * by <code>height + 1</code> pixels tall.
  245.      * @param        x the <i>x</i> coordinate of the 
  246.      *                    upper-left corner of the arc to be drawn.
  247.      * @param        y the <i>y</i>  coordinate of the 
  248.      *                    upper-left corner of the arc to be drawn.
  249.      * @param        width the width of the arc to be drawn.
  250.      * @param        height the height of the arc to be drawn.
  251.      * @param        startAngle the beginning angle.
  252.      * @param        arcAngle the angular extent of the arc, 
  253.      *                    relative to the start angle.
  254.      * @see         java.awt.Graphics#fillArc
  255.      * @since       JDK1.0
  256.      */
  257.     public void drawArc(int x, int y, int width, int height,
  258.                  int startAngle, int arcAngle) {
  259.     Rectangle2D.Float rect = new Rectangle2D.Float(x, y,width, height);
  260.     addDrawingRect(rect);
  261.     }
  262.  
  263.     /** 
  264.      * Fills a circular or elliptical arc covering the specified rectangle.
  265.      * <p>
  266.      * The resulting arc begins at <code>startAngle</code> and extends  
  267.      * for <code>arcAngle</code> degrees.
  268.      * Angles are interpreted such that 0 degrees 
  269.      * is at the 3 o'clock position. 
  270.      * A positive value indicates a counter-clockwise rotation
  271.      * while a negative value indicates a clockwise rotation.
  272.      * <p>
  273.      * The center of the arc is the center of the rectangle whose origin 
  274.      * is (<i>x</i>, <i>y</i>) and whose size is specified by the 
  275.      * <code>width</code> and <code>height</code> arguments. 
  276.      * <p>
  277.      * The resulting arc covers an area 
  278.      * <code>width + 1</code> pixels wide
  279.      * by <code>height + 1</code> pixels tall.
  280.      * @param        x the <i>x</i> coordinate of the 
  281.      *                    upper-left corner of the arc to be filled.
  282.      * @param        y the <i>y</i>  coordinate of the 
  283.      *                    upper-left corner of the arc to be filled.
  284.      * @param        width the width of the arc to be filled.
  285.      * @param        height the height of the arc to be filled.
  286.      * @param        startAngle the beginning angle.
  287.      * @param        arcAngle the angular extent of the arc, 
  288.      *                    relative to the start angle.
  289.      * @see         java.awt.Graphics#drawArc
  290.      * @since       JDK1.0
  291.      */
  292.     public void fillArc(int x, int y, int width, int height,
  293.             int startAngle, int arcAngle) {
  294.     Rectangle2D.Float rect = new Rectangle2D.Float(x, y,width, height);
  295.     addDrawingRect(rect);
  296.     }
  297.  
  298.     /** 
  299.      * Draws a sequence of connected lines defined by 
  300.      * arrays of <i>x</i> and <i>y</i> coordinates. 
  301.      * Each pair of (<i>x</i>, <i>y</i>) coordinates defines a point.
  302.      * The figure is not closed if the first point 
  303.      * differs from the last point.
  304.      * @param       xPoints an array of <i>x</i> points
  305.      * @param       yPoints an array of <i>y</i> points
  306.      * @param       nPoints the total number of points
  307.      * @see         java.awt.Graphics#drawPolygon(int[], int[], int)
  308.      * @since       JDK1.1
  309.      */
  310.     public void drawPolyline(int xPoints[], int yPoints[],
  311.                  int nPoints) {
  312.     if (nPoints > 0) {
  313.         int x = xPoints[0];
  314.         int y = yPoints[0];
  315.  
  316.         for (int i = 1; i < nPoints; i++) {
  317.         drawLine(x, y, xPoints[i], yPoints[i]);
  318.         x = xPoints[i];
  319.         y = yPoints[i];
  320.         }
  321.     }
  322.  
  323.     }
  324.  
  325.     /** 
  326.      * Draws a closed polygon defined by 
  327.      * arrays of <i>x</i> and <i>y</i> coordinates. 
  328.      * Each pair of (<i>x</i>, <i>y</i>) coordinates defines a point.
  329.      * <p>
  330.      * This method draws the polygon defined by <code>nPoint</code> line 
  331.      * segments, where the first <code>nPoint - 1</code> 
  332.      * line segments are line segments from 
  333.      * <code>(xPoints[i - 1], yPoints[i - 1])</code> 
  334.      * to <code>(xPoints[i], yPoints[i])</code>, for 
  335.      * 1 ≤ <i>i</i> ≤ <code>nPoints</code>.  
  336.      * The figure is automatically closed by drawing a line connecting
  337.      * the final point to the first point, if those points are different.
  338.      * @param        xPoints   a an array of <code>x</code> coordinates.
  339.      * @param        yPoints   a an array of <code>y</code> coordinates.
  340.      * @param        nPoints   a the total number of points.
  341.      * @see          java.awt.Graphics#fillPolygon
  342.      * @see          java.awt.Graphics#drawPolyline
  343.      * @since        JDK1.0
  344.      */
  345.     public void drawPolygon(int xPoints[], int yPoints[],
  346.                 int nPoints) {
  347.     if (nPoints > 0) {
  348.         drawPolyline(xPoints, yPoints, nPoints);
  349.             drawLine(xPoints[nPoints - 1], yPoints[nPoints - 1],
  350.              xPoints[0], yPoints[0]);
  351.     }
  352.  
  353.     }
  354.  
  355.    /** 
  356.      * Fills a closed polygon defined by 
  357.      * arrays of <i>x</i> and <i>y</i> coordinates. 
  358.      * <p>
  359.      * This method draws the polygon defined by <code>nPoint</code> line 
  360.      * segments, where the first <code>nPoint - 1</code> 
  361.      * line segments are line segments from 
  362.      * <code>(xPoints[i - 1], yPoints[i - 1])</code> 
  363.      * to <code>(xPoints[i], yPoints[i])</code>, for 
  364.      * 1 ≤ <i>i</i> ≤ <code>nPoints</code>.  
  365.      * The figure is automatically closed by drawing a line connecting
  366.      * the final point to the first point, if those points are different.
  367.      * <p>
  368.      * The area inside the polygon is defined using an 
  369.      * even-odd fill rule, also known as the alternating rule.
  370.      * @param        xPoints   a an array of <code>x</code> coordinates.
  371.      * @param        yPoints   a an array of <code>y</code> coordinates.
  372.      * @param        nPoints   a the total number of points.
  373.      * @see          java.awt.Graphics#drawPolygon(int[], int[], int)
  374.      * @since        JDK1.0
  375.      */
  376.     public void fillPolygon(int xPoints[], int yPoints[],
  377.                 int nPoints) {
  378.     if (nPoints > 0) {
  379.         int minX = xPoints[0];
  380.         int minY = yPoints[0];
  381.         int maxX = xPoints[0];
  382.         int maxY = yPoints[0];
  383.  
  384.         for (int i = 1; i < nPoints; i++) {
  385.  
  386.         if (xPoints[i] < minX) {
  387.             minX = xPoints[i];
  388.         } else if (xPoints[i] > maxX) {
  389.             maxX = xPoints[i];
  390.         }
  391.  
  392.         if (yPoints[i] < minY) {
  393.             minY = yPoints[i];
  394.         } else if (yPoints[i] > maxY) {
  395.             maxY = yPoints[i];
  396.         }
  397.         }
  398.  
  399.         addDrawingRect(minX, minY, maxX - minX, maxY - minY);
  400.     }
  401.     }
  402.  
  403.     /** 
  404.      * Draws the text given by the specified string, using this 
  405.      * graphics context's current font and color. The baseline of the 
  406.      * first character is at position (<i>x</i>, <i>y</i>) in this 
  407.      * graphics context's coordinate system. 
  408.      * @param       str      the string to be drawn.
  409.      * @param       x        the <i>x</i> coordinate.
  410.      * @param       y        the <i>y</i> coordinate.
  411.      * @see         java.awt.Graphics#drawBytes
  412.      * @see         java.awt.Graphics#drawChars
  413.      * @since       JDK1.0
  414.      */
  415.     public void drawString(String str, int x, int y) {
  416.     drawString(new StyledString(str, getFont()), x, y);
  417.     }
  418.  
  419.    /** 
  420.      * Draws as much of the specified image as is currently available.
  421.      * The image is drawn with its top-left corner at 
  422.      * (<i>x</i>, <i>y</i>) in this graphics context's coordinate 
  423.      * space. Transparent pixels in the image do not affect whatever 
  424.      * pixels are already there. 
  425.      * <p>
  426.      * This method returns immediately in all cases, even if the
  427.      * complete image has not yet been loaded, and it has not been dithered 
  428.      * and converted for the current output device.
  429.      * <p>
  430.      * If the image has not yet been completely loaded, then
  431.      * <code>drawImage</code> returns <code>false</code>. As more of
  432.      * the image becomes available, the process that draws the image notifies 
  433.      * the specified image observer.
  434.      * @param    img the specified image to be drawn.
  435.      * @param    x   the <i>x</i> coordinate.
  436.      * @param    y   the <i>y</i> coordinate.
  437.      * @param    observer    object to be notified as more of 
  438.      *                          the image is converted.
  439.      * @see      java.awt.Image
  440.      * @see      java.awt.image.ImageObserver
  441.      * @see      java.awt.image.ImageObserver#imageUpdate(java.awt.Image, int, int, int, int, int)
  442.      * @since    JDK1.0
  443.      */
  444.     public boolean drawImage(Image img, int x, int y, 
  445.                  ImageObserver observer) {
  446.  
  447.     /* The ImageWaiter creation does not return until the
  448.      * image is loaded.
  449.      */
  450.     ImageWaiter dim = new ImageWaiter(img);
  451.  
  452.     addDrawingRect(x, y, dim.getWidth(), dim.getHeight());
  453.  
  454.     return super.drawImage(img, x, y, observer);
  455.     }
  456.  
  457.     /**
  458.      * Draws as much of the specified image as has already been scaled
  459.      * to fit inside the specified rectangle.
  460.      * <p>
  461.      * The image is drawn inside the specified rectangle of this 
  462.      * graphics context's coordinate space, and is scaled if 
  463.      * necessary. Transparent pixels do not affect whatever pixels
  464.      * are already there. 
  465.      * <p>
  466.      * This method returns immediately in all cases, even if the
  467.      * entire image has not yet been scaled, dithered, and converted
  468.      * for the current output device.
  469.      * If the current output representation is not yet complete, then
  470.      * <code>drawImage</code> returns <code>false</code>. As more of
  471.      * the image becomes available, the process that draws the image notifies 
  472.      * the image observer by calling its <code>imageUpdate</code> method.
  473.      * <p>
  474.      * A scaled version of an image will not necessarily be
  475.      * available immediately just because an unscaled version of the
  476.      * image has been constructed for this output device.  Each size of
  477.      * the image may be cached separately and generated from the original
  478.      * data in a separate image production sequence.
  479.      * @param    img    the specified image to be drawn.
  480.      * @param    x      the <i>x</i> coordinate.
  481.      * @param    y      the <i>y</i> coordinate.
  482.      * @param    width  the width of the rectangle.
  483.      * @param    height the height of the rectangle.
  484.      * @param    observer    object to be notified as more of 
  485.      *                          the image is converted.
  486.      * @see      java.awt.Image
  487.      * @see      java.awt.image.ImageObserver
  488.      * @see      java.awt.image.ImageObserver#imageUpdate(java.awt.Image, int, int, int, int, int)
  489.      * @since    JDK1.0
  490.      */
  491.     public boolean drawImage(Image img, int x, int y,
  492.                  int width, int height, 
  493.                  ImageObserver observer) {
  494.  
  495.     addDrawingRect(x, y, width, height);
  496.  
  497.     return super.drawImage(img, x, y, width, height, observer);
  498.  
  499.     }
  500.     
  501.     /** 
  502.      * Draws as much of the specified image as is currently available.
  503.      * The image is drawn with its top-left corner at 
  504.      * (<i>x</i>, <i>y</i>) in this graphics context's coordinate 
  505.      * space.  Transparent pixels are drawn in the specified
  506.      * background color.
  507.      * <p> 
  508.      * This operation is equivalent to filling a rectangle of the
  509.      * width and height of the specified image with the given color and then
  510.      * drawing the image on top of it, but possibly more efficient.
  511.      * <p>
  512.      * This method returns immediately in all cases, even if the
  513.      * complete image has not yet been loaded, and it has not been dithered 
  514.      * and converted for the current output device.
  515.      * <p>
  516.      * If the image has not yet been completely loaded, then
  517.      * <code>drawImage</code> returns <code>false</code>. As more of
  518.      * the image becomes available, the process that draws the image notifies 
  519.      * the specified image observer.
  520.      * @param    img    the specified image to be drawn.
  521.      * @param    x      the <i>x</i> coordinate.
  522.      * @param    y      the <i>y</i> coordinate.
  523.      * @param    bgcolor the background color to paint under the
  524.      *                         non-opaque portions of the image.
  525.      * @param    observer    object to be notified as more of 
  526.      *                          the image is converted.
  527.      * @see      java.awt.Image
  528.      * @see      java.awt.image.ImageObserver
  529.      * @see      java.awt.image.ImageObserver#imageUpdate(java.awt.Image, int, int, int, int, int)
  530.      * @since    JDK1.0
  531.      */
  532.     public boolean drawImage(Image img, int x, int y, 
  533.                  Color bgcolor,
  534.                  ImageObserver observer) {
  535.  
  536.     /* The ImageWaiter creation does not return until the
  537.      * image is loaded.
  538.      */
  539.     ImageWaiter dim = new ImageWaiter(img);
  540.  
  541.     addDrawingRect(x, y, dim.getWidth(), dim.getHeight());
  542.  
  543.     return super.drawImage(img, x, y, bgcolor, observer);
  544.  
  545.     }
  546.  
  547.     /**
  548.      * Draws as much of the specified image as has already been scaled
  549.      * to fit inside the specified rectangle.
  550.      * <p>
  551.      * The image is drawn inside the specified rectangle of this 
  552.      * graphics context's coordinate space, and is scaled if 
  553.      * necessary. Transparent pixels are drawn in the specified
  554.      * background color. 
  555.      * This operation is equivalent to filling a rectangle of the
  556.      * width and height of the specified image with the given color and then
  557.      * drawing the image on top of it, but possibly more efficient.
  558.      * <p>
  559.      * This method returns immediately in all cases, even if the
  560.      * entire image has not yet been scaled, dithered, and converted
  561.      * for the current output device.
  562.      * If the current output representation is not yet complete then
  563.      * <code>drawImage</code> returns <code>false</code>. As more of
  564.      * the image becomes available, the process that draws the image notifies 
  565.      * the specified image observer.
  566.      * <p>
  567.      * A scaled version of an image will not necessarily be
  568.      * available immediately just because an unscaled version of the
  569.      * image has been constructed for this output device.  Each size of
  570.      * the image may be cached separately and generated from the original
  571.      * data in a separate image production sequence.
  572.      * @param    img       the specified image to be drawn.
  573.      * @param    x         the <i>x</i> coordinate.
  574.      * @param    y         the <i>y</i> coordinate.
  575.      * @param    width     the width of the rectangle.
  576.      * @param    height    the height of the rectangle.
  577.      * @param    bgcolor   the background color to paint under the
  578.      *                         non-opaque portions of the image.
  579.      * @param    observer    object to be notified as more of 
  580.      *                          the image is converted.
  581.      * @see      java.awt.Image
  582.      * @see      java.awt.image.ImageObserver
  583.      * @see      java.awt.image.ImageObserver#imageUpdate(java.awt.Image, int, int, int, int, int)
  584.      * @since    JDK1.0
  585.      */
  586.     public boolean drawImage(Image img, int x, int y,
  587.                  int width, int height, 
  588.                  Color bgcolor,
  589.                  ImageObserver observer) {
  590.  
  591.     addDrawingRect(x, y, width, height);
  592.  
  593.     return super.drawImage(img, x, y, width, height, bgcolor, observer);
  594.  
  595.     }
  596.     
  597.     /**
  598.      * Draws as much of the specified area of the specified image as is
  599.      * currently available, scaling it on the fly to fit inside the
  600.      * specified area of the destination drawable surface. Transparent pixels 
  601.      * do not affect whatever pixels are already there.
  602.      * <p>
  603.      * This method returns immediately in all cases, even if the
  604.      * image area to be drawn has not yet been scaled, dithered, and converted
  605.      * for the current output device.
  606.      * If the current output representation is not yet complete then
  607.      * <code>drawImage</code> returns <code>false</code>. As more of
  608.      * the image becomes available, the process that draws the image notifies 
  609.      * the specified image observer.
  610.      * <p>
  611.      * This method always uses the unscaled version of the image
  612.      * to render the scaled rectangle and performs the required
  613.      * scaling on the fly. It does not use a cached, scaled version
  614.      * of the image for this operation. Scaling of the image from source
  615.      * to destination is performed such that the first coordinate
  616.      * of the source rectangle is mapped to the first coordinate of
  617.      * the destination rectangle, and the second source coordinate is
  618.      * mapped to the second destination coordinate. The subimage is
  619.      * scaled and flipped as needed to preserve those mappings.
  620.      * @param       img the specified image to be drawn
  621.      * @param       dx1 the <i>x</i> coordinate of the first corner of the
  622.      *                    destination rectangle.
  623.      * @param       dy1 the <i>y</i> coordinate of the first corner of the
  624.      *                    destination rectangle.
  625.      * @param       dx2 the <i>x</i> coordinate of the second corner of the
  626.      *                    destination rectangle.
  627.      * @param       dy2 the <i>y</i> coordinate of the second corner of the
  628.      *                    destination rectangle.
  629.      * @param       sx1 the <i>x</i> coordinate of the first corner of the
  630.      *                    source rectangle.
  631.      * @param       sy1 the <i>y</i> coordinate of the first corner of the
  632.      *                    source rectangle.
  633.      * @param       sx2 the <i>x</i> coordinate of the second corner of the
  634.      *                    source rectangle.
  635.      * @param       sy2 the <i>y</i> coordinate of the second corner of the
  636.      *                    source rectangle.
  637.      * @param       observer object to be notified as more of the image is
  638.      *                    scaled and converted.
  639.      * @see         java.awt.Image
  640.      * @see         java.awt.image.ImageObserver
  641.      * @see         java.awt.image.ImageObserver#imageUpdate(java.awt.Image, int, int, int, int, int)
  642.      * @since       JDK1.1
  643.      */
  644.     public boolean drawImage(Image img,
  645.                       int dx1, int dy1, int dx2, int dy2,
  646.                       int sx1, int sy1, int sx2, int sy2,
  647.                       ImageObserver observer) {
  648.  
  649.     int width = dx2 - dx1;
  650.     int height = dy2 - dy1;
  651.  
  652.     addDrawingRect(dx1, dy1, width, height);
  653.  
  654.     return super.drawImage(img, dx1, dy1, dx2, dy2,
  655.                    sx1, sy1, sx2, sy2, observer);
  656.  
  657.     }
  658.  
  659.     /**
  660.      * Draws as much of the specified area of the specified image as is
  661.      * currently available, scaling it on the fly to fit inside the
  662.      * specified area of the destination drawable surface. 
  663.      * <p>
  664.      * Transparent pixels are drawn in the specified background color. 
  665.      * This operation is equivalent to filling a rectangle of the
  666.      * width and height of the specified image with the given color and then
  667.      * drawing the image on top of it, but possibly more efficient.
  668.      * <p>
  669.      * This method returns immediately in all cases, even if the
  670.      * image area to be drawn has not yet been scaled, dithered, and converted
  671.      * for the current output device.
  672.      * If the current output representation is not yet complete then
  673.      * <code>drawImage</code> returns <code>false</code>. As more of
  674.      * the image becomes available, the process that draws the image notifies 
  675.      * the specified image observer.
  676.      * <p>
  677.      * This method always uses the unscaled version of the image
  678.      * to render the scaled rectangle and performs the required
  679.      * scaling on the fly. It does not use a cached, scaled version
  680.      * of the image for this operation. Scaling of the image from source
  681.      * to destination is performed such that the first coordinate
  682.      * of the source rectangle is mapped to the first coordinate of
  683.      * the destination rectangle, and the second source coordinate is
  684.      * mapped to the second destination coordinate. The subimage is
  685.      * scaled and flipped as needed to preserve those mappings.
  686.      * @param       img the specified image to be drawn
  687.      * @param       dx1 the <i>x</i> coordinate of the first corner of the
  688.      *                    destination rectangle.
  689.      * @param       dy1 the <i>y</i> coordinate of the first corner of the
  690.      *                    destination rectangle.
  691.      * @param       dx2 the <i>x</i> coordinate of the second corner of the
  692.      *                    destination rectangle.
  693.      * @param       dy2 the <i>y</i> coordinate of the second corner of the
  694.      *                    destination rectangle.
  695.      * @param       sx1 the <i>x</i> coordinate of the first corner of the
  696.      *                    source rectangle.
  697.      * @param       sy1 the <i>y</i> coordinate of the first corner of the
  698.      *                    source rectangle.
  699.      * @param       sx2 the <i>x</i> coordinate of the second corner of the
  700.      *                    source rectangle.
  701.      * @param       sy2 the <i>y</i> coordinate of the second corner of the
  702.      *                    source rectangle.
  703.      * @param       bgcolor the background color to paint under the
  704.      *                    non-opaque portions of the image.
  705.      * @param       observer object to be notified as more of the image is
  706.      *                    scaled and converted.
  707.      * @see         java.awt.Image
  708.      * @see         java.awt.image.ImageObserver
  709.      * @see         java.awt.image.ImageObserver#imageUpdate(java.awt.Image, int, int, int, int, int)
  710.      * @since       JDK1.1
  711.      */
  712.     public boolean drawImage(Image img,
  713.                  int dx1, int dy1, int dx2, int dy2,
  714.                  int sx1, int sy1, int sx2, int sy2,
  715.                  Color bgcolor,
  716.                  ImageObserver observer) {
  717.  
  718.     int width = dx2 - dx1;
  719.     int height = dy2 - dy1;
  720.  
  721.     addDrawingRect(dx1, dy1, width, height);
  722.  
  723.     return super.drawImage(img, dx1, dy1, dx2, dy2,
  724.                    sx1, sy1, sx2, sy2, bgcolor, observer);
  725.  
  726.     }
  727.  
  728.  
  729. /* Graphics2D Drawing */
  730.  
  731.    /**
  732.      * Strokes the outline of a Shape using the settings of the current
  733.      * graphics state.  The rendering attributes applied include the
  734.      * clip, transform, paint or color, composite and stroke attributes.
  735.      * @param s The shape to be drawn.
  736.      * @see #setStroke
  737.      * @see #setPaint
  738.      * @see java.awt.Graphics#setColor
  739.      * @see #transform
  740.      * @see #setTransform
  741.      * @see #clip
  742.      * @see #setClip
  743.      * @see #setComposite
  744.      */
  745.     public void draw(Shape s) {
  746.     addDrawingRect(s.getBounds());
  747.     }
  748.  
  749.     /**
  750.      * Draws an image, applying a transform from image space into user space
  751.      * before drawing.
  752.      * The transformation from user space into device space is done with
  753.      * the current transform in the Graphics2D.
  754.      * The given transformation is applied to the image before the
  755.      * transform attribute in the Graphics2D state is applied.
  756.      * The rendering attributes applied include the clip, transform,
  757.      * and composite attributes. Note that the result is
  758.      * undefined, if the given transform is noninvertible.
  759.      * @param img The image to be drawn.
  760.      * @param xform The transformation from image space into user space.
  761.      * @param obs The image observer to be notified as more of the image
  762.      * is converted.
  763.      * @see #transform
  764.      * @see #setTransform
  765.      * @see #setComposite
  766.      * @see #clip
  767.      * @see #setClip
  768.      */
  769.     public void drawImage(Image img,
  770.               AffineTransform xform,
  771.               ImageObserver obs) {
  772.  
  773.     mDrawingArea = null;
  774.     super.drawImage(img, xform, obs);
  775.  
  776.  
  777. //    if (mDrawingArea != null) {
  778. //        Rectangle2D.Double bbox = new Rectangle2D.Double();
  779. //        Point2D leftTop = new Point2D.Double(0, 0);
  780. //        Point2D rightBottom = new Point2D.Double(getImageWidth(img),
  781. //                             getImageHeight(img));
  782.                                      
  783. //        xform.transform(leftTop, leftTop);
  784. //        xform.transform(rightBottom, rightBottom);
  785.  
  786. //        bbox.setBoundsFromDiagonal(leftTop, rightBottom);
  787. //        addDrawingRect(bbox);
  788.  
  789. //    }    
  790.     }
  791.  
  792.     /**
  793.      * Draws a BufferedImage that is filtered with a BufferedImageOp.
  794.      * The rendering attributes applied include the clip, transform
  795.      * and composite attributes.  This is equivalent to:
  796.      * <pre>
  797.      * img1 = op.filter(img, null);
  798.      * drawImage(img1, new AffineTransform(1f,0f,0f,1f,x,y), null);
  799.      * </pre>
  800.      * @param op The filter to be applied to the image before drawing.
  801.      * @param img The BufferedImage to be drawn.
  802.      * @param x,y The location in user space where the image should be drawn.
  803.      * @see #transform
  804.      * @see #setTransform
  805.      * @see #setComposite
  806.      * @see #clip
  807.      * @see #setClip
  808.      */
  809.     public void drawImage(BufferedImage img,
  810.               BufferedImageOp op,
  811.               int x,
  812.               int y) {
  813.  
  814.     super.drawImage(img, op, x, y);
  815.     }
  816.  
  817.     
  818.     /**
  819.      * Draws an image, applying a transform from image space into user space
  820.      * before drawing.
  821.      * The transformation from user space into device space is done with
  822.      * the current transform in the Graphics2D.
  823.      * The given transformation is applied to the image before the
  824.      * transform attribute in the Graphics2D state is applied.
  825.      * The rendering attributes applied include the clip, transform,
  826.      * and composite attributes. Note that the result is
  827.      * undefined, if the given transform is noninvertible.
  828.      * @param img The image to be drawn.
  829.      * @param xform The transformation from image space into user space.
  830.      * @param obs The image observer to be notified as more of the image
  831.      * is converted.
  832.      * @see #transform
  833.      * @see #setTransform
  834.      * @see #setComposite
  835.      * @see #clip
  836.      * @see #setClip
  837.      */
  838.     public void drawRenderedImage(RenderedImage img,
  839.                   AffineTransform xform) {
  840.         super.drawRenderedImage(img, xform);
  841.     }
  842.  
  843.     public void drawRenderableImage(RenderableImage img,
  844.                     AffineTransform xfrom,
  845.                     Hashtable renderHints,
  846.                     Hashtable renderHintsObserved) {
  847.         super.drawRenderableImage(img, xfrom, renderHints,
  848.                   renderHintsObserved);
  849.     }
  850.  
  851.     /**
  852.      * Draws a string of text.
  853.      * The rendering attributes applied include the clip, transform,
  854.      * paint or color, font and composite attributes.
  855.      * @param s The string to be drawn.
  856.      * @param x,y The coordinates where the string should be drawn.
  857.      * @see #setPaint
  858.      * @see java.awt.Graphics#setColor
  859.      * @see java.awt.Graphics#setFont
  860.      * @see #transform
  861.      * @see #setTransform
  862.      * @see #setComposite
  863.      * @see #clip
  864.      * @see #setClip
  865.      */
  866.     public void drawString(String s,
  867.                float x,
  868.                float y) {
  869.  
  870.  
  871.     /* Map this call to the StyleString draw as we can
  872.      * ask the StyledString for its bounds.
  873.      */
  874.     drawString(new StyledString(s, getFont()), x, y);
  875.     }
  876.  
  877.  
  878.     /**
  879.      * Draws a StyledString.
  880.      * The rendering attributes applied include the clip, transform,
  881.      * paint or color, and composite attributes.  A Font is associated
  882.      * with each character in the StyledString.
  883.      * @param s The StyledString to be drawn.
  884.      * @param x,y The coordinates where the StyledString should be drawn.
  885.      * @see #setPaint
  886.      * @see java.awt.Graphics#setColor
  887.      * @see #transform
  888.      * @see #setTransform
  889.      * @see #setComposite
  890.      * @see #clip
  891.      * @see #setClip
  892.      * @see Font
  893.      */
  894.     public void drawString(StyledString s,
  895.                float x,
  896.                float y) {
  897.  
  898.     /* The bounding box for the StyledString has an origin at
  899.      * (0, 0) but (x, y) is at the baseline of the font so
  900.      * we need to subtract off the ascent.
  901.      */
  902.     addDrawingRect(s.getBounds2D(), x, y - s.getAscent());
  903.     }
  904.  
  905.     /**
  906.      * Draws a GlyphSet.
  907.      * The rendering attributes applied include the clip, transform,
  908.      * paint or color, and composite attributes.  The glyphSet specifies
  909.      * individual glyphs from a Font.
  910.      * @param g The GlyphSet to be drawn.
  911.      * @param x,y The coordinates where the glyphs should be drawn.
  912.      * @see #setPaint
  913.      * @see java.awt.Graphics#setColor
  914.      * @see #transform
  915.      * @see #setTransform
  916.      * @see #setComposite
  917.      * @see #clip
  918.      * @see #setClip
  919.      */
  920.     public void drawString(GlyphSet g,
  921.                float x,
  922.                float y) {
  923.  
  924.     Rectangle2D.Float bbox =
  925.             new Rectangle2D.Float(0,
  926.                       -g.getAscent(),
  927.                       g.getAdvance(),
  928.                       g.getAscent() + g.getDescent());
  929.  
  930.     addDrawingRect(bbox, x, y);
  931.  
  932.     }
  933.  
  934.     public void drawString(TextLayout text, float x, float y) {
  935.  
  936.     addDrawingRect(text.getBounds(), x, y);
  937.     }
  938.  
  939.     /**
  940.      * Fills the interior of a Shape using the settings of the current
  941.      * graphics state. The rendering attributes applied include the
  942.      * clip, transform, paint or color, and composite.
  943.      * @see #setPaint
  944.      * @see java.awt.Graphics#setColor
  945.      * @see #transform
  946.      * @see #setTransform
  947.      * @see #setComposite
  948.      * @see #clip
  949.      * @see #setClip
  950.      */
  951.     public void fill(Shape s) {
  952.     addDrawingRect(s.getBounds());
  953.     }
  954.  
  955.     /**
  956.      * Concatenates the current transform of this Graphics2D with a
  957.      * translation transformation.
  958.      * This is equivalent to calling transform(T), where T is an
  959.      * AffineTransform represented by the following matrix:
  960.      * <pre>
  961.      *        [   1    0    tx  ]
  962.      *        [   0    1    ty  ]
  963.      *        [   0    0    1   ]
  964.      * </pre>
  965.      */
  966.     public void translate(double tx, double ty) {
  967.     super.translate(tx, ty);
  968.     }
  969.  
  970.     /**
  971.      * Concatenates the current transform of this Graphics2D with a
  972.      * rotation transformation.
  973.      * This is equivalent to calling transform(R), where R is an
  974.      * AffineTransform represented by the following matrix:
  975.      * <pre>
  976.      *        [   cos(theta)    -sin(theta)    0   ]
  977.      *        [   sin(theta)     cos(theta)    0   ]
  978.      *        [       0              0         1   ]
  979.      * </pre>
  980.      * Rotating with a positive angle theta rotates points on the positive
  981.      * x axis toward the positive y axis.
  982.      * @param theta The angle of rotation in radians.
  983.      */
  984.     public void rotate(double theta) {
  985.     super.rotate(theta);
  986.     }
  987.  
  988.     /**
  989.      * Concatenates the current transform of this Graphics2D with a
  990.      * translated rotation transformation.
  991.      * This is equivalent to the following sequence of calls:
  992.      * <pre>
  993.      *        translate(x, y);
  994.      *        rotate(theta);
  995.      *        translate(-x, -y);
  996.      * </pre>
  997.      * Rotating with a positive angle theta rotates points on the positive
  998.      * x axis toward the positive y axis.
  999.      * @param theta The angle of rotation in radians.
  1000.      * @param x The x coordinate of the origin of the rotation
  1001.      * @param y The x coordinate of the origin of the rotation
  1002.      */
  1003.     public void rotate(double theta, double x, double y) {
  1004.     super.rotate(theta, x, y);
  1005.     }
  1006.  
  1007.     /**
  1008.      * Concatenates the current transform of this Graphics2D with a
  1009.      * scaling transformation.
  1010.      * This is equivalent to calling transform(S), where S is an
  1011.      * AffineTransform represented by the following matrix:
  1012.      * <pre>
  1013.      *        [   sx   0    0   ]
  1014.      *        [   0    sy   0   ]
  1015.      *        [   0    0    1   ]
  1016.      * </pre>
  1017.      */
  1018.     public void scale(double sx, double sy) {
  1019.     super.scale(sx, sy);
  1020.     }
  1021.  
  1022.     /**
  1023.      * Concatenates the current transform of this Graphics2D with a
  1024.      * shearing transformation.
  1025.      * This is equivalent to calling transform(SH), where SH is an
  1026.      * AffineTransform represented by the following matrix:
  1027.      * <pre>
  1028.      *        [   1   shx   0   ]
  1029.      *        [  shy   1    0   ]
  1030.      *        [   0    0    1   ]
  1031.      * </pre>
  1032.      * @param shx The factor by which coordinates are shifted towards the
  1033.      * positive X axis direction according to their Y coordinate
  1034.      * @param shy The factor by which coordinates are shifted towards the
  1035.      * positive Y axis direction according to their X coordinate
  1036.      */
  1037.     public void shear(double shx, double shy) {
  1038.     super.shear(shx, shy);
  1039.     }
  1040.  
  1041.     /**
  1042.      * Translate
  1043.      */
  1044.     public void translate(int x, int y) {
  1045.         super.translate(x, y);
  1046.     }
  1047.  
  1048.  /* Image Observer */
  1049.     
  1050.     /**
  1051.      * Notify this object when the height or width become available
  1052.      * for an image.
  1053.      */
  1054.     public synchronized boolean imageUpdate(Image img, int infoFlags,
  1055.                         int x, int y,
  1056.                         int width, int height) {
  1057.     
  1058.     boolean gotInfo = false;
  1059.  
  1060.     if((infoFlags & (WIDTH | HEIGHT)) != 0) {
  1061.         gotInfo = true;
  1062.         notify();
  1063.     }
  1064.  
  1065.     return gotInfo;
  1066.     }
  1067.  
  1068.     private synchronized int getImageWidth(Image img) {
  1069.  
  1070.     /* Wait for the width the image to
  1071.      * become available.
  1072.      */
  1073.     while (img.getWidth(this) == -1) {
  1074.         try {
  1075.         wait();
  1076.         } catch (InterruptedException e) {
  1077.         }
  1078.     }
  1079.     
  1080.  
  1081.     return img.getWidth(this);
  1082.     }
  1083.  
  1084.     private synchronized int getImageHeight(Image img) {
  1085.  
  1086.     /* Wait for the height the image to
  1087.      * become available.
  1088.      */
  1089.     while (img.getHeight(this) == -1) {
  1090.         try {
  1091.         wait();
  1092.         } catch (InterruptedException e) {
  1093.         }
  1094.     }
  1095.  
  1096.     
  1097.     return img.getHeight(this);
  1098.     }
  1099.  
  1100.     /**
  1101.      * Shift the rectangle 'rect' to the position ('x', 'y')
  1102.      * and add the resulting rectangle to the area representing
  1103.      * the part of the page which is drawn into.
  1104.      */
  1105.     private void addDrawingRect(Rectangle2D rect, float x, float y) {
  1106.  
  1107.     addDrawingRect((float) (rect.getX() + x),
  1108.                (float) (rect.getY() + y),
  1109.                (float) rect.getWidth(),
  1110.                (float) rect.getHeight());
  1111.  
  1112.     }
  1113.  
  1114.     private void addDrawingRect(float x, float y, float width, float height) {
  1115.  
  1116.     Rectangle2D.Float bbox = new Rectangle2D.Float(x, y, width, height);
  1117.     addDrawingRect(bbox);
  1118.     }
  1119.  
  1120.     /**
  1121.      * Add the rectangle 'rect' to the area representing
  1122.      * the part of the page which is drawn into.
  1123.      */
  1124.     private void addDrawingRect(Rectangle2D rect) {
  1125.     if (mDrawingArea != null) {
  1126.         Area rectArea = new Area(rect);
  1127.         mDrawingArea.add(rectArea);
  1128.     }
  1129.     }
  1130.  
  1131.     /**
  1132.      * This private class does not return from its constructor
  1133.      * until 'img's wdth and height are available.
  1134.      */
  1135.     protected class ImageWaiter implements ImageObserver {
  1136.  
  1137.     private int mWidth;
  1138.     private int mHeight;
  1139.  
  1140.     ImageWaiter(Image img) {
  1141.         waitForDimensions(img);
  1142.     }
  1143.  
  1144.     public int getWidth() {
  1145.         return mWidth;
  1146.     }
  1147.  
  1148.     public int getHeight() {
  1149.         return mHeight;
  1150.     }
  1151.  
  1152.     synchronized private void waitForDimensions(Image img) {
  1153.         mHeight = img.getHeight(null);
  1154.         mWidth = img.getWidth(null);        
  1155.         if (mWidth < 0 || mHeight < 0) {
  1156.         try {
  1157.             wait();
  1158.         } catch(InterruptedException e) {
  1159.             // do nothing.
  1160.         }
  1161.         mHeight = img.getHeight(null);
  1162.         mWidth = img.getWidth(null);        
  1163.         }
  1164.     }
  1165.  
  1166.     synchronized public boolean imageUpdate(Image image, int flags,
  1167.                         int x, int y, int w, int h) {
  1168.  
  1169.         boolean dontCallMeAgain = (flags & (HEIGHT | ABORT | ERROR)) != 0;
  1170.  
  1171.         if (dontCallMeAgain == true) {
  1172.         notify();
  1173.         }
  1174.  
  1175.         return dontCallMeAgain;
  1176.     }
  1177.         
  1178.     }
  1179. }
  1180.  
  1181.