Example usage for java.awt Graphics2D setFont

List of usage examples for java.awt Graphics2D setFont

Introduction

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

Prototype

public abstract void setFont(Font font);

Source Link

Document

Sets this graphics context's font to the specified font.

Usage

From source file:FontSizeAnimation.java

public void paint(Graphics g) {
    super.paintComponent(g);
    Graphics2D g2d = (Graphics2D) g;

    Font font = new Font("Dialog", Font.PLAIN, x);
    g2d.setFont(font);

    FontMetrics fm = g2d.getFontMetrics();
    String s = "Java";

    int w = (int) getSize().getWidth();
    int h = (int) getSize().getHeight();

    int stringWidth = fm.stringWidth(s);

    g2d.drawString(s, (w - stringWidth) / 2, h / 2);
}

From source file:PrinterSettingUpDialogPrint.java

public void paintContent(Graphics2D g2D, int w, int h) {
    g2D.setFont(font);

    g2D.drawString("Java 2D", (float) (0.5 * (w - fontMetrics.stringWidth("Java 2D"))),
            (float) (0.5 * h - 1.25 * fontMetrics.getHeight()));
}

From source file:HorizontallyCenteredText.java

public void paint(Graphics g) {
    Graphics2D g2 = (Graphics2D) g;

    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

    g2.setFont(new Font("Serif", Font.PLAIN, 48));

    paintHorizontallyCenteredText(g2, "Java Source", 200, 75);
    paintHorizontallyCenteredText(g2, "and", 200, 125);
    paintHorizontallyCenteredText(g2, "Support", 200, 175);
}

From source file:MainClass.java

public void paint(Graphics g) {
    if (layouts == null)
        getLayouts(g);/*  ww w . ja va  2 s .c o  m*/

    Point pen = new Point(0, 0);
    Graphics2D g2d = (Graphics2D) g;
    g2d.setColor(java.awt.Color.black); // or a property
    g2d.setFont(font);

    Iterator it = layouts.iterator();
    while (it.hasNext()) {
        TextLayout layout = (TextLayout) it.next();
        pen.y += (layout.getAscent());
        g2d.setFont(font);
        layout.draw(g2d, pen.x, pen.y);
        pen.y += layout.getDescent();
    }
}

From source file:MainClass.java

public void paint(Graphics g) {
    Graphics2D g2 = (Graphics2D) g;

    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

    g2.setFont(new Font("Serif", Font.PLAIN, 48));

    FontRenderContext frc = g2.getFontRenderContext();
    String s = "www.java2s.com";
    Rectangle2D bounds = g2.getFont().getStringBounds(s, frc);
    float width = (float) bounds.getWidth();
    int centerX = 100;
    int baselineY = 70;
    g2.drawString(s, centerX - width / 2, baselineY);
}

From source file:DrawStringDemo.java

public void paint(Graphics g) {
    Graphics2D graphics2D = (Graphics2D) g;
    GraphicsEnvironment.getLocalGraphicsEnvironment();
    Font font = new Font("LucidaSans", Font.PLAIN, 40);
    graphics2D.setFont(font);
    graphics2D.drawString(message, 50, 75);
}

From source file:Main.java

public void render(Graphics g) {
    Graphics2D g2d = (Graphics2D) g;
    g2d.setColor(Color.WHITE);//from   ww w.j av  a2s .  co m
    Font font = new Font("Verdana", Font.PLAIN, 20);
    g2d.setFont(font);
    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    FontMetrics fm = g2d.getFontMetrics();

    String option = "This is a test";

    int x = (getWidth() - fm.stringWidth(option)) / 2;
    int y = ((getHeight() - fm.getHeight()) / 2);
    g2d.drawString(option, x, y + fm.getAscent());
    g2d.drawRect((int) x - 20, (int) y - 10, (int) fm.stringWidth(option) + 40, (int) fm.getHeight() + 20);
}

From source file:com.alibaba.webx.tutorial.app1.module.screen.simple.SayHiImage.java

private void writeImage(OutputStream out) throws IOException {
    BufferedImage img = new BufferedImage(800, 600, BufferedImage.TYPE_INT_RGB);
    Graphics2D g2d = img.createGraphics();

    g2d.setPaint(Color.red);//from  w  w  w  . ja  v a  2s . c o  m
    g2d.setFont(new Font("Serif", Font.BOLD, 36));
    g2d.drawString("Hi there, how are you doing today?", 5, g2d.getFontMetrics().getHeight());
    g2d.dispose();

    ImageIO.write(img, "jpg", out);
}

From source file:$.SayHiImage.java

private void writeImage(OutputStream out) throws IOException {
        BufferedImage img = new BufferedImage(800, 600, BufferedImage.TYPE_INT_RGB);
        Graphics2D g2d = img.createGraphics();

        g2d.setPaint(Color.red);/*from w  w w .  j av  a 2 s  . c om*/
        g2d.setFont(new Font("Serif", Font.BOLD, 36));
        g2d.drawString("Hi there, how are you doing today?", 5, g2d.getFontMetrics().getHeight());
        g2d.dispose();

        ImageIO.write(img, "jpg", out);
    }

From source file:Main.java

public void paint(Graphics g) {
    Graphics2D g2 = (Graphics2D) g;
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    Font font = new Font("Serif", Font.PLAIN, 96);
    g2.setFont(font);

    g2.drawString("java2s.com", 40, 120);
}