Difference between revisions of "Documentation/DevGuide/Drawings/Shapes"

From Apache OpenOffice Wiki
Jump to: navigation, search
m (Show sub-chapters in TOC)
Line 1: Line 1:
|PrevPage=Documentation/DevGuide/Drawings/Working with Drawing Documents
|PrevPage=Documentation/DevGuide/Drawings/Working with Drawing Documents

Revision as of 17:04, 11 May 2010

Drawings consist of shapes on draw pages. Shapes are drawing elements, such as rectangles, circles, polygons, and lines. To create a drawing, get a shape by its service name at the ServiceFactory of a drawing document and add it to the appropriate DrawPage.

The code below demonstrates how to create shapes. It consists of a static helper method located in the class ShapeHelper and will be used throughout this chapter to create shapes. The parameter xComponent must be a reference to a loaded drawing document. The x, y, height and width are the desired position and size, and sShapeType expects a service name for the shape, such as "com.sun.star.drawing.RectangleShape". The method does not actually insert the shape into a page. It instantiates it and returns the instance to the caller.


The size and position of a shape can be set before adding a shape to a page. After adding the shape, change the shape properties through com.sun.star.beans.XPropertySet.

 public static XShape createShape( XComponent xComponent,
     int x, int y, int width, int height, String sShapeType) throws java.lang.Exception {
     // query the document for the document-internal service factory
     XMultiServiceFactory xFactory = (XMultiServiceFactory)UnoRuntime.queryInterface(
         XMultiServiceFactory.class, xComponent);
     // get the given Shape service from the factory
     Object xObj = xFactory.createInstance(sShapeType);
     Point aPos = new Point(x, y);
     Size aSize = new Size(width, height);
     // use its XShape interface to determine position and size before insertion
     xShape = (XShape)UnoRuntime.queryInterface(XShape.class, xObj);
     return xShape;
Documentation caution.png Notice, the following restrictions: A shape cannot be inserted into multiple pages, and most methods do not work before the shape is inserted into a draw page.

The previously declared method will be used to create a simple rectangle shape with a size of 10 cm x 5 cm that is positioned in the upper-left, and inserted into a drawing page.

Simple rectangle shape
 // query DrawPage for XShapes interface
 XShapes xShapes = (XShapes)UnoRuntime.queryInterface(XShapes.class, xDrawPage);
 // create the shape 
 XShape xShape = createShape(xComponent, 0, 0, 10000, 5000, "com.sun.star.drawing.RectangleShape");
 // add shape to DrawPage
 // set text
 XText xText = (XText)UnoRuntime.queryInterface( XText.class, xShape );
 xText.setString("My new RectangleShape");
 // to be able to set Properties a XPropertySet interface is needed
 XPropertySet xPropSet = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class, xShape);
 xPropSet.setPropertyValue("CornerRadius", new Integer(1000));
 xPropSet.setPropertyValue("Shadow", new Boolean(true));
 xPopSet.setPropertyValue("ShadowXDistance", new Integer(250));
 xPropSet.setPropertyValue("ShadowYDistance", new Integer(250));
 // blue fill color
 xPropSet.setPropertyValue("FillColor", new Integer(0xC0C0C0));
 // black line color
 xPropSet.setPropertyValue("LineColor", new Integer(0x000000));
 xPropSet.setPropertyValue("Name", "Rounded Gray Rectangle");

The UML diagram in the illustration below describes all services that are included by the com.sun.star.drawing.RectangleShape service and provides an overview of properties that can be used with such a simple shape.

Content on this page is licensed under the Public Documentation License (PDL).
Personal tools
In other languages