Search in sources :

Example 46 with Rectangle2D

use of java.awt.geom.Rectangle2D in project poi by apache.

the class HSLFTable method setRowHeight.

@Override
public void setRowHeight(int row, final double height) {
    if (row < 0 || row >= cells.length) {
        throw new IllegalArgumentException("Row index '" + row + "' is not within range [0-" + (cells.length - 1) + "]");
    }
    // update row height in the table properties
    AbstractEscherOptRecord opt = getEscherChild(RecordTypes.EscherUserDefined.typeID);
    EscherArrayProperty p = opt.lookup(EscherProperties.GROUPSHAPE__TABLEROWPROPERTIES);
    byte[] masterBytes = p.getElement(row);
    double currentHeight = Units.masterToPoints(LittleEndian.getInt(masterBytes, 0));
    LittleEndian.putInt(masterBytes, 0, Units.pointsToMaster(height));
    p.setElement(row, masterBytes);
    // move the cells
    double dy = height - currentHeight;
    for (int i = row; i < cells.length; i++) {
        for (HSLFTableCell c : cells[i]) {
            if (c == null) {
                continue;
            }
            Rectangle2D anchor = c.getAnchor();
            if (i == row) {
                anchor.setRect(anchor.getX(), anchor.getY(), anchor.getWidth(), height);
            } else {
                anchor.setRect(anchor.getX(), anchor.getY() + dy, anchor.getWidth(), anchor.getHeight());
            }
            c.setAnchor(anchor);
        }
    }
    Rectangle2D tblanchor = getAnchor();
    tblanchor.setRect(tblanchor.getX(), tblanchor.getY(), tblanchor.getWidth(), tblanchor.getHeight() + dy);
    setExteriorAnchor(tblanchor);
}
Also used : EscherArrayProperty(org.apache.poi.ddf.EscherArrayProperty) Rectangle2D(java.awt.geom.Rectangle2D) AbstractEscherOptRecord(org.apache.poi.ddf.AbstractEscherOptRecord)

Example 47 with Rectangle2D

use of java.awt.geom.Rectangle2D in project poi by apache.

the class HSLFTableCell method anchorBorder.

private void anchorBorder(BorderEdge edge, final HSLFLine line) {
    if (line == null) {
        return;
    }
    Rectangle2D cellAnchor = getAnchor();
    double x, y, w, h;
    switch(edge) {
        case top:
            x = cellAnchor.getX();
            y = cellAnchor.getY();
            w = cellAnchor.getWidth();
            h = 0;
            break;
        case right:
            x = cellAnchor.getX() + cellAnchor.getWidth();
            y = cellAnchor.getY();
            w = 0;
            h = cellAnchor.getHeight();
            break;
        case bottom:
            x = cellAnchor.getX();
            y = cellAnchor.getY() + cellAnchor.getHeight();
            w = cellAnchor.getWidth();
            h = 0;
            break;
        case left:
            x = cellAnchor.getX();
            y = cellAnchor.getY();
            w = 0;
            h = cellAnchor.getHeight();
            break;
        default:
            throw new IllegalArgumentException();
    }
    line.setAnchor(new Rectangle2D.Double(x, y, w, h));
}
Also used : Rectangle2D(java.awt.geom.Rectangle2D)

Example 48 with Rectangle2D

use of java.awt.geom.Rectangle2D in project poi by apache.

the class HSLFTable method cellListToArray.

private void cellListToArray() {
    List<HSLFTableCell> htc = new ArrayList<HSLFTableCell>();
    for (HSLFShape h : getShapes()) {
        if (h instanceof HSLFTableCell) {
            htc.add((HSLFTableCell) h);
        }
    }
    if (htc.isEmpty()) {
        throw new IllegalStateException("HSLFTable without HSLFTableCells");
    }
    SortedSet<Double> colSet = new TreeSet<Double>();
    SortedSet<Double> rowSet = new TreeSet<Double>();
    // #1 pass - determine cols and rows
    for (HSLFTableCell sh : htc) {
        Rectangle2D anchor = sh.getAnchor();
        colSet.add(anchor.getX());
        rowSet.add(anchor.getY());
    }
    cells = new HSLFTableCell[rowSet.size()][colSet.size()];
    List<Double> colLst = new ArrayList<Double>(colSet);
    List<Double> rowLst = new ArrayList<Double>(rowSet);
    // #2 pass - assign shape to table cells
    for (HSLFTableCell sh : htc) {
        Rectangle2D anchor = sh.getAnchor();
        int row = rowLst.indexOf(anchor.getY());
        int col = colLst.indexOf(anchor.getX());
        assert (row != -1 && col != -1);
        cells[row][col] = sh;
        // determine gridSpan / rowSpan
        int gridSpan = calcSpan(colLst, anchor.getWidth(), col);
        int rowSpan = calcSpan(rowLst, anchor.getHeight(), row);
        sh.setGridSpan(gridSpan);
        sh.setRowSpan(rowSpan);
    }
}
Also used : TreeSet(java.util.TreeSet) ArrayList(java.util.ArrayList) Rectangle2D(java.awt.geom.Rectangle2D)

Example 49 with Rectangle2D

use of java.awt.geom.Rectangle2D in project poi by apache.

the class HSLFTextShape method resizeToFitText.

/**
     * Adjust the size of the shape so it encompasses the text inside it.
     *
     * @return a <code>Rectangle2D</code> that is the bounds of this shape.
     */
public Rectangle2D resizeToFitText() {
    Rectangle2D anchor = getAnchor();
    if (anchor.getWidth() == 0.) {
        LOG.log(POILogger.WARN, "Width of shape wasn't set. Defaulting to 200px");
        anchor.setRect(anchor.getX(), anchor.getY(), 200., anchor.getHeight());
        setAnchor(anchor);
    }
    double height = getTextHeight();
    // add a pixel to compensate rounding errors
    height += 1;
    anchor.setRect(anchor.getX(), anchor.getY(), anchor.getWidth(), height);
    setAnchor(anchor);
    return anchor;
}
Also used : Rectangle2D(java.awt.geom.Rectangle2D)

Example 50 with Rectangle2D

use of java.awt.geom.Rectangle2D in project poi by apache.

the class TestTable method moveTable.

@Test
public void moveTable() throws IOException {
    HSLFSlideShow ppt = new HSLFSlideShow();
    HSLFSlide slide = ppt.createSlide();
    int rows = 3, cols = 5;
    HSLFTable table = slide.createTable(rows, cols);
    for (int row = 0; row < rows; row++) {
        for (int col = 0; col < cols; col++) {
            HSLFTableCell c = table.getCell(row, col);
            c.setText("r" + row + "c" + col);
        }
    }
    new DrawTableShape(table).setAllBorders(1.0, Color.black, StrokeStyle.LineDash.DASH_DOT);
    table.setAnchor(new Rectangle2D.Double(100, 100, 400, 400));
    Rectangle2D rectExp = new Rectangle2D.Double(420, 366.625, 80, 133.375);
    Rectangle2D rectAct = table.getCell(rows - 1, cols - 1).getAnchor();
    assertEquals(rectExp, rectAct);
    ppt.close();
}
Also used : Rectangle2D(java.awt.geom.Rectangle2D) DrawTableShape(org.apache.poi.sl.draw.DrawTableShape) Test(org.junit.Test)

Aggregations

Rectangle2D (java.awt.geom.Rectangle2D)262 Graphics2D (java.awt.Graphics2D)42 AffineTransform (java.awt.geom.AffineTransform)34 Color (java.awt.Color)33 Paint (java.awt.Paint)28 Point2D (java.awt.geom.Point2D)26 BufferedImage (java.awt.image.BufferedImage)23 Font (java.awt.Font)21 FontMetrics (java.awt.FontMetrics)20 Dimension (java.awt.Dimension)19 RoundRectangle2D (java.awt.geom.RoundRectangle2D)18 Rectangle (java.awt.Rectangle)16 FontRenderContext (java.awt.font.FontRenderContext)15 Point (java.awt.Point)11 BasicStroke (java.awt.BasicStroke)10 ArrayList (java.util.ArrayList)10 GradientPaint (java.awt.GradientPaint)9 TextLayout (java.awt.font.TextLayout)9 RadialGradientPaint (java.awt.RadialGradientPaint)8 Shape (java.awt.Shape)7