01: /* ====================================================================
02: Licensed to the Apache Software Foundation (ASF) under one or more
03: contributor license agreements. See the NOTICE file distributed with
04: this work for additional information regarding copyright ownership.
05: The ASF licenses this file to You under the Apache License, Version 2.0
06: (the "License"); you may not use this file except in compliance with
07: the License. You may obtain a copy of the License at
08:
09: http://www.apache.org/licenses/LICENSE-2.0
10:
11: Unless required by applicable law or agreed to in writing, software
12: distributed under the License is distributed on an "AS IS" BASIS,
13: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: See the License for the specific language governing permissions and
15: limitations under the License.
16: ==================================================================== */
17: package org.apache.poi.hslf.model;
18:
19: import junit.framework.TestCase;
20: import org.apache.poi.hslf.usermodel.SlideShow;
21: import org.apache.poi.hslf.HSLFSlideShow;
22:
23: import java.awt.*;
24: import java.awt.Rectangle;
25: import java.io.ByteArrayOutputStream;
26: import java.io.ByteArrayInputStream;
27:
28: /**
29: * Test drawing shapes via Graphics2D
30: *
31: * @author Yegor Kozlov
32: */
33: public class TestPPGraphics2D extends TestCase {
34: private SlideShow ppt;
35:
36: protected void setUp() throws Exception {
37: String dirname = System.getProperty("HSLF.testdata.path");
38: String filename = dirname + "/empty.ppt";
39: ppt = new SlideShow(new HSLFSlideShow(filename));
40: }
41:
42: public void testGraphics() throws Exception {
43: // Starts off empty
44: assertEquals(0, ppt.getSlides().length);
45:
46: // Add a slide
47: Slide slide = ppt.createSlide();
48: assertEquals(1, ppt.getSlides().length);
49:
50: // Add some stuff into it
51: ShapeGroup group = new ShapeGroup();
52: Dimension pgsize = ppt.getPageSize();
53: java.awt.Rectangle bounds = new java.awt.Rectangle(0, 0,
54: (int) pgsize.getWidth(), (int) pgsize.getHeight());
55: group.setAnchor(bounds);
56: slide.addShape(group);
57:
58: PPGraphics2D graphics = new PPGraphics2D(group);
59: graphics.setColor(Color.blue);
60: graphics.draw(new Rectangle(1296, 2544, 1344, 0));
61:
62: graphics.setColor(Color.red);
63: graphics.setStroke(new BasicStroke((float) 2.5));
64: graphics.drawLine(500, 500, 1500, 2500);
65:
66: graphics.setColor(Color.green);
67: graphics.setPaint(Color.gray);
68: graphics.drawOval(4000, 1000, 1000, 1000);
69:
70: // Write the file out
71: ByteArrayOutputStream out = new ByteArrayOutputStream();
72: ppt.write(out);
73: out.close();
74:
75: // And read it back in
76: ppt = new SlideShow(new HSLFSlideShow(new ByteArrayInputStream(
77: out.toByteArray())));
78: assertEquals(1, ppt.getSlides().length);
79:
80: slide = ppt.getSlides()[0];
81: Shape[] shape = slide.getShapes();
82: assertEquals(shape.length, 1); //group shape
83:
84: assertTrue(shape[0] instanceof ShapeGroup); //group shape
85:
86: group = (ShapeGroup) shape[0];
87: shape = group.getShapes();
88: assertEquals(shape.length, 7);
89: }
90:
91: }
|