FileDocCategorySizeDatePackage
EscherGraphics2d.javaAPI DocApache Poi 3.0.118039Mon Jun 04 19:18:46 BST 2007org.apache.poi.hssf.usermodel

EscherGraphics2d

public class EscherGraphics2d extends Graphics2D
Translates Graphics2d calls into escher calls. The translation is lossy so many features are not supported and some just aren't implemented yet. If in doubt test the specific calls you wish to make. Graphics calls are always drawn into an EscherGroup so one will need to be created.

Important:

One important concept worth considering is that of font size. One of the difficulties in converting Graphics calls into escher drawing calls is that Excel does not have the concept of absolute pixel positions. It measures it's cell widths in 'characters' and the cell heights in points. Unfortunately it's not defined exactly what a type of character it's measuring. Presumably this is due to the fact that the Excel will be using different fonts on different platforms or even within the same platform.

Because of this constraint you have to calculate the verticalPointsPerPixel. This the amount the font should be scaled by when you issue commands such as drawString(). A good way to calculate this is to use the follow formula:

multipler = groupHeightInPoints / heightOfGroup

The height of the group is calculated fairly simply by calculating the difference between the y coordinates of the bounding box of the shape. The height of the group can be calculated by using a convenience called HSSFClientAnchor.getAnchorHeightInPoints().

author
Glen Stampoultzis (glens at apache.org)

Fields Summary
private EscherGraphics
escherGraphics
private BufferedImage
img
private AffineTransform
trans
private Stroke
stroke
private Paint
paint
private Shape
deviceclip
private POILogger
logger
Constructors Summary
public EscherGraphics2d(EscherGraphics escherGraphics)
Constructs one escher graphics object from an escher graphics object.

param
escherGraphics the original EscherGraphics2d object to copy


                              
      
    
        this.escherGraphics = escherGraphics;
        setImg( new BufferedImage(1, 1, 2) );
        setColor(Color.black);
    
Methods Summary
public voidaddRenderingHints(java.util.Map map)

        getG2D().addRenderingHints(map);
    
public voidclearRect(int i, int j, int k, int l)

        Paint paint1 = getPaint();
        setColor(getBackground());
        fillRect(i, j, k, l);
        setPaint(paint1);
    
public voidclip(java.awt.Shape shape)

        if(getDeviceclip() != null)
        {
            Area area = new Area(getClip());
            if(shape != null)
                area.intersect(new Area(shape));
            shape = area;
        }
        setClip(shape);
    
public voidclipRect(int x, int y, int width, int height)

        clip(new Rectangle(x,y,width,height));
    
public voidcopyArea(int x, int y, int width, int height, int dx, int dy)

        getG2D().copyArea(x,y,width,height,dx,dy);
    
public java.awt.Graphicscreate()

        EscherGraphics2d g2d = new EscherGraphics2d(escherGraphics);
        return g2d;
    
public voiddispose()

        getEscherGraphics().dispose();
        getG2D().dispose();
        getImg().flush();
    
public voiddraw(java.awt.Shape shape)

        if (shape instanceof Line2D)
        {
            Line2D shape2d = (Line2D) shape;

            int width = 0;
            if (stroke != null && stroke instanceof BasicStroke) {
                width = (int) ((BasicStroke)stroke).getLineWidth() * 12700;
            }

            drawLine((int)shape2d.getX1(), (int)shape2d.getY1(), (int)shape2d.getX2(), (int)shape2d.getY2(), width);
        }
        else
        {
            if (logger.check(POILogger.WARN))
                logger.log(POILogger.WARN, "draw not fully supported");
        }
    
public voiddrawArc(int x, int y, int width, int height, int startAngle, int arcAngle)

        draw(new java.awt.geom.Arc2D.Float(x, y, width, height, startAngle, arcAngle, 0));
    
public voiddrawGlyphVector(java.awt.font.GlyphVector g, float x, float y)

        fill(g.getOutline(x, y));
    
public booleandrawImage(java.awt.Image image, int dx1, int dy1, int dx2, int dy2, int sx1, int sy1, int sx2, int sy2, java.awt.Color bgColor, java.awt.image.ImageObserver imageobserver)

        if (logger.check( POILogger.WARN ))
            logger.log(POILogger.WARN,"drawImage() not supported");
        return true;
    
public booleandrawImage(java.awt.Image image, int dx1, int dy1, int dx2, int dy2, int sx1, int sy1, int sx2, int sy2, java.awt.image.ImageObserver imageobserver)

        if (logger.check( POILogger.WARN ))
            logger.log(POILogger.WARN,"drawImage() not supported");
        return drawImage(image, dx1, dy1, dx2, dy2, sx1, sy1, sx2, sy2, null, imageobserver);
    
public booleandrawImage(java.awt.Image image, int dx1, int dy1, int dx2, int dy2, java.awt.Color bgColor, java.awt.image.ImageObserver imageobserver)

        if (logger.check( POILogger.WARN ))
            logger.log(POILogger.WARN,"drawImage() not supported");
        return true;
    
public booleandrawImage(java.awt.Image img, int x, int y, int width, int height, java.awt.image.ImageObserver observer)

        return drawImage(img, x,y,width,height, null, observer);
    
public booleandrawImage(java.awt.Image image, int x, int y, java.awt.Color bgColor, java.awt.image.ImageObserver imageobserver)

        return drawImage(image, x, y, image.getWidth(imageobserver), image.getHeight(imageobserver), bgColor, imageobserver);
    
public booleandrawImage(java.awt.Image image, int x, int y, java.awt.image.ImageObserver imageobserver)

        return drawImage(image, x, y, image.getWidth(imageobserver), image.getHeight(imageobserver), imageobserver);
    
public booleandrawImage(java.awt.Image image, java.awt.geom.AffineTransform affinetransform, java.awt.image.ImageObserver imageobserver)

        AffineTransform affinetransform1 = (AffineTransform)getTrans().clone();
        getTrans().concatenate(affinetransform);
        drawImage(image, 0, 0, imageobserver);
        setTrans( affinetransform1 );
        return true;
    
public voiddrawImage(java.awt.image.BufferedImage bufferedimage, java.awt.image.BufferedImageOp op, int x, int y)

        BufferedImage img = op.filter(bufferedimage, null);
        drawImage(((Image) (img)), new AffineTransform(1.0F, 0.0F, 0.0F, 1.0F, x, y), null);
    
public voiddrawLine(int x1, int y1, int x2, int y2, int width)

        getEscherGraphics().drawLine(x1,y1,x2,y2, width);
    
public voiddrawLine(int x1, int y1, int x2, int y2)

        int width = 0;
        if (stroke != null && stroke instanceof BasicStroke) {
            width = (int) ((BasicStroke)stroke).getLineWidth() * 12700;
        }
        getEscherGraphics().drawLine(x1,y1,x2,y2, width);
//        draw(new GeneralPath(new java.awt.geom.Line2D.Float(x1, y1, x2, y2)));
    
public voiddrawOval(int x, int y, int width, int height)

        getEscherGraphics().drawOval(x,y,width,height);
//        draw(new java.awt.geom.Ellipse2D.Float(x, y, width, height));
    
public voiddrawPolygon(int[] xPoints, int[] yPoints, int nPoints)

        getEscherGraphics().drawPolygon(xPoints, yPoints, nPoints);
    
public voiddrawPolyline(int[] xPoints, int[] yPoints, int nPoints)

        if(nPoints > 0)
        {
            GeneralPath generalpath = new GeneralPath();
            generalpath.moveTo(xPoints[0], yPoints[0]);
            for(int j = 1; j < nPoints; j++)
                generalpath.lineTo(xPoints[j], yPoints[j]);

            draw(generalpath);
        }
    
public voiddrawRect(int x, int y, int width, int height)

        escherGraphics.drawRect(x,y,width,height);
    
public voiddrawRenderableImage(java.awt.image.renderable.RenderableImage renderableimage, java.awt.geom.AffineTransform affinetransform)

        drawRenderedImage(renderableimage.createDefaultRendering(), affinetransform);
    
public voiddrawRenderedImage(java.awt.image.RenderedImage renderedimage, java.awt.geom.AffineTransform affinetransform)

        BufferedImage bufferedimage = new BufferedImage(renderedimage.getColorModel(), renderedimage.getData().createCompatibleWritableRaster(), false, null);
        bufferedimage.setData(renderedimage.getData());
        drawImage(bufferedimage, affinetransform, null);
    
public voiddrawRoundRect(int i, int j, int k, int l, int i1, int j1)

        draw(new java.awt.geom.RoundRectangle2D.Float(i, j, k, l, i1, j1));
    
public voiddrawString(java.lang.String string, float x, float y)

        getEscherGraphics().drawString(string, (int)x, (int)y);
    
public voiddrawString(java.lang.String string, int x, int y)

        getEscherGraphics().drawString(string, x, y);
    
public voiddrawString(java.text.AttributedCharacterIterator attributedcharacteriterator, float x, float y)

        TextLayout textlayout = new TextLayout(attributedcharacteriterator, getFontRenderContext());
        Paint paint1 = getPaint();
        setColor(getColor());
        fill(textlayout.getOutline(AffineTransform.getTranslateInstance(x, y)));
        setPaint(paint1);
    
public voiddrawString(java.text.AttributedCharacterIterator attributedcharacteriterator, int x, int y)

        drawString(attributedcharacteriterator, x, y);
    
public voidfill(java.awt.Shape shape)

        if (logger.check( POILogger.WARN ))
            logger.log(POILogger.WARN,"fill(Shape) not supported");
    
public voidfillArc(int i, int j, int k, int l, int i1, int j1)

        fill(new java.awt.geom.Arc2D.Float(i, j, k, l, i1, j1, 2));
    
public voidfillOval(int x, int y, int width, int height)

        escherGraphics.fillOval(x,y,width,height);
    
public voidfillPolygon(int[] xPoints, int[] yPoints, int nPoints)
Fills a (closed) polygon, as defined by a pair of arrays, which hold the x and y coordinates.

This draws the polygon, with nPoint line segments. The first nPoint - 1 line segments are drawn between sequential points (xPoints[i],yPoints[i],xPoints[i+1],yPoints[i+1]). The final line segment is a closing one, from the last point to the first (assuming they are different).

The area inside of the polygon is defined by using an even-odd fill rule (also known as the alternating rule), and the area inside of it is filled.

param
xPoints array of the x coordinates.
param
yPoints array of the y coordinates.
param
nPoints the total number of points in the polygon.
see
java.awt.Graphics#drawPolygon(int[], int[], int)

        escherGraphics.fillPolygon(xPoints, yPoints, nPoints);
    
public voidfillRect(int x, int y, int width, int height)

        getEscherGraphics().fillRect(x,y,width,height);
    
public voidfillRoundRect(int x, int y, int width, int height, int arcWidth, int arcHeight)

        fill(new java.awt.geom.RoundRectangle2D.Float(x, y, width, height, arcWidth, arcHeight));
    
public java.awt.ColorgetBackground()

        return getEscherGraphics().getBackground();
    
public java.awt.ShapegetClip()

        try
        {
            return getTrans().createInverse().createTransformedShape(getDeviceclip());
        }
        catch(Exception _ex)
        {
            return null;
        }
    
public java.awt.RectanglegetClipBounds()

        if(getDeviceclip() != null)
            return getClip().getBounds();
        else
            return null;
    
public java.awt.ColorgetColor()

        return escherGraphics.getColor();
    
public java.awt.CompositegetComposite()

        return getG2D().getComposite();
    
public java.awt.GraphicsConfigurationgetDeviceConfiguration()

        return getG2D().getDeviceConfiguration();
    
private java.awt.ShapegetDeviceclip()

        return deviceclip;
    
private org.apache.poi.hssf.usermodel.EscherGraphicsgetEscherGraphics()

        return escherGraphics;
    
public java.awt.FontgetFont()

        return getEscherGraphics().getFont();
    
public java.awt.FontMetricsgetFontMetrics(java.awt.Font font)

        return getEscherGraphics().getFontMetrics(font);
    
public java.awt.font.FontRenderContextgetFontRenderContext()

        getG2D().setTransform(getTrans());
        return getG2D().getFontRenderContext();
    
private java.awt.Graphics2DgetG2D()

        return (Graphics2D) img.getGraphics();
    
private java.awt.image.BufferedImagegetImg()

        return img;
    
public java.awt.PaintgetPaint()

        return paint;
    
public java.lang.ObjectgetRenderingHint(java.awt.RenderingHints$Key key)

        return getG2D().getRenderingHint(key);
    
public java.awt.RenderingHintsgetRenderingHints()

        return getG2D().getRenderingHints();
    
public java.awt.StrokegetStroke()

        return stroke;
    
private java.awt.geom.AffineTransformgetTrans()

        return trans;
    
public java.awt.geom.AffineTransformgetTransform()

        return (AffineTransform)getTrans().clone();
    
public booleanhit(java.awt.Rectangle rectangle, java.awt.Shape shape, boolean flag)

        getG2D().setTransform(getTrans());
        getG2D().setStroke(getStroke());
        getG2D().setClip(getClip());
        return getG2D().hit(rectangle, shape, flag);
    
public voidrotate(double d)

        getTrans().rotate(d);
    
public voidrotate(double d, double d1, double d2)

        getTrans().rotate(d, d1, d2);
    
public voidscale(double d, double d1)

        getTrans().scale(d, d1);
    
public voidsetBackground(java.awt.Color c)

        getEscherGraphics().setBackground(c);
    
public voidsetClip(int i, int j, int k, int l)

        setClip(((Shape) (new Rectangle(i, j, k, l))));
    
public voidsetClip(java.awt.Shape shape)

        setDeviceclip( getTrans().createTransformedShape(shape) );
    
public voidsetColor(java.awt.Color c)

        escherGraphics.setColor(c);
    
public voidsetComposite(java.awt.Composite composite)

        getG2D().setComposite(composite);
    
private voidsetDeviceclip(java.awt.Shape deviceclip)

        this.deviceclip = deviceclip;
    
public voidsetFont(java.awt.Font font)

        getEscherGraphics().setFont(font);
    
private voidsetImg(java.awt.image.BufferedImage img)

        this.img = img;
    
public voidsetPaint(java.awt.Paint paint1)

        if(paint1 != null)
        {
            paint = paint1;
            if(paint1 instanceof Color)
                setColor( (Color)paint1 );
        }
    
public voidsetPaintMode()

        getEscherGraphics().setPaintMode();
    
public voidsetRenderingHint(java.awt.RenderingHints$Key key, java.lang.Object obj)

        getG2D().setRenderingHint(key, obj);
    
public voidsetRenderingHints(java.util.Map map)

        getG2D().setRenderingHints(map);
    
public voidsetStroke(java.awt.Stroke s)

        stroke = s;
    
private voidsetTrans(java.awt.geom.AffineTransform trans)

        this.trans = trans;
    
public voidsetTransform(java.awt.geom.AffineTransform affinetransform)

        setTrans( (AffineTransform)affinetransform.clone() );
    
public voidsetXORMode(java.awt.Color color1)

        getEscherGraphics().setXORMode(color1);
    
public voidshear(double d, double d1)

        getTrans().shear(d, d1);
    
public voidtransform(java.awt.geom.AffineTransform affinetransform)

        getTrans().concatenate(affinetransform);
    
public voidtranslate(double d, double d1)

        getTrans().translate(d, d1);
    
public voidtranslate(int i, int j)

        getTrans().translate(i, j);