by Dinesh Thakur Category: Java 2D Example

much more powerful mechanism for drawing text is provided  the TextLayout class.This class not only allows you to draw text in a similar way  to as was done be fore but also provides a number of methods that  let youk now what is specific to draw features.

The following tables hows the methods involved in this section:




Returns information about therendering context.

TextLayout(String s, Font f,

FontRenderContext frc)

Creates anobject fromText Layouta chain, a source andtextrendering context

Font(String s, intstyle, int size)

Createa fontfrom aname,andplot stylesize.

draw(Graphics2D, float x,

float y)

Drawtext onGraphics2Dandindicates thedisplacementto the axisofcoordinates.


import java.applet.Applet;

import java.awt.*;
import java.awt.event.*;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.RenderingHints;
import java.awt.font.FontRenderContext;
import java.awt.font.TextLayout;
import java.awt.geom.*;
import java.awt.image.*;
public class DrawTextLayoutExample extends Applet {
public static void main(String[] args) {
Frame DrawTextLayout = new Frame("Draw TextLayout Example");
DrawTextLayout.setSize(350, 250);
Applet DrawTextLayoutExample = new DrawTextLayoutExample();
DrawTextLayout.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
  public void paint(Graphics g) {
    g.setFont(new Font("Arial",Font.BOLD,14));
    g.drawString("Draw TextLayout Java Example", 50, 40);
    g.setFont(new Font("Arial",Font.BOLD,10));
    g.drawString("", 200, 205);
                Graphics2D G2D = (Graphics2D)g;
                FontRenderContext FontRC = G2D.getFontRenderContext();
                Font font = new Font("Arial", Font.ITALIC, 22);
                TextLayout TextL = new TextLayout("Welcome to Java World", font, FontRC);
                TextL.draw(G2D, 60, 120);

TextLayout Java Example

About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.SC (Computer Science), MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.

Search Content