Available in the LEADTOOLS Imaging toolkit. |
Drawing Simple Lines and Shapes (Visual J++)
Take the following steps to add code that lets you draw a line, rectangle and ellipse on the bitmap.
1. Start with the project that you created in Loading and Displaying an Image.
2. Add the following private member variables to your main form:
private int m_nDrawObject = 0; // The object we are drawing
private int m_nStartX = 0; // Starting X position
private int m_nStartY = 0; // Starting Y position
private int m_nEndX = 0; // Ending X position
private int m_nEndY = 0; // Ending Y position
3. Code the LEAD control's mouseDown event as follows. This code selects a different drawing object each time the event occurs.
private void LEAD1_mouseDown(Object source, MouseEvent e)
{
// Save the starting position.
m_nStartX = e.x;
m_nStartY = e.y;
m_nEndX = e.x;
m_nEndY = e.y;
// Cycle through the types of drawing objects.
switch( m_nDrawObject )
{
case 0 :
m_nDrawObject = 1; // Line
break ;
case 1 :
m_nDrawObject = 2; // Rectangle
break ;
case 3 :
m_nDrawObject = 0; // Ellipse
break ;
default :
m_nDrawObject = 0;
break ;
}
}
4. Code the LEAD control's mouseMove event as follows. This code uses DRAWMODE_INVERT for the DrawMode, which means that pixel colors are inverted. Thus, the drawing methods can erase the previous object and draw a new one.
private void LEAD1_mouseMove(Object source, MouseEvent e)
{
// Set the drawing styles.
LEAD1.setDrawPenStyle( (short) LTOCXU.DrawPenStyleConstants.DRAWPENSTYLE_SOLID );
LEAD1.setDrawMode( (short) LTOCXU.DrawModeConstants.DRAWMODE_INVERT );
LEAD1.setDrawFillStyle( (short) LTOCXU.DrawFillStyleConstants.DRAWFILLSTYLE_TRANSPARENT );
LEAD1.setDrawPersistence( false ); // On the window, not the bitmap
// Save the previous ending mouse position.
int nOldEndX = m_nEndX;
int nOldEndY = m_nEndY;
// Get the current mouse position.
m_nEndX = e.x;
m_nEndY = e.y;
// Calculate the origin of the current object.
int nDrawX, nDrawY;
if( m_nEndX > m_nStartX )
nDrawX = m_nStartX;
else
nDrawX = m_nEndX;
if( m_nEndY > m_nStartY )
nDrawY = m_nStartY;
else
nDrawY = m_nEndY;
// Calculate the origin of the previous object.
int nOldDrawX, nOldDrawY;
if( nOldEndX > m_nStartX )
nOldDrawX = m_nStartX;
else
nOldDrawX = nOldEndX;
if( nOldEndY > m_nStartY )
nOldDrawY = m_nStartY;
else
nOldDrawY = nOldEndY;
// Calculate the height and width of the current object.
int nNewHeight = Math.abs( m_nStartY - m_nEndY );
int nNewWidth = Math.abs( m_nStartX - m_nEndX );
// Calculate the height and width of the previous object.
int nOldHeight = Math.abs( m_nStartY - nOldEndY );
int nOldWidth = Math.abs( m_nStartX - nOldEndX );
// Erase the old object and draw the new one.
switch( m_nDrawObject )
{
case 0 : // Ellipse
LEAD1.DrawEllipse( nOldDrawX, nOldDrawY, nOldWidth, nOldHeight );
LEAD1.DrawEllipse( nDrawX, nDrawY, nNewWidth, nNewHeight );
break;
case 1 : // Line
LEAD1.DrawLine( m_nStartX, m_nStartY, nOldEndX, nOldEndY );
LEAD1.DrawLine( m_nStartX, m_nStartY, m_nEndX, m_nEndY );
break;
case 2 : // Rectangle
LEAD1.DrawRectangle( nOldDrawX, nOldDrawY, nOldWidth, nOldHeight );
LEAD1.DrawRectangle( nDrawX, nDrawY, nNewWidth, nNewHeight );
break;
default :
break;
}
}
5. Code the LEAD control's mouseUp event as follows. This code sets the drawing style and draws the object on the bitmap.
private void LEAD1_mouseUp(Object source, MouseEvent e)
{
// Set the drawing style.
LEAD1.setDrawPenStyle( (short) LTOCXU.DrawPenStyleConstants.DRAWPENSTYLE_SOLID );
LEAD1.setDrawPenWidth( (short) 2 );
LEAD1.setDrawPenColor( new Color( 255, 0, 0 ) ); // Red
LEAD1.setDrawMode( (short) LTOCXU.DrawModeConstants.DRAWMODE_COPY_PEN );
LEAD1.setDrawFillColor( new Color( 0, 255, 0 ) ); // Green
LEAD1.setDrawFillStyle( (short) LTOCXU.DrawFillStyleConstants.DRAWFILLSTYLE_HORIZONTAL_LINE );
LEAD1.setDrawPersistence( true ); // On the bitmap
// Get the current mouse position
m_nEndX = e.x;
m_nEndY = e.y;
// Determine the origin of the object.
int nDrawX, nDrawY;
if( m_nEndX > m_nStartX )
nDrawX = m_nStartX;
else
nDrawX = m_nEndX;
if( m_nEndY > m_nStartY )
nDrawY = m_nStartY;
else
nDrawY = m_nEndY;
// Determine the height and width of the object.
int nNewHeight = Math.abs( m_nStartY - m_nEndY );
int nNewWidth = Math.abs( m_nStartX - m_nEndX );
// Draw the object
switch( m_nDrawObject )
{
case 0 : // Ellipse
LEAD1.DrawEllipse( nDrawX, nDrawY, nNewWidth, nNewHeight );
break;
case 1 : // Line
LEAD1.DrawLine( m_nStartX, m_nStartY, m_nEndX, m_nEndY );
break;
case 2 : // Rectangle
LEAD1.DrawRectangle( nDrawX, nDrawY, nNewWidth, nNewHeight );
break;
default :
break;
}
}
6. Run your program to test it.