Example usage for java.awt.geom Rectangle2D getWidth

List of usage examples for java.awt.geom Rectangle2D getWidth

Introduction

In this page you can find the example usage for java.awt.geom Rectangle2D getWidth.

Prototype

public abstract double getWidth();

Source Link

Document

Returns the width of the framing rectangle in double precision.

Usage

From source file:net.sf.mzmine.chartbasics.ChartLogics.java

/**
 * calculates the correct height with multiple iterations Domain and Range axes need to share the
 * same unit (e.g. mm)/*from w  w  w.  j  av  a2 s  . c o  m*/
 * 
 * @param myChart
 * @param copyToNewPanel
 * @param dataWidth width of data
 * @param axis for width calculation
 * @return
 */
public static double calcHeightToWidth(ChartPanel myChart, double chartWidth, double estimatedHeight,
        int iterations, boolean copyToNewPanel) {
    // if(myChart.getChartRenderingInfo()==null ||
    // myChart.getChartRenderingInfo().getChartArea()==null ||
    // myChart.getChartRenderingInfo().getChartArea().getWidth()==0)
    // result
    double height = estimatedHeight;
    double lastH = height;

    makeChartResizable(myChart);

    // paint on a ghost panel
    JPanel parent = (JPanel) myChart.getParent();
    JPanel p = copyToNewPanel ? new JPanel() : parent;
    if (copyToNewPanel)
        p.add(myChart, BorderLayout.CENTER);
    try {
        for (int i = 0; i < iterations; i++) {
            // paint on ghost panel with estimated height (if copy panel==true)
            myChart.setSize((int) chartWidth, (int) estimatedHeight);
            myChart.paintImmediately(myChart.getBounds());

            XYPlot plot = (XYPlot) myChart.getChart().getPlot();
            ChartRenderingInfo info = myChart.getChartRenderingInfo();
            Rectangle2D dataArea = info.getPlotInfo().getDataArea();
            Rectangle2D chartArea = info.getChartArea();

            // calc title space: will be added later to the right plot size
            double titleWidth = chartArea.getWidth() - dataArea.getWidth();
            double titleHeight = chartArea.getHeight() - dataArea.getHeight();

            // calc right plot size with axis dim.
            // real plot width is given by factor;
            double realPW = chartWidth - titleWidth;

            // ranges
            ValueAxis domainAxis = plot.getDomainAxis();
            org.jfree.data.Range x = domainAxis.getRange();
            ValueAxis rangeAxis = plot.getRangeAxis();
            org.jfree.data.Range y = rangeAxis.getRange();

            // real plot height can be calculated by
            double realPH = realPW / x.getLength() * y.getLength();

            // the real height
            height = realPH + titleHeight;

            // for next iteration
            estimatedHeight = height;
            if ((int) lastH == (int) height)
                break;
            else
                lastH = height;
        }
    } catch (Exception ex) {
        ex.printStackTrace();
    }

    if (copyToNewPanel) {
        // reset to frame
        p.removeAll();
        parent.add(myChart);
    }

    return height;
}

From source file:HorizontallyCenteredText.java

protected void paintHorizontallyCenteredText(Graphics2D g2, String s, float centerX, float baselineY) {
    FontRenderContext frc = g2.getFontRenderContext();
    Rectangle2D bounds = g2.getFont().getStringBounds(s, frc);
    float width = (float) bounds.getWidth();
    g2.drawString(s, centerX - width / 2, baselineY);
}

From source file:com.t_oster.visicut.misc.Helper.java

/**
 * Returns a rectangle (parralel to x and y axis), which contains
 * the given rectangle after the given transform. If the transform
 * contains a rotation, the resulting rectangle is the smallest bounding-box
 * @param src/*from w ww.  ja  v  a2s  .  c o m*/
 * @param at
 * @return
 */
public static Rectangle2D transform(Rectangle2D src, AffineTransform at) {
    if (at == null) {
        return src;
    } else {
        java.awt.Point.Double[] points = new java.awt.Point.Double[4];
        points[0] = new java.awt.Point.Double(src.getX(), src.getY());
        points[1] = new java.awt.Point.Double(src.getX(), src.getY() + src.getHeight());
        points[2] = new java.awt.Point.Double(src.getX() + src.getWidth(), src.getY());
        points[3] = new java.awt.Point.Double(src.getX() + src.getWidth(), src.getY() + src.getHeight());
        for (int i = 0; i < 4; i++) {
            at.transform(points[i], points[i]);
        }
        return smallestBoundingBox(points);
    }
}

From source file:MainClass.java

public void paint(Graphics g) {
    Graphics2D g2 = (Graphics2D) g;

    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

    g2.setFont(new Font("Serif", Font.PLAIN, 48));

    FontRenderContext frc = g2.getFontRenderContext();
    String s = "www.java2s.com";
    Rectangle2D bounds = g2.getFont().getStringBounds(s, frc);
    float width = (float) bounds.getWidth();
    int centerX = 100;
    int baselineY = 70;
    g2.drawString(s, centerX - width / 2, baselineY);
}

From source file:net.sf.jasperreports.customizers.shape.LineDotShapeCustomizer.java

@Override
protected Point getOffset(Rectangle2D bounds) {
    return new Point((int) (bounds.getWidth() / 2 + bounds.getX()),
            (int) (bounds.getHeight() / 2 + bounds.getY()));
}

From source file:ImageLabel.java

public void paint(Graphics g) {

    /*//from   www . ja  v a  2  s  .c om
     * Draw the image stretched to exactly cover the size of the drawing area.
     */
    Dimension size = getSize();
    g.drawImage(img, 0, 0, size.width, size.height, 0, 0, img.getWidth(null), img.getHeight(null), null);

    /*
     * Fill a rounded rectangle centered in the drawing area. Calculate the size
     * of the rectangle from the size of the text
     */
    g.setFont(font);
    FontRenderContext frc = ((Graphics2D) g).getFontRenderContext();
    Rectangle2D bounds = font.getStringBounds(text, frc);

    int wText = (int) bounds.getWidth();
    int hText = (int) bounds.getHeight();

    int rX = (size.width - wText) / 2;
    int rY = (size.height - hText) / 2;
    g.setColor(Color.yellow);
    g.fillRoundRect(rX, rY, wText, hText, hText / 2, hText / 2);

    /*
     * Draw text positioned in the rectangle. Since the rectangle is sized based
     * on the bounds of the String we can position it using those bounds.
     */
    int xText = rX - (int) bounds.getX();
    int yText = rY - (int) bounds.getY();
    g.setColor(Color.black);
    g.setFont(font);
    g.drawString(text, xText, yText);
}

From source file:ShowOff.java

protected void drawText(Graphics2D g2) {
    FontRenderContext frc = g2.getFontRenderContext();
    mLayout = new TextLayout(mMessage, mFont, frc);
    int width = getSize().width;
    int height = getSize().height;
    Rectangle2D bounds = mLayout.getBounds();
    double x = (width - bounds.getWidth()) / 2;
    double y = height - bounds.getHeight();
    drawString(g2, x, y, 0);/*from   w w w  .j av a  2  s  .  c  o  m*/
    drawString(g2, width - bounds.getHeight(), y, -Math.PI / 2);
}

From source file:org.apache.xmlgraphics.ps.PSImageUtils.java

/**
 * Places an EPS file in the PostScript stream.
 * @param in the InputStream that contains the EPS stream
 * @param name name for the EPS document
 * @param viewport the viewport in points in which to place the EPS
 * @param bbox the EPS bounding box in points
 * @param gen the PS generator/*from  ww  w . j a va 2s .co m*/
 * @throws IOException in case an I/O error happens during output
 */
public static void renderEPS(InputStream in, String name, Rectangle2D viewport, Rectangle2D bbox,
        PSGenerator gen) throws IOException {
    gen.getResourceTracker().notifyResourceUsageOnPage(PSProcSets.EPS_PROCSET);
    gen.writeln("%AXGBeginEPS: " + name);
    gen.writeln("BeginEPSF");

    gen.writeln(gen.formatDouble(viewport.getX()) + " " + gen.formatDouble(viewport.getY()) + " translate");
    gen.writeln("0 " + gen.formatDouble(viewport.getHeight()) + " translate");
    gen.writeln("1 -1 scale");
    double sx = viewport.getWidth() / bbox.getWidth();
    double sy = viewport.getHeight() / bbox.getHeight();
    if (sx != 1 || sy != 1) {
        gen.writeln(gen.formatDouble(sx) + " " + gen.formatDouble(sy) + " scale");
    }
    if (bbox.getX() != 0 || bbox.getY() != 0) {
        gen.writeln(gen.formatDouble(-bbox.getX()) + " " + gen.formatDouble(-bbox.getY()) + " translate");
    }
    gen.writeln(gen.formatDouble(bbox.getX()) + " " + gen.formatDouble(bbox.getY()) + " "
            + gen.formatDouble(bbox.getWidth()) + " " + gen.formatDouble(bbox.getHeight()) + " re clip");
    gen.writeln("newpath");

    PSResource res = new PSResource(PSResource.TYPE_FILE, name);
    gen.getResourceTracker().registerSuppliedResource(res);
    gen.getResourceTracker().notifyResourceUsageOnPage(res);
    gen.writeDSCComment(DSCConstants.BEGIN_DOCUMENT, res.getName());
    IOUtils.copy(in, gen.getOutputStream());
    gen.newLine();
    gen.writeDSCComment(DSCConstants.END_DOCUMENT);
    gen.writeln("EndEPSF");
    gen.writeln("%AXGEndEPS");
}

From source file:edu.ucla.stat.SOCR.chart.gui.CircleDrawer.java

/**
 * Draws the circle./*from w  w  w .  ja  va2  s  . c o  m*/
 * 
 * @param g2  the graphics device.
 * @param area  the area in which to draw.
 */
public void draw(Graphics2D g2, Rectangle2D area) {
    Ellipse2D ellipse = new Ellipse2D.Double(area.getX(), area.getY(), area.getWidth(), area.getHeight());
    if (this.fillPaint != null) {
        g2.setPaint(this.fillPaint);
        g2.fill(ellipse);
    }
    if (this.outlinePaint != null && this.outlineStroke != null) {
        g2.setPaint(this.outlinePaint);
        g2.setStroke(this.outlineStroke);
        g2.draw(ellipse);
    }

    g2.setPaint(Color.black);
    g2.setStroke(new BasicStroke(1.0f));
    Line2D line1 = new Line2D.Double(area.getCenterX(), area.getMinY(), area.getCenterX(), area.getMaxY());
    Line2D line2 = new Line2D.Double(area.getMinX(), area.getCenterY(), area.getMaxX(), area.getCenterY());
    g2.draw(line1);
    g2.draw(line2);
}

From source file:FontTest.java

public void paintComponent(Graphics g) {
    Graphics2D g2 = (Graphics2D) g;

    String message = "Hello, World!";

    Font f = new Font("Serif", Font.BOLD, 36);
    g2.setFont(f);/*  w w  w  .  j av a 2 s . c  o m*/

    // measure the size of the message

    FontRenderContext context = g2.getFontRenderContext();
    Rectangle2D bounds = f.getStringBounds(message, context);

    // set (x,y) = top left corner of text

    double x = (getWidth() - bounds.getWidth()) / 2;
    double y = (getHeight() - bounds.getHeight()) / 2;

    // add ascent to y to reach the baseline

    double ascent = -bounds.getY();
    double baseY = y + ascent;

    // draw the message

    g2.drawString(message, (int) x, (int) baseY);

    g2.setPaint(Color.LIGHT_GRAY);

    // draw the baseline

    g2.draw(new Line2D.Double(x, baseY, x + bounds.getWidth(), baseY));

    // draw the enclosing rectangle

    Rectangle2D rect = new Rectangle2D.Double(x, y, bounds.getWidth(), bounds.getHeight());
    g2.draw(rect);
}