Search in sources :

Example 1 with HSLFGroupShape

use of org.apache.poi.hslf.usermodel.HSLFGroupShape in project poi by apache.

the class Graphics2DDemo method main.

/**
     * A simple bar chart demo
     */
public static void main(String[] args) throws Exception {
    HSLFSlideShow ppt = new HSLFSlideShow();
    try {
        //bar chart data. The first value is the bar color, the second is the width
        Object[] def = new Object[] { Color.yellow, 40, Color.green, 60, Color.gray, 30, Color.red, 80 };
        HSLFSlide slide = ppt.createSlide();
        HSLFGroupShape group = new HSLFGroupShape();
        //define position of the drawing in the slide
        Rectangle bounds = new java.awt.Rectangle(200, 100, 350, 300);
        group.setAnchor(bounds);
        group.setInteriorAnchor(new java.awt.Rectangle(0, 0, 100, 100));
        slide.addShape(group);
        Graphics2D graphics = new PPGraphics2D(group);
        //draw a simple bar graph
        int x = 10, y = 10;
        graphics.setFont(new Font("Arial", Font.BOLD, 10));
        for (int i = 0, idx = 1; i < def.length; i += 2, idx++) {
            graphics.setColor(Color.black);
            int width = ((Integer) def[i + 1]).intValue();
            graphics.drawString("Q" + idx, x - 5, y + 10);
            graphics.drawString(width + "%", x + width + 3, y + 10);
            graphics.setColor((Color) def[i]);
            graphics.fill(new Rectangle(x, y, width, 10));
            y += 15;
        }
        graphics.setColor(Color.black);
        graphics.setFont(new Font("Arial", Font.BOLD, 14));
        graphics.draw(group.getInteriorAnchor());
        graphics.drawString("Performance", x + 30, y + 10);
        FileOutputStream out = new FileOutputStream("hslf-graphics.ppt");
        ppt.write(out);
        out.close();
    } finally {
        ppt.close();
    }
}
Also used : Rectangle(java.awt.Rectangle) Rectangle(java.awt.Rectangle) HSLFSlideShow(org.apache.poi.hslf.usermodel.HSLFSlideShow) Font(java.awt.Font) PPGraphics2D(org.apache.poi.hslf.model.PPGraphics2D) Graphics2D(java.awt.Graphics2D) PPGraphics2D(org.apache.poi.hslf.model.PPGraphics2D) HSLFGroupShape(org.apache.poi.hslf.usermodel.HSLFGroupShape) FileOutputStream(java.io.FileOutputStream) HSLFSlide(org.apache.poi.hslf.usermodel.HSLFSlide)

Example 2 with HSLFGroupShape

use of org.apache.poi.hslf.usermodel.HSLFGroupShape in project poi by apache.

the class TestShapes method shapeGroup.

/**
     * Test adding shapes to <code>ShapeGroup</code>
     */
@Test
public void shapeGroup() throws IOException {
    HSLFSlideShow ss = new HSLFSlideShow();
    HSLFSlide slide = ss.createSlide();
    Dimension pgsize = ss.getPageSize();
    HSLFGroupShape group = new HSLFGroupShape();
    group.setAnchor(new Rectangle2D.Double(0, 0, pgsize.getWidth(), pgsize.getHeight()));
    slide.addShape(group);
    HSLFPictureData data = ss.addPicture(_slTests.readFile("clock.jpg"), PictureType.JPEG);
    HSLFPictureShape pict = new HSLFPictureShape(data, group);
    pict.setAnchor(new Rectangle2D.Double(0, 0, 200, 200));
    group.addShape(pict);
    HSLFLine line = new HSLFLine(group);
    line.setAnchor(new Rectangle2D.Double(300, 300, 500, 0));
    group.addShape(line);
    //serialize and read again.
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    ss.write(out);
    out.close();
    ss.close();
    ByteArrayInputStream is = new ByteArrayInputStream(out.toByteArray());
    ss = new HSLFSlideShow(is);
    is.close();
    slide = ss.getSlides().get(0);
    List<HSLFShape> shape = slide.getShapes();
    assertEquals(1, shape.size());
    assertTrue(shape.get(0) instanceof HSLFGroupShape);
    group = (HSLFGroupShape) shape.get(0);
    List<HSLFShape> grshape = group.getShapes();
    assertEquals(2, grshape.size());
    assertTrue(grshape.get(0) instanceof HSLFPictureShape);
    assertTrue(grshape.get(1) instanceof HSLFLine);
    pict = (HSLFPictureShape) grshape.get(0);
    assertEquals(new Rectangle2D.Double(0, 0, 200, 200), pict.getAnchor());
    line = (HSLFLine) grshape.get(1);
    assertEquals(new Rectangle2D.Double(300, 300, 500, 0), line.getAnchor());
    ss.close();
}
Also used : Rectangle2D(java.awt.geom.Rectangle2D) Dimension(java.awt.Dimension) ByteArrayOutputStream(java.io.ByteArrayOutputStream) HSLFSlideShow(org.apache.poi.hslf.usermodel.HSLFSlideShow) HSLFGroupShape(org.apache.poi.hslf.usermodel.HSLFGroupShape) HSLFLine(org.apache.poi.hslf.usermodel.HSLFLine) HSLFPictureShape(org.apache.poi.hslf.usermodel.HSLFPictureShape) HSLFShape(org.apache.poi.hslf.usermodel.HSLFShape) ByteArrayInputStream(java.io.ByteArrayInputStream) HSLFPictureData(org.apache.poi.hslf.usermodel.HSLFPictureData) HSLFSlide(org.apache.poi.hslf.usermodel.HSLFSlide) Test(org.junit.Test)

Aggregations

HSLFGroupShape (org.apache.poi.hslf.usermodel.HSLFGroupShape)2 HSLFSlide (org.apache.poi.hslf.usermodel.HSLFSlide)2 HSLFSlideShow (org.apache.poi.hslf.usermodel.HSLFSlideShow)2 Dimension (java.awt.Dimension)1 Font (java.awt.Font)1 Graphics2D (java.awt.Graphics2D)1 Rectangle (java.awt.Rectangle)1 Rectangle2D (java.awt.geom.Rectangle2D)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 FileOutputStream (java.io.FileOutputStream)1 PPGraphics2D (org.apache.poi.hslf.model.PPGraphics2D)1 HSLFLine (org.apache.poi.hslf.usermodel.HSLFLine)1 HSLFPictureData (org.apache.poi.hslf.usermodel.HSLFPictureData)1 HSLFPictureShape (org.apache.poi.hslf.usermodel.HSLFPictureShape)1 HSLFShape (org.apache.poi.hslf.usermodel.HSLFShape)1 Test (org.junit.Test)1