JasperReports Ultimate Guide - Sample Reference - Schema Reference - Configuration Reference - API (Javadoc)

JasperReports - Shapes Sample (version 4.6.0)


Shows how various graphic elements could be used inside report templates.

Download All Sample Source Files
Browse Sample Source Files on SVN


Main Features in This Sample

Lines, Rectangles, Ellipses (Shapes)


top

Lines, Rectangles, Ellipses (Shapes)Documented by Sanda Zaharia


Description / Goal
How to use shape elements such as rectangles, ellipses and lines.

Since
0.1.0


Common graphic elements

Shapes are predefined geometric objects, very useful when drawing diagrams, flowcharts and other schematic representations. There are a lot of shapes: lines, rectangles, arrows, callouts, stars, etc. All of them can be obtained from a set of line shapes, taking into account that a line shape can be either straight or curved.
JasperReports provides support for lines, rectangles and ellipses. All shape objects in JasperReports are graphic report elements which implement both JRCommonElement and JRGraphicElement interfaces.
By default any common element is characterized by its width, height, unique key identifier, visualization mode, foreground and background colors. Methods exposed by the JRCommonElement are:
  • public int getWidth();
  • public int getHeight();
  • public String getKey();
  • public ModeEnum getModeValue();
  • public ModeEnum getOwnModeValue();
  • public void setMode(ModeEnum mode);
  • public Color getForecolor();
  • public Color getOwnForecolor();
  • public void setForecolor(Color forecolor);
  • public Color getBackcolor();
  • public Color getOwnBackcolor();
  • public void setBackcolor(Color backcolor);

The JRGraphicElement interface extends the JRCommonGraphicElement. By default any common graphic element (or shape) should have a contour line and a fill type (ie solid, no fill, etc).
Methods which should be implemented from the JRCommonGraphicElement interface are:
  • public JRPen getLinePen();
  • public FillEnum getFillValue();
  • public FillEnum getOwnFillValue();
  • public void setFill(FillEnum fillEnum);
The line shape

Derived from common graphic elements, a line is an one-dimensional shape which is supposed to have a given direction. Any line element should implement the JRLine interface which exposes the following methods:
  • public LineDirectionEnum getDirectionValue();
  • public void setDirection(LineDirectionEnum lineDirectionEnum);
Possible line directions are: top-down or bottom-up. The default direction is top-down.
In the .jrxml file a line element is represented as follows:
  <line direction="BottomUp">
    <reportElement x="0" y="150" width="200" height="40" forecolor="#ff00"/>
    <graphicElement>
      <pen lineWidth="4"/>
    </graphicElement>
  </line>
The graphicElement's fill attribute is not specified because at the moment only the solid fill type is taken into account and, it is considered as default value.

The rectangle shape

A rectangle is a bi-dimensional shape having either rounded corners or no-rounded ones. The radius element, if set, indicates how the rectangle's corners should be rounded. By default corners are not rounded in a rectangle.
Any rectangle shape implements the JRRectangle interface, which extends the JRCommonRectangle. The JRCommonRectangle exposes the following methods:
  • public int getRadius();
  • public Integer getOwnRadius();
  • public void setRadius(int radius);
  • public void setRadius(Integer radius);
In the .jrxml file a rectangle element is represented as follows:
  <rectangle radius="15">
    <reportElement x="0" y="350" width="200" height="40" backcolor="#ffff00"/>
    <graphicElement>
      <pen lineWidth="2"/>
    </graphicElement>
  </rectangle>
If corners are not rounded, then the radius attribute is not necessary.

The ellipse shape

An ellipse is a closed bi-dimensional curved shape characterized by 2 semiaxis: the semimajor and the semiminor axis. When the semimajor axis equals the semiminor axis, the ellipse becomes a circle.
Any ellipse shape implements the JREllipse interface, which extends the JRGraphicElement.
No supplementary methods are needed, because both ellipse's semiaxis can be determined from the common element's width and height attributes.
In the .jrxml file an ellipse element is represented as follows:
  <ellipse>
    <reportElement x="0" y="600" width="200" height="40" forecolor="#ff0000" backcolor="#ffff99"/>
    <graphicElement>
      <pen lineWidth="2"/>
    </graphicElement>
  </ellipse>
Running the Sample

Running the sample requires the Apache Ant library. Make sure that ant is already installed on your system (version 1.5 or later).
In a command prompt/terminal window set the current folder to demo/samples/shapes within the JasperReports source project and run the > ant test view command.
It will generate all supported document types containing the sample report in the demo/samples/shapes/build/reports directory.
Then the report will open in the JasperReports internal viewer.



© 2001- Jaspersoft Corporation www.jaspersoft.com