Example usage for java.awt FontMetrics stringWidth

List of usage examples for java.awt FontMetrics stringWidth

Introduction

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

Prototype

public int stringWidth(String str) 

Source Link

Document

Returns the total advance width for showing the specified String in this Font .

Usage

From source file:metrics.java

License:asdf

public void paint(Graphics g) {
    g.translate(100, 100);/*w w  w  . j a  va  2  s .  c  o  m*/
    FontMetrics fm = null;
    int ascent, descent, leading, width1, width2, height;
    String string1 = "dsdas";
    String string2 = "asdf";
    int xPos = 25, yPos = 50;
    fm = g.getFontMetrics();
    ascent = fm.getAscent();
    descent = fm.getDescent();
    leading = fm.getLeading();
    height = fm.getHeight();
    width1 = fm.stringWidth(string1);
    width2 = fm.stringWidth(string2);
    g.drawString(string1, xPos, yPos);
    g.drawLine(xPos, yPos - ascent - leading, xPos + width1, yPos - ascent - leading);
    g.drawLine(xPos, yPos - ascent, xPos + width1, yPos - ascent);
    g.drawLine(xPos, yPos, xPos + width1, yPos);
    g.drawLine(xPos, yPos + descent, xPos + width1, yPos + descent);
    g.drawString(string2, xPos, yPos + height);
    g.drawLine(xPos, yPos - ascent - leading + height, xPos + width2, yPos - ascent - leading + height);
    g.drawLine(xPos, yPos - ascent + height, xPos + width2, yPos - ascent + height);
    g.drawLine(xPos, yPos + height, xPos + width2, yPos + height);
    g.drawLine(xPos, yPos + descent + height, xPos + width2, yPos + descent + height);

}

From source file:main.MapKit.java

@Override
public void paintWaypoint(Graphics2D g, JXMapViewer viewer, MyWaypoint w) {
    g = (Graphics2D) g.create();//  w w w  . j  a  v a2s.c  om

    Point2D point = viewer.getTileFactory().geoToPixel(w.getPosition(), viewer.getZoom());
    int x = (int) point.getX();
    int y = (int) point.getY();

    if (w.amount == -1) { //means it's an original data point
        int fontSize = 28 - viewer.getZoom() * 2; //font size is larger when zoom in
        if (fontSize < 6)
            fontSize = 6;
        g.setFont(new Font("Arial", Font.PLAIN, fontSize));
        g.setColor(w.color);
        g.drawString("x", x - fontSize / 2, y + fontSize / 2);
        g.dispose();
        return;
    }

    if (origImage == null) {
        return;
    }

    BufferedImage myImg = map.get(w.color);

    if (myImg == null) {
        myImg = convert(origImage, w.color);
        map.put(w.color, myImg);
    }

    g.drawImage(myImg, x - myImg.getWidth() / 2, y - myImg.getHeight(), null);

    String label = String.valueOf(w.amount);

    //      g.setFont(font);
    FontMetrics metrics = g.getFontMetrics();
    int tw = metrics.stringWidth(label);
    int th = 1 + metrics.getAscent();

    //      g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    g.drawString(label, x - tw / 2, y + th);

    g.dispose();
}

From source file:edu.ku.brc.ui.GradiantButton.java

@Override
public Dimension getPreferredSize() {
    String text = getText();/*from ww w.  j a  v  a 2 s  . c  om*/
    FontMetrics fm = this.getFontMetrics(getFont());
    float scale = (50f / 40f) * this.getFont().getSize2D();
    int w = fm.stringWidth(text);
    w += (int) (scale * 1.4f);
    int h = fm.getHeight();
    h += (int) (scale * .3f);

    // Make sure the height is at least as big as the icon
    if (icon != null) {
        if (h <= icon.getIconHeight()) {
            h = icon.getIconHeight() + 4;
        }
        if (w <= icon.getIconWidth()) {
            w = icon.getIconWidth() + 2;
        }
    }
    return new Dimension(w, h);
}

From source file:Main.java

public void paint(Graphics g) {
    g.translate(100, 100);//from w  w w .  j  a  va  2 s .  com
    FontMetrics fm = null;
    int ascent, descent, leading, width1, width2, height;
    String string1 = "www.java2s.com";
    String string2 = "java2s.com";
    int xPos = 25, yPos = 50;
    fm = g.getFontMetrics();
    ascent = fm.getAscent();
    descent = fm.getDescent();
    leading = fm.getLeading();
    height = fm.getHeight();
    width1 = fm.stringWidth(string1);
    width2 = fm.stringWidth(string2);
    g.drawString(string1, xPos, yPos);
    g.drawLine(xPos, yPos - ascent - leading, xPos + width1, yPos - ascent - leading);
    g.drawLine(xPos, yPos - ascent, xPos + width1, yPos - ascent);
    g.drawLine(xPos, yPos, xPos + width1, yPos);
    g.drawLine(xPos, yPos + descent, xPos + width1, yPos + descent);
    g.drawString(string2, xPos, yPos + height);
    g.drawLine(xPos, yPos - ascent - leading + height, xPos + width2, yPos - ascent - leading + height);
    g.drawLine(xPos, yPos - ascent + height, xPos + width2, yPos - ascent + height);
    g.drawLine(xPos, yPos + height, xPos + width2, yPos + height);
    g.drawLine(xPos, yPos + descent + height, xPos + width2, yPos + descent + height);

}

From source file:org.evors.rs.ui.sandpit.TrialViewer.java

public void drawText(Graphics2D g2, String text) {
    FontMetrics fm = g2.getFontMetrics();
    List<String> strings = Splitter.on("\n").splitToList(text);
    float x1 = 40, y = 40;
    int dx = 0;//from   www .ja  v a2  s.  com

    for (String s : strings) {
        int width = fm.stringWidth(s);
        if (width > dx) {
            dx = width;
        }
    }
    g2.setColor(new Color(0.6f, 0.6f, 0.6f, 0.6f));
    g2.fillRect((int) x1, (int) y - fm.getHeight(), dx + 5, fm.getHeight() * strings.size() + 5);
    g2.setColor(Color.red);
    for (String s : strings) {
        g2.drawString(s, 40, y);
        y += fm.getHeight();
    }

}

From source file:org.openmicroscopy.shoola.agents.util.ui.ChannelButton.java

/**
 * Returns the preferred dimension of the component.
 *
 * @param dimWidth The width of the component.
 *//*ww w .ja  v a2 s  .  c om*/
private Dimension setComponentSize(int dimWidth) {
    Font f = getFont();
    String text = getText();
    FontMetrics fm = getFontMetrics(f);
    int width = fm.stringWidth(text);
    Dimension d = DEFAULT_MIN_SIZE;
    if (width > DEFAULT_MIN_SIZE.width && width <= dimWidth) {
        d = new Dimension(width + 10, DEFAULT_MIN_SIZE.height);
    } else if (width > dimWidth) {
        int size = fm.stringWidth(UIUtilities.DOTS);
        width += size;
        String s = "";
        int n = text.length() - 1;
        List l = new ArrayList();
        char ch;
        while (fm.stringWidth(s) + size < dimWidth - size) {
            ch = text.charAt(n);
            s += ch;
            l.add(ch);
            n = n - 1;
        }
        Collections.reverse(l);
        Iterator i = l.iterator();
        s = UIUtilities.DOTS;
        while (i.hasNext())
            s += i.next();
        super.setText(s);
        //reset text
        width = fm.stringWidth(s);
        d = new Dimension(width + 10, DEFAULT_MIN_SIZE.height);
    }
    return d;
}

From source file:de.codesourcery.eve.skills.ui.utils.ResizingTextField.java

protected Dimension calcSize(String text) {
    final FontMetrics fm = this.getFontMetrics(this.getFont());
    final int height = fm.getHeight();

    final int colCount = Math.max(getColumns(), 1);
    final String realText = StringUtils.isBlank(text) ? StringUtils.leftPad("X", colCount, " ")
            : " " + text + " ";

    int width = (int) (1.2f * fm.stringWidth(realText));
    return new Dimension(width, height + 6);
}

From source file:net.pms.util.GenericIcons.java

/**
 * Add the format(container) name of the media to the generic icon image.
 *
 * @param image BufferdImage to be the label added
 * @param label the media container name to be added as a label
 * @param renderer the renderer configuration
 *
 * @return the generic icon with the container label added and scaled in accordance with renderer setting
 *///ww  w .  j a  v a2s. c  o  m
private DLNAThumbnail addFormatLabelToImage(String label, ImageFormat imageFormat, IconType iconType)
        throws IOException {

    BufferedImage image;
    switch (iconType) {
    case AUDIO:
        image = genericAudioIcon;
        break;
    case IMAGE:
        image = genericImageIcon;
        break;
    case VIDEO:
        image = genericVideoIcon;
        break;
    default:
        image = genericUnknownIcon;
    }

    if (image != null) {
        // Make a copy
        ColorModel colorModel = image.getColorModel();
        image = new BufferedImage(colorModel, image.copyData(null), colorModel.isAlphaPremultiplied(), null);
    }

    ByteArrayOutputStream out = null;

    if (label != null && image != null) {
        out = new ByteArrayOutputStream();
        Graphics2D g = image.createGraphics();
        g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
        g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);

        try {
            int size = 40;
            Font font = new Font(Font.SANS_SERIF, Font.BOLD, size);
            FontMetrics metrics = g.getFontMetrics(font);
            while (size > 7 && metrics.stringWidth(label) > 135) {
                size--;
                font = new Font(Font.SANS_SERIF, Font.BOLD, size);
                metrics = g.getFontMetrics(font);
            }
            // Text center point 127x, 49y - calculate centering coordinates
            int x = 127 - metrics.stringWidth(label) / 2;
            int y = 46 + metrics.getAscent() / 2;
            g.drawImage(image, 0, 0, null);
            g.setColor(Color.WHITE);
            g.setFont(font);
            g.drawString(label, x, y);

            ImageIO.setUseCache(false);
            ImageIOTools.imageIOWrite(image, imageFormat.toString(), out);
        } finally {
            g.dispose();
        }
    }
    return out != null ? DLNAThumbnail.toThumbnail(out.toByteArray(), 0, 0, ScaleType.MAX, imageFormat, false)
            : null;
}

From source file:org.mskcc.cbio.portal.util.MakeOncoPrint.java

/**
 * Computes the longest (in pixels) gene, % altered string pairing.
 *
 * @param matrix[][] GeneticEvent//w ww  .  j  ava2s.c  o m
 * @param dataSummary ProfileDataSummary
 *
 * @return String
 */
private static String getLongestLabel(GeneticEvent matrix[][], ProfileDataSummary dataSummary) {

    int maxWidth = 0;
    String longestLabel = null;
    FontMetrics fontMetrics = getFontMetrics();
    for (int lc = 0; lc < matrix.length; lc++) {
        GeneticEvent rowEvent = matrix[lc][0];
        String gene = rowEvent.getGene().toUpperCase();
        String alterationValue = alterationValueToString(
                dataSummary.getPercentCasesWhereGeneIsAltered(rowEvent.getGene()));
        String label = gene + alterationValue;
        int width = fontMetrics.stringWidth(label);
        if (width > maxWidth) {
            maxWidth = width;
            longestLabel = label;
        }
    }

    // outta here
    return longestLabel;
}

From source file:uk.co.real_logic.aeron.tools.perf_tools.AeronLatencyUnderLoadPublisher.java

private void plotSubset(final Graphics2D g, final int start, final int end, final String title,
        final double startX, final double width, final double stepY, final double mean) {
    final FontMetrics fm = g.getFontMetrics();
    final Color color = g.getColor();
    g.setColor(Color.black);/*w w  w.j  av a 2s  .  co  m*/
    g.drawString(title, (int) (startX + width / 2 - fm.stringWidth(title) / 2), 975);
    final String tmp = String.format("Mean: %.3fus", mean);
    g.drawString(tmp, (int) (startX + width / 2 - fm.stringWidth(tmp) / 2), 990);
    g.setColor(color);
    final double stepX = width / (end - start);
    for (int i = start; i < end; i++) {
        final int posX = (int) (startX + stepX * (i - start));
        final int posY = 960 - ((int) (stepY * (timestamps[i] / 1000.0)) + 1);
        g.drawLine(posX, posY, posX, 960);
    }
}