Example usage for java.awt Graphics drawString

List of usage examples for java.awt Graphics drawString

Introduction

In this page you can find the example usage for java.awt Graphics drawString.

Prototype

public abstract void drawString(AttributedCharacterIterator iterator, int x, int y);

Source Link

Document

Renders the text of the specified iterator applying its attributes in accordance with the specification of the java.awt.font.TextAttribute TextAttribute class.

Usage

From source file:CenterTextRectangle.java

public void drawCenteredString(String s, int w, int h, Graphics g) {
    FontMetrics fm = g.getFontMetrics();
    int x = (w - fm.stringWidth(s)) / 2;
    int y = (fm.getAscent() + (h - (fm.getAscent() + fm.getDescent())) / 2);
    g.drawString(s, x, y);
}

From source file:MainClass.java

public void paint(Graphics g) {
    Dimension d = this.getPreferredSize();
    int fontSize = 20;

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

    g.setColor(Color.red);//from ww  w  . j av a2 s  .  c om

    g.drawString("www.java2s.com", 10, 20);
}

From source file:TextLayoutJustify.java

public void drawString(Graphics g, String line, int wc, int lineWidth, int y) {
    if (lineWidth < (int) (d.width * .75)) {
        g.drawString(line, 0, y);
    } else {/*from   ww w .  ja v  a  2  s. co  m*/
        int toFill = (int) ((d.width - lineWidth) / wc);
        int nudge = d.width - lineWidth - (toFill * wc);
        StringTokenizer st = new StringTokenizer(line);
        int x = 0;
        while (st.hasMoreTokens()) {
            String word = st.nextToken();
            g.drawString(word, x, y);
            if (nudge > 0) {
                x = x + fm.stringWidth(word) + space + toFill + 1;
                nudge--;
            } else {
                x = x + fm.stringWidth(word) + space + toFill;
            }
        }
    }
}

From source file:MainClass.java

public void print(Graphics g) {
    Font oldFont = g.getFont();//w w  w. j  av a2s. 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:Redir.java

/** Print a little message to the user. */
public void paint(Graphics g) {
    if (urlString != null)
        g.drawString(urlString, 20, 50);
    else//from  ww  w  . j  a v  a 2 s  .  co m
        g.drawString("Initializing...", 20, 50);
}

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);//from  ww w. j  a va2  s  .  c o m
    g.drawString("Courier", 10, 30);
    g.setFont(system);
    g.drawString("System", 10, 70);
    g.setFont(helvetica);
    g.drawString("Helvetica", 10, 90);
}

From source file:Main.java

@Override
protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    Color color = getBackground();
    String text = color.getRed() + "x" + color.getGreen() + "x" + color.getBlue();

    g.drawString(text, 20, 30);
}

From source file:Main.java

public void paint(Graphics g) {
    int fontSize = 20;
    Font font = new Font("TimesRoman", Font.PLAIN, fontSize);
    g.setFont(font);// www .ja  v a2s. c  om

    font.canDisplay('A');

    String s = "www.java2s.com";

    g.setColor(Color.black);
    g.drawString(s, 30, 30);
}

From source file:Main.java

public void paintComponent(Graphics g) {
    ((Graphics2D) g).setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    g.fillRect(0, 0, getWidth(), getHeight());
    g.setColor(Color.black);/*  www. ja  va  2s  .c o  m*/
    g.drawString(month.format(date), 34, 36);
    g.setColor(Color.white);
    g.drawString(year.format(date), 235, 36);

    Calendar today = Calendar.getInstance();
    today.setTime(date);
    Calendar cal = Calendar.getInstance();
    cal.setTime(date);
    cal.set(Calendar.DATE, 1);
    cal.add(Calendar.DATE, -cal.get(Calendar.DAY_OF_WEEK) + 1);
    for (int week = 0; week < 6; week++) {
        for (int d = 0; d < 7; d++) {
            Color col = Color.black;
            g.drawString(day.format(cal.getTime()), d * 30 + 46 + 4, week * 29 + 81 + 20);
            cal.add(Calendar.DATE, +1);
        }
    }
}

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
}