Example usage for java.awt Graphics setFont

List of usage examples for java.awt Graphics setFont

Introduction

In this page you can find the example usage for java.awt Graphics 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:Main.java

public void paint(Graphics g) {
    int fontSize = 20;
    Font font = new Font("TimesRoman", Font.PLAIN, fontSize);
    g.setFont(font);

    font.canDisplay('A');

    String s = "www.java2s.com";

    g.setColor(Color.black);//from  w w  w .j a  va  2s .co m
    g.drawString(s, 30, 30);
}

From source file:TextLayoutLeft.java

public void paint(Graphics g) {
    d = getSize();//from   w  ww  .  java 2s  .  c  om
    g.setFont(f);
    if (fm == null) {
        fm = g.getFontMetrics();
        ascent = fm.getAscent();
        fh = ascent + fm.getDescent();
        space = fm.stringWidth(" ");
    }
    g.setColor(Color.black);
    StringTokenizer st = new StringTokenizer(
            "this is a text. this is a test <BR> this is a text. this is a test");
    int x = 0;
    int nextx;
    int y = 0;
    String word, sp;
    int wordCount = 0;
    String line = "";
    while (st.hasMoreTokens()) {
        word = st.nextToken();
        if (word.equals("<BR>")) {
            drawString(g, line, wordCount, fm.stringWidth(line), y + ascent);
            line = "";
            wordCount = 0;
            x = 0;
            y = y + (fh * 2);
        } else {
            int w = fm.stringWidth(word);
            if ((nextx = (x + space + w)) > d.width) {
                drawString(g, line, wordCount, fm.stringWidth(line), y + ascent);
                line = "";
                wordCount = 0;
                x = 0;
                y = y + fh;
            }
            if (x != 0) {
                sp = " ";
            } else {
                sp = "";
            }
            line = line + sp + word;
            x = x + space + w;
            wordCount++;
        }
    }
    drawString(g, line, wordCount, fm.stringWidth(line), y + ascent);
}

From source file:ColorGradient.java

/** Draw the applet. The interesting code is in fillGradient() below */
public void paint(Graphics g) {
    fillGradient(this, g, startColor, endColor); // display the gradient
    g.setFont(bigFont); // set a font
    g.setColor(new Color(100, 100, 200)); // light blue
    g.drawString("Colors!", 100, 100); // draw something interesting
}

From source file:Main.java

public void paint(Graphics g) {

    int fontSize = 20;

    Font font = new Font("TimesRoman", Font.PLAIN, fontSize);
    g.setFont(font);

    FontMetrics fm = g.getFontMetrics(font);

    String s = "www.java2s.com";

    int stringWidth = fm.stringWidth(s);

    int w = 200;//from  ww w .  j a v a  2 s.  c o  m
    int h = 200;

    int x = (w - stringWidth) / 2;
    int baseline = fm.getMaxAscent() + (h - (fm.getAscent() + fm.getMaxDecent())) / 2;
    int ascent = fm.getMaxAscent();
    int descent = fm.getMaxDecent();
    int fontHeight = fm.getMaxAscent() + fm.getMaxDecent();

    g.setColor(Color.white);
    g.fillRect(x, baseline - ascent, stringWidth, fontHeight);

    g.setColor(Color.gray);
    g.drawLine(x, baseline, x + stringWidth, baseline);

    g.setColor(Color.red);
    g.drawLine(x, baseline + descent, x + stringWidth, baseline + descent);

    g.setColor(Color.blue);
    g.drawLine(x, baseline - ascent, x + stringWidth, baseline - ascent);
    g.setColor(Color.black);
    g.drawString(s, x, baseline);

}

From source file:MainClass.java

public void print(Graphics g) {
    Font oldFont = g.getFont();/* ww w.  ja v  a2  s  . c om*/
    if (type == TEXT) {
        g.setFont(font);
        g.drawString(text, x, y);
    } else if (type == GRAPHICS) {
        if (shape.equals("LINE")) {
            g.drawLine(x, y, width, height);
        } else if (shape.equals("OVAL")) {
            g.drawOval(x, y, width, height);
        } else if (shape.equals("RECTANGLE")) {
            g.drawRect(x, y, width, height);
        }
    }
    g.setFont(oldFont);
}

From source file:CenterTextRectangle.java

public void paint(Graphics g) {
    Dimension d = this.getSize();

    g.setColor(Color.white);// w  ww  . j  ava 2 s  . c  o m
    g.fillRect(0, 0, d.width, d.height);
    g.setColor(Color.black);
    g.setFont(f);
    drawCenteredString("This is centered.", d.width, d.height, g);
    g.drawRect(0, 0, d.width - 1, d.height - 1);
}

From source file:MyCanvas.java

public void paint(Graphics g) {
    Font courier = new Font("Courier", Font.PLAIN, 12);
    Font system = new Font("System", Font.BOLD, 16);
    Font helvetica = new Font("Helvetica", Font.BOLD, 18);

    g.setFont(courier);
    g.drawString("Courier", 10, 30);
    g.setFont(system);//www.  ja v  a  2  s  . c  o  m
    g.drawString("System", 10, 70);
    g.setFont(helvetica);
    g.drawString("Helvetica", 10, 90);
}

From source file:MyCheckBoxUI.java

public void paint(Graphics g, JComponent c) {
    AbstractButton b = (AbstractButton) c;
    ButtonModel model = b.getModel();
    Dimension d = b.getSize();//from w w  w. j  a  v a 2 s .c o  m

    g.setFont(c.getFont());
    FontMetrics fm = g.getFontMetrics();
    g.setColor(Color.white);
    g.drawString("Am I a check box", 10, 10);

}

From source file:Main.java

@Override
protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    FontMetrics fm = g.getFontMetrics(font);
    wordWidth = fm.stringWidth(word);/*from  ww  w. jav  a2s  .  c o  m*/
    wordHeight = fm.getAscent();

    g.setFont(new Font("impact", Font.PLAIN, 28));
    g.setColor(Color.BLUE);
    g.drawString(word, x, y);
}

From source file:com.codenvy.corp.MainPage.java

private void addText(File file, String burnDownInfo) throws IOException {
    BufferedImage image = ImageIO.read(file);
    Graphics g = image.getGraphics();
    g.setColor(Color.RED);/*from  w ww . j a v  a 2 s  . co m*/
    g.setFont(g.getFont().deriveFont(30f));
    g.drawString(burnDownInfo, 550, 45);
    g.dispose();
    ImageIO.write(image, "png", file);
}