Search in sources :

Example 31 with FontMetrics

use of java.awt.FontMetrics in project ACS by ACS-Community.

the class ColoredEditorSupport method paintValue.

/**
   * a static version of the paintValue method that can be called from other places
   * @param graphics the graphics on which to drow
   * @param rectangle the rectangle to fill
   * @param text the text to display
   */
public static void paintValue(Graphics g, Rectangle rectangle, String text, Color backGround, Color foreGround) {
    Color origColor = g.getColor();
    // change background color only if the parameter backGround is non-null
    if (backGround != null) {
        g.setColor(backGround);
    }
    g.fillRect(rectangle.x, rectangle.y, rectangle.width, rectangle.height);
    // draw the text
    int xOffset = 6;
    if (foreGround != null) {
        g.setColor(foreGround);
    } else {
        g.setColor(Color.black);
    }
    FontMetrics fm = g.getFontMetrics();
    g.drawString(text, rectangle.x + xOffset, rectangle.y + (rectangle.height - fm.getHeight()) / 2 + fm.getAscent());
    // restore color
    g.setColor(origColor);
}
Also used : FontMetrics(java.awt.FontMetrics) Color(java.awt.Color)

Example 32 with FontMetrics

use of java.awt.FontMetrics in project ACS by ACS-Community.

the class AlarmDetailTable method setTitleColumnSize.

/**
	 * Calculate the width of the first column to be at least wide 
	 * enough to contain the titles in {@link RowTitles}.
	 * <P>
	 * The width of the column is the greatest between the width needed
	 * to show the title or the passed width
	 * 
	 * @param sz A vector of string (can be <code>null</code>)
	 * @return The width of the first column
	 */
private int setTitleColumnSize(Vector<String> strings) {
    BufferedImage bImg = new BufferedImage(100, 100, BufferedImage.TYPE_INT_RGB);
    Graphics2D g2D = bImg.createGraphics();
    FontMetrics fm = g2D.getFontMetrics();
    int sz = 0;
    for (RowTitles row : RowTitles.values()) {
        if (sz < fm.stringWidth(row.title)) {
            sz = fm.stringWidth(row.title);
        }
    }
    if (strings != null) {
        for (String str : strings) {
            if (sz < fm.stringWidth(str)) {
                sz = fm.stringWidth(str);
            }
        }
    }
    sz += 20;
    TableColumn col = getColumnModel().getColumn(0);
    col.setPreferredWidth(sz);
    col.setMinWidth(sz);
    col.setMaxWidth(sz);
    col.setWidth(sz);
    col.setResizable(false);
    col = getColumnModel().getColumn(1);
    col.setResizable(true);
    return sz;
}
Also used : FontMetrics(java.awt.FontMetrics) TableColumn(javax.swing.table.TableColumn) BufferedImage(java.awt.image.BufferedImage) Graphics2D(java.awt.Graphics2D)

Example 33 with FontMetrics

use of java.awt.FontMetrics in project jdk8u_jdk by JetBrains.

the class TestSGEuseAlternateFontforJALocales method main.

public static void main(String[] args) throws Exception {
    System.out.println("Default Charset = " + Charset.defaultCharset().name());
    System.out.println("Locale = " + Locale.getDefault());
    String os = System.getProperty("os.name");
    String encoding = System.getProperty("file.encoding");
    /* Want to test the JA locale uses alternate font for DialogInput. */
    boolean jaTest = encoding.equalsIgnoreCase("windows-31j");
    if (!os.startsWith("Win") && jaTest) {
        System.out.println("Skipping Windows only test");
        return;
    }
    String className = "sun.java2d.SunGraphicsEnvironment";
    String methodName = "useAlternateFontforJALocales";
    Class sge = Class.forName(className);
    Method uafMethod = sge.getMethod(methodName, (Class[]) null);
    Object ret = uafMethod.invoke(null);
    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    ge.preferLocaleFonts();
    ge.preferProportionalFonts();
    if (jaTest) {
        Font msMincho = new Font("MS Mincho", Font.PLAIN, 12);
        if (!msMincho.getFamily(Locale.ENGLISH).equals("MS Mincho")) {
            System.out.println("MS Mincho not installed. Skipping test");
            return;
        }
        Font dialogInput = new Font("DialogInput", Font.PLAIN, 12);
        Font courierNew = new Font("Courier New", Font.PLAIN, 12);
        Font msGothic = new Font("MS Gothic", Font.PLAIN, 12);
        BufferedImage bi = new BufferedImage(1, 1, 1);
        Graphics2D g2d = bi.createGraphics();
        FontMetrics cnMetrics = g2d.getFontMetrics(courierNew);
        FontMetrics diMetrics = g2d.getFontMetrics(dialogInput);
        FontMetrics mmMetrics = g2d.getFontMetrics(msMincho);
        FontMetrics mgMetrics = g2d.getFontMetrics(msGothic);
        //  "preferLocaleFonts for Japanese
        if (cnMetrics.charWidth('A') == diMetrics.charWidth('A')) {
            throw new RuntimeException("Courier New should not be used for DialogInput");
        }
        // not definite proof.
        if (diMetrics.charWidth('A') != mmMetrics.charWidth('A')) {
            throw new RuntimeException("MS Mincho should be used for DialogInput");
        }
    }
}
Also used : FontMetrics(java.awt.FontMetrics) Method(java.lang.reflect.Method) GraphicsEnvironment(java.awt.GraphicsEnvironment) Font(java.awt.Font) BufferedImage(java.awt.image.BufferedImage) Graphics2D(java.awt.Graphics2D)

Example 34 with FontMetrics

use of java.awt.FontMetrics in project lwjgl by LWJGL.

the class AppletLoader method paint.

/*
	 * @see java.awt.Container#paint(java.awt.Graphics)
	 */
public void paint(Graphics g) {
    // don't paint loader if applet loaded
    if (state == STATE_DONE) {
        // clean up resources
        cleanUp();
        return;
    }
    // no drawing in headless mode
    if (headless)
        return;
    // create offscreen if missing
    if (offscreen == null) {
        offscreen = createImage(getWidth(), getHeight());
        // create buffers for animated gifs
        if (logo != null) {
            logoBuffer = createImage(logo.getWidth(null), logo.getHeight(null));
            // add image observer, it will notify when next animated gif frame is ready
            offscreen.getGraphics().drawImage(logo, 0, 0, this);
            // in case image is not animated fill image buffer once
            imageUpdate(logo, ImageObserver.FRAMEBITS, 0, 0, 0, 0);
        }
        if (progressbar != null) {
            progressbarBuffer = createImage(progressbar.getWidth(null), progressbar.getHeight(null));
            // add image observer, it will notify when next animated gif frame is ready
            offscreen.getGraphics().drawImage(progressbar, 0, 0, this);
            // in case image is not animated fill image buffer once
            imageUpdate(progressbar, ImageObserver.FRAMEBITS, 0, 0, 0, 0);
        }
    }
    // draw everything onto an image before drawing to avoid flicker
    Graphics og = offscreen.getGraphics();
    FontMetrics fm = og.getFontMetrics();
    // clear background color
    og.setColor(bgColor);
    og.fillRect(0, 0, offscreen.getWidth(null), offscreen.getHeight(null));
    og.setColor(fgColor);
    // if we had a failure of some sort, notify the user
    if (fatalError) {
        for (int i = 0; i < errorMessage.length; i++) {
            if (errorMessage[i] != null) {
                int messageX = (offscreen.getWidth(null) - fm.stringWidth(errorMessage[i])) / 2;
                int messageY = (offscreen.getHeight(null) - (fm.getHeight() * errorMessage.length)) / 2;
                og.drawString(errorMessage[i], messageX, messageY + i * fm.getHeight());
            }
        }
    } else {
        og.setColor(fgColor);
        painting = true;
        // get position at the middle of the offscreen buffer
        int x = offscreen.getWidth(null) / 2;
        int y = offscreen.getHeight(null) / 2;
        // draw logo
        if (logo != null) {
            og.drawImage(logoBuffer, x - logo.getWidth(null) / 2, y - logo.getHeight(null) / 2, this);
        }
        // draw message
        String message = getDescriptionForState();
        int messageX = (offscreen.getWidth(null) - fm.stringWidth(message)) / 2;
        int messageY = y + 20;
        if (logo != null)
            messageY += logo.getHeight(null) / 2;
        else if (progressbar != null)
            messageY += progressbar.getHeight(null) / 2;
        og.drawString(message, messageX, messageY);
        // draw subtaskmessage, if any
        if (subtaskMessage.length() > 0) {
            messageX = (offscreen.getWidth(null) - fm.stringWidth(subtaskMessage)) / 2;
            og.drawString(subtaskMessage, messageX, messageY + 20);
        }
        // draw loading progress bar, clipping it depending on percentage done
        if (progressbar != null) {
            int barSize = (progressbar.getWidth(null) * percentage) / 100;
            og.clipRect(x - progressbar.getWidth(null) / 2, 0, barSize, offscreen.getHeight(null));
            og.drawImage(progressbarBuffer, x - progressbar.getWidth(null) / 2, y - progressbar.getHeight(null) / 2, this);
        }
        painting = false;
    }
    og.dispose();
    // finally draw it all centred
    g.drawImage(offscreen, (getWidth() - offscreen.getWidth(null)) / 2, (getHeight() - offscreen.getHeight(null)) / 2, null);
}
Also used : Graphics(java.awt.Graphics) FontMetrics(java.awt.FontMetrics)

Example 35 with FontMetrics

use of java.awt.FontMetrics in project intellij-community by JetBrains.

the class RelativeFontTest method toMonospaced.

private static Font toMonospaced(Font font, boolean monospaced) {
    FontMetrics fm = new JLabel().getFontMetrics(font);
    assertEquals(monospaced, fm.charWidth('i') == fm.charWidth('m'));
    return MONOSPACED_FONT.derive(font);
}
Also used : FontMetrics(java.awt.FontMetrics) JLabel(javax.swing.JLabel)

Aggregations

FontMetrics (java.awt.FontMetrics)179 Font (java.awt.Font)61 Graphics2D (java.awt.Graphics2D)35 Point (java.awt.Point)34 Dimension (java.awt.Dimension)29 Color (java.awt.Color)23 Rectangle (java.awt.Rectangle)21 Rectangle2D (java.awt.geom.Rectangle2D)21 Insets (java.awt.Insets)20 Graphics (java.awt.Graphics)16 FilteredTreeModel (gov.sandia.n2a.ui.eq.FilteredTreeModel)14 GradientPaint (java.awt.GradientPaint)14 MPart (gov.sandia.n2a.eqset.MPart)13 NodeBase (gov.sandia.n2a.ui.eq.tree.NodeBase)12 Paint (java.awt.Paint)10 JTree (javax.swing.JTree)10 BasicStroke (java.awt.BasicStroke)9 BufferedImage (java.awt.image.BufferedImage)9 PanelModel (gov.sandia.n2a.ui.eq.PanelModel)8 JLabel (javax.swing.JLabel)8