In this section, you will study how to draw the rotated text in Java using SWT.
In SWT, we have used the package org.eclipse.draw2D.Graphics to draw the figure on to the surface. The interface IFigure allows to create the complex Graphics and the Figure class implements the Graphical figures. The SWT provides all the drawing capabilities to the class GC of the package org.eclipse.draw2D.Graphics. The object of this class is used to draw an image or any figure. This class provides different methods either to draw or to fill the figure.
To draw the text, we have used the class Font of package java.awt.* which sets the font. The method setColor() of class java.awt.Color sets the color.
Following code draws the rotated text:
| int angle = 12; for (int i = 0; i < angle; i++) { g2d.drawString("HelloWorld", 20, 0); g2d.rotate(-2 * Math.PI / angle); } |
To allow the figure to be hosted on the canvas, we have used the class LightweightSystem. The method setContents() sets the contents of the LightweightSystem to the figure. The class PaletteData describes the color data used by an image. The method setClip() sets the clip region and returns that rectangle. The method getClipping() of class GC returns the bounding rectangle. The description of an image is provided by the class ImageData.
Here is the code of RotateText.java
import java.awt.*;
|
Output will be displayed as:

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.
Ask Questions? Discuss: Rotate Text in Java
Post your Comment