Java Swing Font drawString(Graphics g, String text, int x, int y, Font font, Color color)

Here you can find the source of drawString(Graphics g, String text, int x, int y, Font font, Color color)

Description

draw String

License

Open Source License

Declaration

public static void drawString(Graphics g, String text, int x, int y, Font font, Color color) 

Method Source Code

//package com.java2s;
/*//from   ww  w.  j  av a 2  s.c o m
 * Copyright appNativa Inc. All Rights Reserved.
 *
 * This file is part of the Real-time Application Rendering Engine (RARE).
 *
 * RARE is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

import java.awt.Color;

import java.awt.Font;
import java.awt.FontMetrics;

import java.awt.Graphics;
import java.awt.Graphics2D;

import java.awt.Insets;

import java.awt.RenderingHints;

import javax.swing.JComponent;

public class Main {
    static RenderingHints renderingHints = null;

    public static void drawString(Graphics g, String text, int x, int y) {
        drawString(g, text, x, y, null, null);
    }

    public static void drawString(JComponent c, Graphics g, String s, Font f, Color fg) {
        if (fg == null) {
            fg = c.getForeground();
        }

        if (f == null) {
            f = c.getFont();
        }

        Insets in = c.getInsets();
        int height = c.getHeight();
        FontMetrics fm = c.getFontMetrics(f);
        int fh = fm.getHeight();
        int dy = (height - fh) / 2;

        drawString(g, s, in.left, height - dy - fm.getDescent(), f, fg);
    }

    public static void drawString(Graphics g, String text, int x, int y, Font font, Color color) {
        Graphics2D g2d = (Graphics2D) g;
        RenderingHints oldHints = null;
        Color oc = g2d.getColor();
        Font of = g2d.getFont();

        if (renderingHints != null) {
            oldHints = g2d.getRenderingHints();
            g2d.addRenderingHints(renderingHints);
        }

        if (font != null) {
            g2d.setFont(font);
        }

        if (color != null) {
            g2d.setColor(color);
        }

        g2d.drawString(text, x, y);

        if (oldHints != null) {
            g2d.addRenderingHints(oldHints);
        }

        g2d.setFont(of);
        g2d.setColor(oc);
    }
}

Related

  1. createTextAttributes(Font baseFont, Color color, boolean bold, boolean italic)
  2. createTextLayout(JComponent c, String s, Font f, FontRenderContext frc)
  3. defaultFont()
  4. drawCentredText(Graphics2D g, Font font, String text, int x, int y)
  5. drawString(Graphics g, Font font, String text, Rectangle rect, int align)
  6. getAsNotUIResource(Font font)
  7. getDefaultFont()
  8. getDefaultLabelBoldFont()
  9. getDefaultLabelFont()