How to Draw a Text in Java


The example aims to draw a text in center of an applet window by Java.awt.

Source Code

package com.beginner.examples;

import java.applet.Applet;
import java.awt.Font;
import java.awt.Graphics;
 
public class DrawExample9 extends Applet{
 
	public void paint(Graphics g){
		
		int x,y;
		String str = "ok";
		// set font
		Font f = new Font("Arial", Font.BOLD, 30);
		g.setFont(f);
		
		//print string
		g.drawString(str, 40, 40);
		System.out.println("ok");
	}
}

Output:

ok

References

Imported packages in Java documentation:

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments