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:TestAppletPolicy.java

public void paint(Graphics g) {
    System.out.println("paint(): querying the database");
    try {/* ww  w .  j  av a2  s  .  c om*/
        Statement stmt = conn.createStatement();
        ResultSet rset = stmt.executeQuery("select 'Hello '||initcap(USER) result from dual");
        while (rset.next())
            g.drawString(rset.getString(1), 10, 10);
        rset.close();
        stmt.close();
    } catch (SQLException e) {
        System.err.println("paint(): SQLException: " + e.getMessage());
    }
}

From source file:haven.Utils.java

public static int drawtext(Graphics g, String text, Coord c) {
    java.awt.FontMetrics m = g.getFontMetrics();
    g.drawString(text, c.x, c.y + m.getAscent());
    return (m.getHeight());
}

From source file:org.alfresco.po.share.util.SiteUtil.java

/**
 * This method create in Temp directory jpg file for uploading.
 * /* w  ww  . j a va2  s . co m*/
 * @param jpgName String
 * @return File object for created Image.
 */
public File prepareJpg(String jpgName) {
    BufferedImage image = new BufferedImage(100, 100, BufferedImage.TYPE_INT_RGB);
    Graphics g = image.getGraphics();
    g.drawString("Test Publish file.", 5, 10);
    g.drawString(jpgName, 5, 50);
    try {
        File jpgFile = File.createTempFile(jpgName, ".jpg");
        ImageIO.write(image, "jpg", jpgFile);
        return jpgFile;
    } catch (IOException e) {
        e.printStackTrace();
    }
    throw new SkipException("Can't create JPG file");
}

From source file:MyButtonUI.java

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

    g.setFont(c.getFont());
    FontMetrics fm = g.getFontMetrics();

    g.setColor(b.getForeground());
    String caption = b.getText();
    int x = (d.width - fm.stringWidth(caption)) / 2;
    int y = (d.height + fm.getAscent()) / 2;
    g.drawString(caption, x, y);

}

From source file:yp.tibco.com.yang.lottery.server.LotteryServerIoHandler.java

/**
 * Create an image using the specified request and the text.  
 *
 * @param request/*from w w  w  .java  2s . co m*/
 *  Determines the height and width of the image
 * @param text
 *  The text that is placed in the image
 * @return
 *  a BufferedImage representing the text.
 */
private BufferedImage createImage(ImageRequest request, String text) {
    BufferedImage image = new BufferedImage(request.getWidth(), request.getHeight(),
            BufferedImage.TYPE_BYTE_INDEXED);
    Graphics graphics = image.createGraphics();
    graphics.setColor(Color.YELLOW);
    graphics.fillRect(0, 0, image.getWidth(), image.getHeight());
    Font serif = new Font("serif", Font.PLAIN, 30);
    graphics.setFont(serif);
    graphics.setColor(Color.BLUE);
    graphics.drawString(text, 10, 50);
    return image;
}

From source file:metrics.java

License:asdf

public void paint(Graphics g) {
    g.translate(100, 100);//from  w w w .  jav a2  s .  c om
    FontMetrics fm = null;
    int ascent, descent, leading, width1, width2, height;
    String string1 = "dsdas";
    String string2 = "asdf";
    int xPos = 25, yPos = 50;
    fm = g.getFontMetrics();
    ascent = fm.getAscent();
    descent = fm.getDescent();
    leading = fm.getLeading();
    height = fm.getHeight();
    width1 = fm.stringWidth(string1);
    width2 = fm.stringWidth(string2);
    g.drawString(string1, xPos, yPos);
    g.drawLine(xPos, yPos - ascent - leading, xPos + width1, yPos - ascent - leading);
    g.drawLine(xPos, yPos - ascent, xPos + width1, yPos - ascent);
    g.drawLine(xPos, yPos, xPos + width1, yPos);
    g.drawLine(xPos, yPos + descent, xPos + width1, yPos + descent);
    g.drawString(string2, xPos, yPos + height);
    g.drawLine(xPos, yPos - ascent - leading + height, xPos + width2, yPos - ascent - leading + height);
    g.drawLine(xPos, yPos - ascent + height, xPos + width2, yPos - ascent + height);
    g.drawLine(xPos, yPos + height, xPos + width2, yPos + height);
    g.drawLine(xPos, yPos + descent + height, xPos + width2, yPos + descent + height);

}

From source file:DesktopManagerDemo.java

public void paintEventSquare(Graphics g, int value, int currwidth, int currheight, int cellwidth,
        int cellheight, Color color) {
    if (value != 0) {
        g.setColor(color);//www.ja  va2 s .c  o  m
        g.fillRect(currwidth, currheight, cellwidth, cellheight);
        g.setColor(Color.green);
        g.drawString("" + value, currwidth + 5, currheight + 14);
    }
    g.setColor(Color.black);
    g.drawRect(currwidth, currheight, cellwidth, cellheight);
}

From source file:Main.java

public void paint(Graphics g) {
    g.translate(100, 100);/*from  w  w  w. ja  v  a2  s  . c  o  m*/
    FontMetrics fm = null;
    int ascent, descent, leading, width1, width2, height;
    String string1 = "www.java2s.com";
    String string2 = "java2s.com";
    int xPos = 25, yPos = 50;
    fm = g.getFontMetrics();
    ascent = fm.getAscent();
    descent = fm.getDescent();
    leading = fm.getLeading();
    height = fm.getHeight();
    width1 = fm.stringWidth(string1);
    width2 = fm.stringWidth(string2);
    g.drawString(string1, xPos, yPos);
    g.drawLine(xPos, yPos - ascent - leading, xPos + width1, yPos - ascent - leading);
    g.drawLine(xPos, yPos - ascent, xPos + width1, yPos - ascent);
    g.drawLine(xPos, yPos, xPos + width1, yPos);
    g.drawLine(xPos, yPos + descent, xPos + width1, yPos + descent);
    g.drawString(string2, xPos, yPos + height);
    g.drawLine(xPos, yPos - ascent - leading + height, xPos + width2, yPos - ascent - leading + height);
    g.drawLine(xPos, yPos - ascent + height, xPos + width2, yPos - ascent + height);
    g.drawLine(xPos, yPos + height, xPos + width2, yPos + height);
    g.drawLine(xPos, yPos + descent + height, xPos + width2, yPos + descent + height);

}

From source file:PrintSampleApp.java

public void paint(Graphics g) {
    Dimension size = getSize();/*from   w  w  w .ja  v a2 s  . co  m*/
    int width = size.width;
    int height = size.height;
    int x1 = (int) (width * 0.1);
    int x2 = (int) (width * 0.9);
    int y1 = (int) (height * 0.1);
    int y2 = (int) (height * 0.9);
    g.drawRect(x1, y1, x2 - x1, y2 - y1);
    g.drawOval(x1, y1, x2 - x1, y2 - y1);
    g.drawLine(x1, y1, x2, y2);
    g.drawLine(x2, y1, x1, y2);
    String text = "Print Me! ";
    text += text;
    text += text;
    g.drawString(text, x1, (int) ((y1 + y2) / 2));
    g.dispose();
}

From source file:haven.Utils.java

static void aligntext(Graphics g, String text, Coord c, double ax, double ay) {
    java.awt.FontMetrics m = g.getFontMetrics();
    java.awt.geom.Rectangle2D ts = m.getStringBounds(text, g);
    g.drawString(text, (int) (c.x - ts.getWidth() * ax), (int) (c.y + m.getAscent() - ts.getHeight() * ay));
}