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);    




  1. Another interesting articles The programming was very easily understand and more important coding are provided.this is very valuable in my studies,all coding easily understand.
    sap basis training in chennai

  2. While i execute this code ,it is showing that main method isnt found.pls help

    1. applet programs DO NOT have main method. And also they are not executed as a normal java program. You have to use either a tag-based program or appletviewer

  3. You would realize that you would be able to easily find and buy graphics software according to your needs and according to reliability if you would only know how. Adobe After Effects CC 2014