Text effect gives the stylistic way to design headings. To rotate the text, we have used AffineTransform class that defines different properties like rotation, translation, scaling and shearing etc.
Show Text effect
This section illustrates you how to rotate the text and makes it transparent.
Text effect gives the stylistic way to design headings. To rotate the text, we have used AffineTransform class that defines different properties like rotation, translation, scaling and shearing etc. The method getRotateInstance(Math.PI * (num1 - 1.0f))) of AffineTransform class returns a rotation transformation which will rotate the text. The Math class performs all the numeric operations. The Font class defines the font.
The Dimension class defines the height and width of a component. These
dimensions are used by the method getTranslateInstance( dim.width / 2,dim.height * 3 /
4) which returns the translation transformation.
To give the blending and transparency effects with
graphics and images and combines
source and destination colors, we used the class AlphaComposite. The method getInstance(AlphaComposite.SRC_OVER,num2)) of
AlphaComposite class defines the rule SRC_OVER which composites the source over the
destination.
Here is the code of TextRenderingExample.java
import java.awt.*; import javax.swing.*; import java.awt.geom.*; public class TextRenderingExample extends JPanel { public void paint(Graphics g) { Graphics2D g2d = (Graphics2D) g; Dimension dim = getSize(); AffineTransform affineTransform = AffineTransform.getTranslateInstance( dim.width / 2,dim.height * 3 / 4); g2d.transform(affineTransform); String st = "Welcome"; Font font = new Font("Book Antiqua", Font.PLAIN, 50); g2d.setFont(font); g2d.setColor(Color.red); int counter = 18; for (int k = 1; k <= counter; k++) { AffineTransform transform = g2d.getTransform(); float num1 = (float) k / (float) counter; g2d.transform(AffineTransform.getRotateInstance(Math.PI * (num1 - 1.0f))); float num2 = ((k == counter) ? 1.0f : num1 / 3); g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, num2)); g2d.drawString(st, 0, 0); g2d.setTransform(transform); } } public static void main(String[] args) { JFrame frame = new JFrame("Text Rendering Example"); frame.getContentPane().add(new TextRenderingExample()); frame.setSize(450, 350); frame.show(); } } |
Output will be displayed as: