Search in sources :

Example 26 with FontMetrics

use of java.awt.FontMetrics in project gephi by gephi.

the class TickGraph method drawReal.

private void drawReal(Graphics2D g) {
    int width = parameters.getWidth();
    int height = parameters.getHeight();
    //Font
    Font font = parameters.getFont();
    FontMetrics fontMetrics = null;
    double factor = parameters.getFontFactor();
    int fontSize = Math.min(parameters.getFontSize(), (int) (height / factor));
    fontSize = fontSize > parameters.getFontSize() / (factor * 2) && fontSize <= parameters.getFontSize() / (factor / 4) ? (int) (parameters.getFontSize() / (factor / 4)) : fontSize;
    if (font != null && fontSize > parameters.getFontSize() / (factor / 2)) {
        font = font.deriveFont(Font.PLAIN, fontSize);
        fontMetrics = g.getFontMetrics(font);
        g.setFont(font);
    } else {
        font = null;
    }
    //50
    //        int fifty = (int) ((50 - min) * (width / (max - min)));
    //        g.setColor(Color.BLUE);
    //        g.drawLine(fifty, 0, fifty, height);
    RealTick graduation = RealTick.create(min, max, width);
    int numberTicks = graduation.getNumberTicks();
    for (int i = 0; i <= numberTicks; i++) {
        int x = graduation.getTickPixelPosition(i, width);
        int rank = graduation.getTickRank(i);
        int h = Math.min(40, (int) (height / 15.0));
        h = rank == 2 ? (int) (h + h) : rank == 1 ? (int) (h + h / 2.) : h;
        if (x > 0) {
            g.setColor(parameters.getRealColor(rank));
            g.drawLine(x, 0, x, h);
            if (font != null && rank >= 1) {
                String label = graduation.getTickValue(i);
                int xLabel = x - (fontMetrics.stringWidth(label) / 2);
                g.drawString(label, xLabel, (int) (h + fontSize * 1.2));
            }
        }
    }
}
Also used : FontMetrics(java.awt.FontMetrics) Font(java.awt.Font)

Example 27 with FontMetrics

use of java.awt.FontMetrics in project gephi by gephi.

the class TickGraph method drawStartEnd.

private void drawStartEnd(Graphics2D g) {
    int width = parameters.getWidth();
    int height = parameters.getHeight();
    //Font
    Font font = parameters.getFont();
    FontMetrics fontMetrics = null;
    int fontSize = Math.min(parameters.getFontSize(), (int) (height / 4.));
    fontSize = fontSize > parameters.getFontSize() / 4 && fontSize <= parameters.getFontSize() / 2 ? parameters.getFontSize() / 2 : fontSize;
    if (font != null && fontSize > parameters.getFontSize() / 4) {
        font = font.deriveFont(Font.PLAIN, fontSize);
        fontMetrics = g.getFontMetrics(font);
        g.setFont(font);
    } else {
        font = null;
    }
    if (font != null) {
        g.setColor(parameters.getRealColor(2));
        StartEndTick startEnd = StartEndTick.create(min, max);
        String labelStart = startEnd.getStartValue();
        String labelEnd = startEnd.getEndValue();
        int xEnd = width - (fontMetrics.stringWidth(labelEnd)) - (int) (fontSize * 0.3);
        g.drawString(labelStart, (int) (fontSize * 0.3), (int) (fontSize * 1.2));
        g.drawString(labelEnd, xEnd, (int) (fontSize * 1.2));
    }
}
Also used : FontMetrics(java.awt.FontMetrics) Font(java.awt.Font)

Example 28 with FontMetrics

use of java.awt.FontMetrics in project binnavi by google.

the class FormattedCharacterBuffer method paintBuffer.

/**
   * Paints the current buffer onto a given graphics context, skipping the first N character
   * columns.
   */
protected void paintBuffer(final Graphics2D context, int x, int y, int skipColumns) {
    context.setFont(perCharFonts[0]);
    FontMetrics fontMetrics = context.getFontMetrics();
    // Clear the context with the default background color.
    context.fillRect(0, 0, numberOfColumnsInBuffer * fontMetrics.charWidth('a'), numberOfLinesInBuffer * fontMetrics.getHeight());
    for (int i = 0; i < numberOfLinesInBuffer; i++) {
        AttributedString stringToDraw = getAttributedStringForLine(i, skipColumns);
        // the +1 for the line index is necessary to avoid cutting off first line.
        context.drawString(stringToDraw.getIterator(), x, y + fontMetrics.getHeight() * (i + 1));
    }
}
Also used : AttributedString(java.text.AttributedString) FontMetrics(java.awt.FontMetrics)

Example 29 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 30 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)

Aggregations

FontMetrics (java.awt.FontMetrics)116 Font (java.awt.Font)43 Graphics2D (java.awt.Graphics2D)32 Point (java.awt.Point)29 Dimension (java.awt.Dimension)21 Rectangle2D (java.awt.geom.Rectangle2D)20 Color (java.awt.Color)18 Insets (java.awt.Insets)18 Rectangle (java.awt.Rectangle)17 GradientPaint (java.awt.GradientPaint)9 BufferedImage (java.awt.image.BufferedImage)9 Graphics (java.awt.Graphics)8 BasicStroke (java.awt.BasicStroke)6 Shape (java.awt.Shape)5 JLabel (javax.swing.JLabel)5 Canvas (java.awt.Canvas)4 BadLocationException (javax.swing.text.BadLocationException)4 Polygon (java.awt.Polygon)3 FontRenderContext (java.awt.font.FontRenderContext)3 ArrayList (java.util.ArrayList)3