Credits

Thursday, March 24, 2011

Java's Graphics Class

 Java allows the use of Graphics class to be the framework for setting text / background colors, changing font status, and drawing geometrical shapes and texts among others.


Just like the init method of Graphics, Java makes use of paint ( ) to create Graphics object where methods can be associated with.


One of the Graphics methods is the drawString ( ) that is used to write text of different font type, styles and size.


The program below illustrates the use of drawString.
[+/-] show/hide


/* sample program on graphics */

import java.applet.*;
import java.awt.*;


public class GraphicsDemo2 extends Applet {
String companyName = new String("STI");
Color newColor = new Color(100,20,255);//creating your own color
Font bigFont = new Font("Helvetica",Font.ITALIC,24);


public void paint(Graphics gr){
gr.setFont(bigFont);
gr.setColor(newColor);
gr.drawString(companyName, 10, 100);
gr.setColor(Color.magenta);
gr.drawString(companyName, 40, 140);
setBackground(Color.YELLOW);
}


}

AddThis