GraphicsUtil class
The GraphicsUtil class creates the image containing the specified text and rotates the text image to the specified angle, using the following methods:
*createRotatedText( )
This method performs the following operations:
*Gets the display text and rotation angle properties
*Sets the display text font and determines the font metrics
*Creates an image the same size as the display text String
*Draws the display text as an image
*Calls the rotateImage( ) method to rotate the image at the specified angle
*Disposes of the operating system resources used to render the image
*Returns the image object
Listing 23‑4 shows the code for createRotatedText( ) method.
Listing 23‑4 The createRotatedText( ) method
public Image createRotatedText( ExtendedItemHandle
modelHandle )
{
Image stringImage;
Image image;
GC gc;
String text = "";
if ( modelHandle.getProperty( "displayText" ) != null ) {
text = ( String ) modelHandle.getProperty
( "displayText" );
}
Integer angle = -45;
if ( modelHandle.getProperty( "rotationAngle" ) != null ) {
try {
angle = Integer.parseInt( (String)
modelHandle.getProperty( "rotationAngle" ));
}
catch( NumberFormatException e ) {
angle = -45;
}
}
String fontFamily = "Arial";
if ( modelHandle.getProperty(Style.FONT_FAMILY_PROP ) !=
null ) {
fontFamily = ( String ) modelHandle.getProperty
( Style.FONT_FAMILY_PROP );
}
StyleHandle labelStyle = modelHandle.getPrivateStyle();
DimensionHandle fontSize =
(DimensionHandle) labelStyle.getFontSize();
int height = 12;
String units = fontSize.getUnits( );
if ( units.compareToIgnoreCase("pt") == 0 )
{
height = (int) fontSize.getMeasure();
}
if ( display == null ) SWT.error( SWT.ERROR_THREAD_INVALID_ACCESS );
}
 
FontData fontData = new FontData( fontFamily, height, 0 );
Font font = new Font( display, fontData );
try {
gc = new GC( display );
gc.setFont( font );
gc.getFontMetrics( );
Point pt = gc.textExtent( text );
gc.dispose( );
stringImage = new Image( display, pt.x, pt.y );
gc = new GC( stringImage );
gc.setFont( font );
gc.drawText( text, 0, 0 );
image = rotateImage( stringImage, angle.doubleValue( ) );
gc.dispose( );
stringImage.dispose( );
return image;
}
catch( Exception e ) {
e.printStackTrace( );
}
return null;
}
*rotateImage( )
This method rotates the image and determines the height, width, and point of origin for the image, as shown in Listing 23‑5.
Listing 23‑5 The rotateImage( ) method
private Image rotateImage ( Image img, double degrees )
{
double positiveDegrees = ( degrees % 360 ) +
( ( degrees < 0 ) ? 360 : 0 );
double degreesMod90 = positiveDegrees % 90;
double radians = Math.toRadians( positiveDegrees );
double radiansMod90 = Math.toRadians( degreesMod90 );
if ( positiveDegrees == 0 ) {
return img;
}
int quadrant = 0;
if ( positiveDegrees < 90 ) {
quadrant = 1;
}
else if ( ( positiveDegrees >= 90 ) &&
( positiveDegrees < 180 ) ) {
quadrant = 2;
}
else if ( ( positiveDegrees >= 180 ) &&
( positiveDegrees < 270 ) ) {
quadrant = 3;
}
else if ( positiveDegrees >= 270 ) {
quadrant = 4;
}
int height = img.getBounds( ).height;
int width = img.getBounds( ).width;
double side1 = ( Math.sin( radiansMod90 ) * height ) +
( Math.cos( radiansMod90 ) * width );
double side2 = ( Math.cos( radiansMod90 ) * height ) +
( Math.sin( radiansMod90 ) * width );
double h = 0;
int newWidth = 0, newHeight = 0;
if ( ( quadrant == 1 ) || ( quadrant == 3) ) {
h = ( Math.sin( radiansMod90) * height );
newWidth = ( int )side1;
newHeight = ( int )side2;
} else {
h = ( Math.sin( radiansMod90 ) * width );
newWidth = ( int )side2;
newHeight = ( int )side1;
}
int shiftX = ( int )( Math.cos( radians ) * h ) -
( ( quadrant == 3 ) || ( quadrant == 4 ) ? width : 0 );
int shiftY = ( int )( Math.sin( radians ) * h ) +
( ( quadrant == 2) || ( quadrant == 3 ) ? height : 0 );
Image newImg = new Image( display, newWidth, newHeight );
GC newGC = new GC( newImg );
Transform tr = new Transform( display );
tr.rotate( ( float )positiveDegrees );
newGC.setTransform( tr );
newGC.setBackground( display.getSystemColor
( SWT.COLOR_WHITE ) );
newGC.drawImage( img, shiftX, -shiftY );
newGC.dispose( );
return newImg;
}