Example usage for java.awt Graphics2D getFont

List of usage examples for java.awt Graphics2D getFont

Introduction

In this page you can find the example usage for java.awt Graphics2D getFont.

Prototype

public abstract Font getFont();

Source Link

Document

Gets the current font.

Usage

From source file:Main.java

public static void drawCenteredShadowText(Graphics2D g2, String s, int x, int y, Color shadowColor) {
    FontRenderContext frc = g2.getFontRenderContext();
    Rectangle2D bounds = g2.getFont().getStringBounds(s, frc);
    int leftX = (int) (x - (float) bounds.getWidth() / 2);
    drawShadowText(g2, s, leftX, y, shadowColor, 1);
}

From source file:GraphicsUtil.java

public static void drawString(Graphics g, String text, RectangularShape bounds, Align align, double angle) {
    Graphics2D g2 = (Graphics2D) g;
    Font font = g2.getFont();
    if (angle != 0)
        g2.setFont(font.deriveFont(AffineTransform.getRotateInstance(Math.toRadians(angle))));

    Rectangle2D sSize = g2.getFontMetrics().getStringBounds(text, g2);
    Point2D pos = getPoint(bounds, align);
    double x = pos.getX();
    double y = pos.getY() + sSize.getHeight();

    switch (align) {
    case North:/* ww w  .ja v  a  2 s .c  o  m*/
    case South:
    case Center:
        x -= (sSize.getWidth() / 2);
        break;
    case NorthEast:
    case East:
    case SouthEast:
        x -= (sSize.getWidth());
        break;
    case SouthWest:
    case West:
    case NorthWest:
        break;
    }

    g2.drawString(text, (float) x, (float) y);
    g2.setFont(font);
}

From source file:org.ut.biolab.medsavant.client.util.ClientMiscUtils.java

/**
 * Draws a string centred in the given box.
 *//* w  ww  .ja  va  2 s .  c  o  m*/
public static void drawCentred(Graphics2D g2, String message, Rectangle2D box) {
    FontMetrics metrics = g2.getFontMetrics();
    Rectangle2D stringBounds = g2.getFont().getStringBounds(message, g2.getFontRenderContext());
    float x = (float) (box.getX() + (box.getWidth() - stringBounds.getWidth()) / 2.0);
    float y = (float) (box.getY() + (box.getHeight() + metrics.getAscent() - metrics.getDescent()) / 2.0);

    g2.drawString(message, x, y);
}

From source file:org.gitools.ui.app.heatmap.drawer.AbstractHeatmapDrawer.java

/**
 * Automatically change the font size to fit in the cell height.
 *
 * @param g           the Graphics2D object
 * @param cellHeight  the cell height//  www  . j  av  a 2 s.  co m
 * @param minFontSize the min font size
 * @return Returns true if the new font size fits in the cell height, false if it doesn't fit.
 */
protected static boolean calculateFontSize(Graphics2D g, int cellHeight, int minFontSize) {

    float fontHeight = g.getFontMetrics().getHeight();

    float fontSize = g.getFont().getSize();
    while (fontHeight > (cellHeight - 2) && fontSize > minFontSize) {
        fontSize--;
        g.setFont(g.getFont().deriveFont(fontSize));
        fontHeight = g.getFontMetrics().getHeight();
    }

    return fontHeight <= (cellHeight - 2);
}

From source file:Main.java

public static int paintMultilineText(Graphics2D g2d, String text, int textX, int textWidth, int textY,
        int maxTextLineCount) {
    FontRenderContext frc = new FontRenderContext(new AffineTransform(), true, false);
    int fa = g2d.getFontMetrics().getAscent();

    if (text.length() == 0)
        return textY;

    int currOffset = 0;
    AttributedString attributedDescription = new AttributedString(text);
    attributedDescription.addAttribute(TextAttribute.FONT, g2d.getFont());
    LineBreakMeasurer lineBreakMeasurer = new LineBreakMeasurer(attributedDescription.getIterator(), frc);
    int lineCount = 0;
    while (true) {
        TextLayout tl = lineBreakMeasurer.nextLayout(textWidth);
        if (tl == null)
            break;

        int charCount = tl.getCharacterCount();
        String line = text.substring(currOffset, currOffset + charCount);

        g2d.drawString(line, textX, textY);

        textY += fa;/*from  w  w  w .j a  v  a 2 s.c  o  m*/
        currOffset += charCount;

        lineCount++;
        if ((maxTextLineCount > 0) && (lineCount == maxTextLineCount))
            break;
    }

    // textY += fh;

    return textY;
}

From source file:savant.util.MiscUtils.java

/**
 * Patterned off GraphPane.drawMessageHelper, draws a string centred in the given box.
 *///from w  w  w  .j  av a  2 s.  c  om
public static void drawMessage(Graphics2D g2, String message, Rectangle2D box) {
    FontMetrics metrics = g2.getFontMetrics();
    Rectangle2D stringBounds = g2.getFont().getStringBounds(message, g2.getFontRenderContext());
    float x = (float) (box.getX() + (box.getWidth() - stringBounds.getWidth()) / 2.0);
    float y = (float) (box.getY() + (box.getHeight() + metrics.getAscent() - metrics.getDescent()) / 2.0);

    g2.drawString(message, x, y);
}

From source file:org.gitools.ui.app.heatmap.drawer.AbstractHeatmapDrawer.java

protected static void paintCell(Decoration decoration, Color gridColor, int gridSize, int offsetX, int offsetY,
        int width, int height, Graphics2D g, Rectangle box) {

    int y = box.y + offsetY;
    int x = box.x + offsetX;

    g.setColor(decoration.getBgColor());
    g.fillRect(x, y, width, height);//from ww  w  .  ja  v a 2s  .  c  o  m

    g.setColor(gridColor);
    g.fillRect(x, y + height, width, gridSize);

    String text = decoration.getFormatedValue();
    if (!StringUtils.isEmpty(text)) {

        Font font = g.getFont();

        boolean isRotated = decoration.isRotate();

        int fontHeight = (int) font.getSize2D();

        if (fontHeight <= (isRotated ? width : height)) {

            int textWidth = (int) g.getFontMetrics().getStringBounds(text, g).getWidth();
            //TODO: textWidth depends on SuperScript

            if (textWidth < (isRotated ? height : width)) {

                int leftMargin = ((width - textWidth) / 2) + 1;
                int bottomMargin = ((height - fontHeight) / 2) + 1;

                if (isRotated) {
                    leftMargin = ((width - fontHeight) / 2) + 1;
                    bottomMargin = height - (((height - textWidth) / 2));
                }

                g.setColor(Colors.bestForegroundColor(decoration.getBgColor()));
                if (text.matches("[0-9\\.]+e-?[0-9]+")) {
                    int e_pos = text.indexOf("e") + 3;
                    text = text.replaceAll("e(-?[0-9]+)", "10$1");
                    int superscriptEnd = text.length();
                    AttributedString attText = new AttributedString(text);
                    attText.addAttribute(TextAttribute.SUPERSCRIPT, TextAttribute.SUPERSCRIPT_SUPER, e_pos,
                            superscriptEnd);
                    if (isRotated) {
                        g.rotate(radianAngle90);
                        g.drawString(attText.getIterator(), y + height - bottomMargin, -x - leftMargin - 1);
                        g.rotate(-radianAngle90);
                    } else {
                        g.drawString(attText.getIterator(), x + leftMargin, y + height - bottomMargin);
                    }
                } else {

                    if (isRotated) {
                        g.rotate(radianAngle90);
                        g.drawString(text, y + height - bottomMargin, -x - leftMargin - 1);

                        if ("CoCA-08".equals(text)) {
                            System.out.println("x = " + x + " leftMargin = " + leftMargin + " width = " + width
                                    + " fontHeight = " + fontHeight);
                        }

                        g.rotate(-radianAngle90);
                    } else {
                        g.drawString(text, x + leftMargin, y + height - bottomMargin);
                    }
                }
            }
        }
    }

}

From source file:org.gumtree.vis.mask.ChartMaskingUtilities.java

private static void drawMaskName(Graphics2D g2, AbstractMask mask, Rectangle2D imageArea, double fontSizeRate) {
    if (mask.getName() == null) {
        return;//from  ww  w. j  a  va 2  s.co  m
    }
    Point2D fontLocation = mask.getTitleLocation(imageArea);
    g2.setPaint(Color.black);
    Font currentFont = g2.getFont();
    g2.setFont(currentFont.deriveFont((float) (maskNameFont.getSize() * fontSizeRate)).deriveFont(Font.ITALIC));
    g2.drawString(mask.getName(), (int) fontLocation.getX(), (int) fontLocation.getY());
    g2.setFont(currentFont);
}

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:org.optaplanner.examples.flightcrewscheduling.swingui.FlightCrewSchedulingWorldPanel.java

public void resetPanel(FlightCrewSolution solution) {
    translator = new LatitudeLongitudeTranslator();
    for (Airport airport : solution.getAirportList()) {
        translator.addCoordinates(airport.getLatitude(), airport.getLongitude());
    }/*w  w w .ja v a 2s.co m*/

    Dimension size = getSize();
    double width = size.getWidth();
    double height = size.getHeight();
    translator.prepareFor(width, height);

    Graphics2D g = createCanvas(width, height);
    g.setFont(g.getFont().deriveFont((float) LOCATION_NAME_TEXT_SIZE));
    g.setColor(TangoColorFactory.PLUM_2);
    for (Airport airport : solution.getAirportList()) {
        int x = translator.translateLongitudeToX(airport.getLongitude());
        int y = translator.translateLatitudeToY(airport.getLatitude());
        g.fillRect(x - 1, y - 1, 3, 3);
        g.drawString(StringUtils.abbreviate(airport.getCode(), 20), x + 3, y - 3);
    }
    g.setColor(TangoColorFactory.CHOCOLATE_1);
    for (Flight flight : solution.getFlightList()) {
        Airport departureAirport = flight.getDepartureAirport();
        Airport arrivalAirport = flight.getArrivalAirport();
        translator.drawRoute(g, departureAirport.getLongitude(), departureAirport.getLatitude(),
                arrivalAirport.getLongitude(), arrivalAirport.getLatitude(), true, false);
    }
    g.setFont(g.getFont().deriveFont((float) TEXT_SIZE));
    // Legend
    g.setColor(TangoColorFactory.PLUM_2);
    g.fillRect(6, (int) height - 11, 3, 3);
    g.drawString("Airport", 15, (int) height - 5);
    repaint();
}