Showing posts with label drawString(). Show all posts
Showing posts with label drawString(). Show all posts

Monday, 22 February 2016

drawString() method of Graphics Class in Java

Details about the drawString() method in Java


               drawString is one of the most used method from teh Graphics class to generate text out in a Swing Window. The drawString() method can be used with abstract and void modifiers. 

General Syntax


i) drawString(String str, int x, int y)
ii) drawString(AttributedCharacterIterator iterator, int x, int y)

i) drawString(String str, int x, int y)


          String str is the string that can be displayed on the screen. Integer type x and y are the variables hold the x and y position on the graphical window. 

ii) drawString(AttributedCharacterIterator iterator, int x, int y)


         Renders the text of the specified iterator applying its attributes in accordance with the specification of the TextAttribute class. An AttributedCharacterIterator allows iteration through both text and related attribute information. An attribute is a key/value pair, identified by the key. No two attributes on a given character can have the same key. The values for an attribute are immutable, or must not be mutated by clients or storage. They are always passed by reference, and not cloned.

Example Program to Demonstrate drawString() method:


// drawString example program
import java.awt.*;
import java.applet.*;

public class HelloWorldApplet extends Applet{
    public void paint(Graphics g) {    

    g.drawString("Hello World", 100, 100);    

    }
}

Output: