Available in the LEADTOOLS Imaging toolkit. |
Font example for Visual J++
This example, in a MouseUp event, determines the size of a rectangle for a string in the specified font. It then draws the rectangle and draws the text on top of the rectangle.
private void LEAD1_mouseUp(Object source, MouseEvent e)
{
// Specify the text, font, and styles
String strTest = "This is my text.";
Font font = new Font( "Times New Roman", 16, 0 );
LEAD1.setFont( font );
LEAD1.setDrawFontColor( new Color( 0, 0, 255 ) ); // Blue
LEAD1.setTextStyle( (short) LTOCXU.EfxTextStyleConstants.EFX_TEXTSTYLE_NORMAL );
LEAD1.setTextAlign( (short) LTOCXU.EfxTextAlignConstants.EFX_TEXTALIGN_HCENTER_VCENTER );
// Get the width and height of the text to be drawn.
int nTextWidth = (int) LEAD1.DrawTextWidth( strTest );
int nTextHeight = (int) LEAD1.DrawTextHeight( strTest );
// set the location for the text
LEAD1.setTextTop( e.y );
LEAD1.setTextLeft( e.x );
LEAD1.setTextWidth( nTextWidth );
LEAD1.setTextHeight( nTextHeight );
// Set the properties for drawing a rectangle behind the text.
LEAD1.setDrawPenStyle( (short) LTOCXU.DrawPenStyleConstants.DRAWPENSTYLE_SOLID );
LEAD1.setDrawMode( (short) LTOCXU.DrawModeConstants.DRAWMODE_COPY_PEN );
LEAD1.setDrawFillColor( new Color( 0, 255, 0 ) ); // Green
// Draw on the screen, not on the bitmap.
LEAD1.setDrawPersistence( false );
// Draw the rectangle, then draw the text.
LEAD1.DrawRectangle( e.x, e.y, nTextWidth, nTextHeight );
LEAD1.DrawText( strTest, 0 );
}