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.drawString(companyName, 10, 100);
gr.drawString(companyName, 40, 140);