Rotate Text in Java

In this section, you will study how to draw the rotated text in Java using SWT.

Rotate Text in Java

Rotate Text in Java

     

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.*;
import org.eclipse.draw2d.*;
import org.eclipse.swt.widgets.*;
import org.eclipse.swt.graphics.*;
import java.awt.image.BufferedImage;
import org.eclipse.draw2d.geometry.Dimension;

public class RotateText {
  public static void main(String[] args) {
  final ImageRenderer renderer = new ImageRenderer();
  Shell shell = new Shell();
  shell.setSize(300300);
  shell.open();
  shell.setText("rotate text");
  LightweightSystem lightweightSystem = new LightweightSystem(shell);
  IFigure figure = new Figure() {
  public void paint(org.eclipse.draw2d.Graphics g) {
  Dimension dim = getSize();
  renderer.createRendering(g);
  
  Graphics2D g2d = renderer.getGraphics2D();
  g2d.setPaint(java.awt.Color.cyan);
  g2d.fillRect(00, dim.width, dim.width);
  g2d.setFont
  (
new java.awt.Font("Arial Narrow", java.awt.Font.BOLD,15));
  g2d.setColor(java.awt.Color.red);
  g2d.translate(dim.width / 2, dim.width / 2);
  int angle = 12;
  for (int i = 0; i < angle; i++) {
  g2d.drawString("HelloWorld"200);
  g2d.rotate(-* Math.PI / angle);
  }
  renderer.render(g);
  }
  };
  lightweightSystem.setContents(figure);
  Display display = Display.getDefault();
  while (!shell.isDisposed()) {
  if (!display.readAndDispatch())
  display.sleep();
  }
  }
}
class ImageRenderer {
  PaletteData paletteData = new PaletteData(0xFF0000,0xFF000xFF);
 java.awt.image.BufferedImage bufferedImage;
 org.eclipse.swt.graphics.Image image;
 ImageData imageData;
int[] pixels;
// allows to render on a Draw2D Graphics.
 public void createRendering(org.eclipse.draw2d.Graphics g) {
  org.eclipse.draw2d.geometry.Rectangle rect = g
  .getClip(new org.eclipse.draw2d.geometry.Rectangle());
  createRendering(rect.x, rect.y, rect.width, rect.height);
  }
// create an image and allows rendering of the rectangular area.
 private void createRendering(int X, int Y, int W, int H) {
  checkImages(W, H);
  java.awt.Graphics awtGraphics = bufferedImage.getGraphics();
  awtGraphics.fillRect(X, Y, W, H);
  }
  public Graphics2D getGraphics2D() {
  if (bufferedImage == null)
  return null;
  return (Graphics2D) bufferedImage.getGraphics();
  }
  public void render(GC gc) {
  if (bufferedImage == null)
  return;

  org.eclipse.swt.graphics.Rectangle rect = gc.getClipping();
  transfer(rect.x, rect.y, rect.width, rect.height);
  gc.drawImage(image, rect.x, rect.y, rect.width, rect.height, rect.x,
  rect.y, rect.width, rect.height);
  }
  public void render(org.eclipse.draw2d.Graphics graphics) {
  if (bufferedImage == null)
  return;
 org.eclipse.draw2d.geometry.Rectangle rect = graphics
  .getClip(new org.eclipse.draw2d.geometry.Rectangle());
  transfer(rect.x, rect.y, rect.width, rect.height);
  graphics.drawImage(image, rect.x, rect.y, rect.width, rect.height,
  rect.x, rect.y, rect.width, rect.height);
  }
  // Transfers the region from the AWT image to the SWT image.
  private void transfer(int X, int Y, int W, int H) {
  int step = imageData.depth / 8;
  byte[] data = imageData.data;
  bufferedImage.getRGB(X, Y, W, H, pixels, 0, W);
  for (int i = 0; i < H; i++) {
  int idx = (Y + i) * imageData.bytesPerLine + X * step;
  for (int j = 0; j < W; j++) {
  int rgb = pixels[j + i * W];
  for (int k = imageData.depth - 8; k >= 0; k -= 8) {
  data[idx++] = (byte) ((rgb >> k) & 0xFF);
  }
  }
  }
  image = new 
org.eclipse.swt.graphics.Image(Display.getDefault(),imageData);
  }
  // Initialize the image.
  private void checkImages(int width, int height) {
  int imageWidth = 0;
  int imageHeight = 0;
  if (image != null) {
  imageWidth = image.getImageData().width;
  imageHeight = image.getImageData().height;
  }
  if (width > imageWidth || height > imageHeight) {
  width = Math.max(width, imageWidth);
  height = Math.max(height, imageHeight);
  bufferedImage = new BufferedImage(width, height,
  BufferedImage.TYPE_INT_ARGB);
  imageData = new ImageData(width, height, 24, paletteData);
  pixels = new int[width * height];
  }
  }
}

Output will be displayed as:

Download Source Code: