Example usage for java.awt FontMetrics getLineMetrics

List of usage examples for java.awt FontMetrics getLineMetrics

Introduction

In this page you can find the example usage for java.awt FontMetrics getLineMetrics.

Prototype

public LineMetrics getLineMetrics(String str, Graphics context) 

Source Link

Document

Returns the LineMetrics object for the specified String in the specified Graphics context.

Usage

From source file:Main.java

/** Draw 'str' centered at 'p'. */
public static void drawCenteredText(Graphics g, Point p, String str) {
    FontMetrics fm = g.getFontMetrics();
    LineMetrics lm = fm.getLineMetrics(str, g);

    // Go to 'p', then add a/2 to get to the baseline.
    // I ignore the descent because it looks better to center without
    // regard to descenders.
    int baseY = p.y + (int) (lm.getAscent() / 2);
    int baseX = p.x - fm.stringWidth(str) / 2;

    g.drawString(str, baseX, baseY);//w ww. j  a v a  2s  .  c o m
}

From source file:org.squidy.designer.util.DrawableString.java

/**
 * @param g/*from   w ww  .  j a v  a2  s. com*/
 */
private void update(Graphics2D g, double viewScale) {
    Cache.Item item;

    // use screen if no bounds set
    if (bounds == null) {
        bounds = g.getDeviceConfiguration().getBounds();
    }

    // get item extents from cache or calculate
    if (cache == null || dirty) {
        cache = new Cache();
        item = null;
    } else {
        item = cache.get(viewScale);
    }

    if (item != null) {
        drawValue = item.drawValue;
        positionX = item.positionX;
    } else {
        FontMetrics fm;
        int width, height;

        // font width does not scale linear to view scale
        fm = g.getFontMetrics();
        drawValue = FontUtils.createCroppedLabelIfNecessary(fm, value, bounds.width - 50);

        positionX = bounds.x;
        if (alignmentH == AlignmentH.CENTER) {
            width = FontUtils.getWidthOfText(fm, drawValue);
            positionX += (bounds.width - width) / 2;
        } else if (alignmentH == AlignmentH.RIGHT) {
            width = FontUtils.getWidthOfText(fm, drawValue);
            positionX += (bounds.width - width);
        }

        // font height keeps the same all the time
        if (dirty) {
            positionY = bounds.y;
            if (alignmentV == AlignmentV.CENTER) {
                height = (int) fm.getLineMetrics(drawValue, g).getHeight();
                positionY += (bounds.height - height) / 2;
            } else if (alignmentV == AlignmentV.BOTTOM) {
                height = (int) fm.getLineMetrics(drawValue, g).getHeight();
                positionY += (bounds.height - height);
            }
        }

        // store extents in cache
        cache.put(drawValue, positionX, viewScale);
    }

    dirty = false;
}

From source file:ded.ui.DiagramController.java

/** The core of the paint routine, after we decide whether to interpose
  * another buffer. *///from   ww  w.  j av  a 2s .  co  m
private void innerPaint(Graphics g) {
    super.paint(g);

    // I do not know the proper way to get a font set automatically
    // in a Graphics object.  Calling JComponent.setFont has gotten
    // me nowhere.  Setting it myself when I first get control
    // seems to work; but note that I have to do this *after*
    // calling super.paint().
    g.setFont(this.dedWindow.diagramFont);

    // Filename label.
    if (this.diagram.drawFileName && !this.fileName.isEmpty()) {
        String name = new File(this.fileName).getName();
        FontMetrics fm = g.getFontMetrics();
        LineMetrics lm = fm.getLineMetrics(name, g);
        int x = fileNameLabelMargin;
        int y = fileNameLabelMargin + (int) lm.getAscent();
        g.drawString(name, x, y);
        y += (int) lm.getUnderlineOffset() + 1 /*...*/;
        g.drawLine(x, y, x + fm.stringWidth(name), y);
    }

    // Controllers.
    for (Controller c : this.controllers) {
        if (c.isSelected()) {
            c.paintSelectionBackground(g);
        }
        c.paint(g);
    }

    // Lasso rectangle.
    if (this.mode == Mode.DCM_RECT_LASSO) {
        Rectangle r = this.getLassoRect();
        g.drawRect(r.x, r.y, r.width, r.height);
    }

    // Current focused Component.
    if (debugFocus) {
        KeyboardFocusManager kfm = KeyboardFocusManager.getCurrentKeyboardFocusManager();
        Component fo = kfm.getFocusOwner();
        g.drawString("Focus: " + fo, 3, this.getHeight() - 22);
    }

    // Mode label.
    if (this.mode != Mode.DCM_SELECT) {
        g.drawString("Mode: " + this.mode.description, 3, this.getHeight() - 4);
    } else if (this.fpsMeasurementMode) {
        this.fpsFrameCount++;
        long current = System.currentTimeMillis();
        long millis = current - this.fpsStartMillis;
        if (millis > 1000) {
            // Update the FPS measurement with the results for this
            // interval.
            this.fpsSampleCount++;
            this.fpsMeasurement = "FPS: " + this.fpsFrameCount + " (millis=" + millis + ", samples="
                    + this.fpsSampleCount + ")";

            // Reset the counters.
            this.fpsStartMillis = current;
            this.fpsFrameCount = 0;
        }
        g.drawString(this.fpsMeasurement + " (Ctrl+G to stop)", 3, this.getHeight() - 4);
    }
}