Thursday, August 2, 2012
Today I will post how to continously rotate a text i.e string in Java Graphics2D. As you know that there are 4 types of transformation available in Java which are translation,rotation,scaling and shearing. Here I will use translation and rotation for this purpose. The text will be placed exactly at the center of the screen such that mid-point of text and screen coincide. Then the text will be rotated with screen center as the anchor point of rotation. Video of output is given here
Here is the code for you ->
import java.awt.*;
import java.awt.geom.*;
import java.awt.font.*;
import javax.swing.*;
public class RotateText extends JPanel{
static int angdeg=0;
@Override
public void paint(Graphics g){
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setColor(Color.white); //to remove trail of painting
g2d.fillRect(0,0,getWidth(),getHeight());
Font font = new Font("serif",Font.BOLD,50);
g2d.setFont(font); //setting font of surface
FontRenderContext frc = g2d.getFontRenderContext();
TextLayout layout = new TextLayout("JAVA", font, frc);
//getting width & height of the text
double sw = layout.getBounds().getWidth();
double sh = layout.getBounds().getHeight();
//getting original transform instance
AffineTransform saveTransform=g2d.getTransform();
g2d.setColor(Color.black);
Rectangle rect = this.getBounds();
//drawing the axis
g2d.drawLine((int)(rect.width)/2,0,(int)(rect.width)/2,rect.height);
g2d.drawLine(0,(int)(rect.height)/2,rect.width,(int)(rect.height)/2);
AffineTransform affineTransform = new AffineTransform(); /*creating instance set the translation to the mid of the component*/
affineTransform.setToTranslation((rect.width)/2,(rect.height)/2);
//rotate with the anchor point as the mid of the text
affineTransform.rotate(Math.toRadians(angdeg), 0, 0);
g2d.setTransform(affineTransform);
g2d.drawString("JAVA",(int)-sw/2,(int)sh/2);
g2d.setTransform(saveTransform); //restoring original transform
}
public static void main(String[] args) throws Exception{
JFrame frame = new JFrame("Rotated text");
RotateText rt=new RotateText();
frame.add(rt);
frame.setSize(500, 500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
while(true){
Thread.sleep(10); //sleeping then increasing angle by 5
angdeg=(angdeg>=360)?0:angdeg+5; //
rt.repaint(); //repainting the surface
}
}
}
You can also download full source from below links
DOWNLOAD the source from Mediafire
DOWNLOAD the source from 4shared
Here is the code for you ->
import java.awt.*;
import java.awt.geom.*;
import java.awt.font.*;
import javax.swing.*;
public class RotateText extends JPanel{
static int angdeg=0;
@Override
public void paint(Graphics g){
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setColor(Color.white); //to remove trail of painting
g2d.fillRect(0,0,getWidth(),getHeight());
Font font = new Font("serif",Font.BOLD,50);
g2d.setFont(font); //setting font of surface
FontRenderContext frc = g2d.getFontRenderContext();
TextLayout layout = new TextLayout("JAVA", font, frc);
//getting width & height of the text
double sw = layout.getBounds().getWidth();
double sh = layout.getBounds().getHeight();
//getting original transform instance
AffineTransform saveTransform=g2d.getTransform();
g2d.setColor(Color.black);
Rectangle rect = this.getBounds();
//drawing the axis
g2d.drawLine((int)(rect.width)/2,0,(int)(rect.width)/2,rect.height);
g2d.drawLine(0,(int)(rect.height)/2,rect.width,(int)(rect.height)/2);
AffineTransform affineTransform = new AffineTransform(); /*creating instance set the translation to the mid of the component*/
affineTransform.setToTranslation((rect.width)/2,(rect.height)/2);
//rotate with the anchor point as the mid of the text
affineTransform.rotate(Math.toRadians(angdeg), 0, 0);
g2d.setTransform(affineTransform);
g2d.drawString("JAVA",(int)-sw/2,(int)sh/2);
g2d.setTransform(saveTransform); //restoring original transform
}
public static void main(String[] args) throws Exception{
JFrame frame = new JFrame("Rotated text");
RotateText rt=new RotateText();
frame.add(rt);
frame.setSize(500, 500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
while(true){
Thread.sleep(10); //sleeping then increasing angle by 5
angdeg=(angdeg>=360)?0:angdeg+5; //
rt.repaint(); //repainting the surface
}
}
}
You can also download full source from below links
DOWNLOAD the source from Mediafire
DOWNLOAD the source from 4shared
Labels:Graphics2D
Subscribe to:
Post Comments
(Atom)
Total Pageviews
Followers
Labels
- Algorithms (7)
- Annotation (3)
- Files (6)
- Generics (3)
- Graphics2D (5)
- Graphics2D-Images (7)
- Inheritance (2)
- J2EE (9)
- Java 8 (4)
- Java FAQs (19)
- JDBC (3)
- Networking (2)
- Packages (1)
- Reflection (4)
- Security (7)
- Sorting (2)
- Swing (3)
- Threads (3)
- Utils (3)
Popular Posts
-
Today I will show you how you can implement Bankers algorithm in Java. The Banker's algorithm is a resource allocation and deadlock a...
-
------------------------- UPDATE ------------------------- I have updated the code on request of some followers so that they can directly...
-
Today I am going to show how to convert a postfix expression to an infix expression using stack in Java. In an earlier post here we ...
-
Today in this article I will tell you how to convert an infix expression to postfix expression using stack. This is an important applicat...
-
--------------------UPDATE------------------- I have updated my post so that now it can detect IE 11. This modification was necessary as t...
-
Today I am going to show you how you can generate and validate captcha. A CAPTCHA (an acronym for "Completely Automated Public Turin...
-
Today I am going to post a program that will be able to produce all the mColorings of a given graph G. What is mColoring : The problem st...
-
Today in this article I will show you how to create or develop a Tower of Hanoi game in Java. The Tower of Hanoi is a famous problem tha...
Links
Blog Archive
-
▼
2012
(
25
)
-
▼
August
(
8
)
- Differences between AWT and Swing
- How to set Java compiler's path in Windows7
- What is the difference between import and #include...
- What is a JIT compiler in Java ?
- Why cant you declare a class private ?
- Java program to create Bubbles using Graphics2D
- Rotate Text in Java using Graphics2D transform
- Java program to draw partially transparent image
-
▼
August
(
8
)
check out this page wordpress blog howtoloseweightfastf.com/
ReplyDeleteAttention:
ReplyDeleteg2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
is needed !!! (At least under OpenJDK 1.7.0_45 / Mac OS)
Ciao,
Thomas
Is it possible to make the text increase in size towards the screen whiles rotating
ReplyDelete