The Java Developers Almanac 1.4


Order this book from Amazon.

   
Home > List of Packages > java.awt  [78 examples] > Text  [3 examples]

e591. Drawing Simple Text

See also e575 The Quintessential Drawing Program.
    public void paint(Graphics g) {
        // Set the desired font if different from default font
        String family = "Serif";
        int style = Font.PLAIN;
        int size = 12;
        Font font = new Font(family, style, size);
        g.setFont(font);
    
        // Draw a string such that its base line is at x, y
        int x = 10;
        int y = 10;
        g.drawString("aString", x, y);
    
    
        // Draw a string such that the top-left corner is at x, y
        x = 10;
        y = 30;
        FontMetrics fontMetrics = g.getFontMetrics();
        g.drawString("aString", x, y+fontMetrics.getAscent());
    }

 Related Examples
e592. Drawing Rotated Text
e593. Getting the Dimensions of Text

See also: Colors    Components    Containers    Cursors    Drawing    Events    Focus    Frames    GridBagLayout    Images    Shapes    Simulating Events    The Screen   


© 2002 Addison-Wesley.