Pages

PART 2 Java Source Code : Draw String In Java

This part of draw string in java is not much different to the previous part. In this part, we just use FontMetrics class to get information about Font like it's height. You can see it at g.drawString("HI EVERYONE",10,fm.getHeight()*3);

************************************************************
COMPLETE SOURCE CODE FOR : DrawingText2.java
************************************************************


import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Font;
import java.awt.Color;
import java.awt.FontMetrics;

import java.awt.event.WindowListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class DrawingText2 extends Frame
{
WindowListener wl=new WindowAdapter()
{
public void windowClosing(WindowEvent evt)
{
System.exit(0);
}
};

public DrawingText2()
{
super("Drawing Text");
addWindowListener(wl);
setSize(400,200);
setVisible(true);
}

public void paint(Graphics g)
{
super.paint(g);
g.setFont(new Font("Serif",Font.BOLD,12));
g.setColor(Color.BLACK);

FontMetrics fm=g.getFontMetrics();
g.drawString("HI EVERYONE",10,fm.getHeight()*3);
}

public static void main(String[]args)
{
DrawingText2 dt=new DrawingText2();
}
}


************************************************************
JUST COMPILE AND EXECUTE IT
************************************************************
Related Posts Plugin for WordPress, Blogger...