Pages

How to draw rotate string in java ???

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


import java.awt.Frame;
import java.awt.Panel;

import java.awt.Graphics;
import java.awt.Graphics2D;

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

import java.awt.geom.AffineTransform;

public class DrawRotateText extends Panel
{
public DrawRotateText()
{
Frame a=new Frame("Draw Rotate Text");
WindowListener wl=new WindowAdapter()
{
public void windowClosing(WindowEvent evt)
{
System.exit(0);
}
};

a.addWindowListener(wl);
a.add(this);
a.setSize(400,400);
a.setVisible(true);
}

public void paint(Graphics g)
{
Graphics2D g2d=(Graphics2D)g;
AffineTransform at=new AffineTransform();
at.setToRotation(-Math.PI/4);//SET ROTATION HERE IN radians
g2d.setTransform(at);

g2d.drawString("HI EVERYONE !!",100,200);
}

public static void main(String[]args)
{
DrawRotateText drt=new DrawRotateText();
}
}


*********************************************************************
JUST COMPILE AND EXECUTE IT
*********************************************************************

PART 3 Java Source Code : Draw String In Java

This part will show you why when you set g.drawString("HI EVERYONE",10,50); to
g.drawString("HI EVERYONE",10,10); in PART 1, you can't see the wording in frame. This is because wording "HI EVERYONE " is hide at back of frame title. So if you want to see it, you just call method setUndecorated for frame and set it's argument to true. This will make your frame title hide and you can see the wording.

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


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

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

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

public DrawingText3()
{
super("Drawing Text");
addWindowListener(wl);
setUndecorated(true);
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);

g.drawString("HI EVERYONE",10,10);
}

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


************************************************************
JUST COMPILE AND EXECUTE IT
************************************************************

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
************************************************************

PART 1 Java Source Code : Draw String In Java

This is part one of draw string in java. If you change g.drawString("HI
EVERYONE",10,50); in source code below to g.drawString("HI EVERYONE",10,10); you can't see wording. If you want to see it, you can see part three of draw string in
java.So you can know why you can't see it in this part.

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


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

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

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

public DrawingText()
{
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);
g.drawString("HI EVERYONE",10,50);
}

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


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

I Also Can Create T-Shirt Logo For You