PaintPalette example for Visual J++

This example sets the paint palette to the palette of each Lead control. It then loads two images and dithers them to 8 bits per pixel with optimized palettes, and displays them side by side. In 8-bit display mode, you can see the palette problems that result when you do not use a fixed palette.

LEAD1.setPaintPalette( (short) LTOCXU.PaintPaletteConstants.PAINTPALETTE_AUTO );
LEAD2.setPaintPalette( (short) LTOCXU.PaintPaletteConstants.PAINTPALETTE_AUTO );

// Disable automatic repainting of the image.
LEAD1.setAutoRepaint( false );
LEAD2.setAutoRepaint( false );

// Turn off scroll bars to make sure we use the full client area.
LEAD1.setAutoScroll( false );
LEAD2.setAutoScroll( false );

// Clear the bitmaps from memory
LEAD1.setBitmap( 0 );
LEAD2.setBitmap( 0 );
setCursor( Cursor.WAIT );  // Hourglass

// Load the bitmaps. These hard-coded path names may be different on your system.
LEAD1.Load( "c:\\lead\\images\\image1.cmp", (short) 0, (short) 0, (short) 1 );
LEAD2.Load( "c:\\lead\\images\\image2.cmp", (short) 0, (short) 0, (short) 1 );

// Change the bitmaps to 8 BPS with optimized palettes
LEAD1.ColorRes( (short) 8, (short) LTOCXU.ColorResPaletteConstants.CRP_OPTIMIZEDPALETTE,
                (short) LTOCXU.ColorResDitherConstants.CRD_FLOYDSTEINDITHERING, (short) 0 );
LEAD2.ColorRes( (short) 8, (short) LTOCXU.ColorResPaletteConstants.CRP_OPTIMIZEDPALETTE, 
                (short) LTOCXU.ColorResDitherConstants.CRD_FLOYDSTEINDITHERING, (short) 0 );

// Make the controls visible so that we can size and position them.
// They will not really appear until we load an image and paint it.
LEAD1.setVisible( true );
LEAD2.setVisible( true );

// Set the variables used for preserving the aspect ratio.
int nHeightFactor1 = (int) LEAD1.getBitmapHeight();
int nWidthFactor1 = (int) LEAD1.getBitmapWidth();
int nHeightFactor2 = (int) LEAD2.getBitmapHeight();
int nWidthFactor2 = (int) LEAD2.getBitmapWidth();
int nHeightAllowed = (int) ( getSize().y * 0.8f );
int nWidthAllowed = (int)( getSize().x * 0.45f );

// Center each LEAD control on half of the form, preserving the aspect ratio.
// Check to see if using the maximum width will make the image too tall.
// Set the dimensions based on the result.
if( ( ( nWidthAllowed * nHeightFactor1 ) / nWidthFactor1 ) < nHeightAllowed )
{
   LEAD1.setLeft( ( getSize().x / 4 ) - ( nWidthAllowed / 2 ) );
   LEAD1.setWidth( nWidthAllowed );
   LEAD1.setHeight( ( LEAD1.getWidth() * nHeightFactor1 ) / nWidthFactor1 );
   LEAD1.setTop( ( getSize().y - LEAD1.getHeight () ) / 2 );
}
else
{
   LEAD1.setTop( ( getSize().y - nHeightAllowed ) / 2 );
   LEAD1.setHeight( nHeightAllowed );
   LEAD1.setWidth( ( LEAD1.getHeight() * nWidthFactor1 ) / nHeightFactor1 );
   LEAD1.setLeft( ( getSize().x / 4 ) - ( LEAD1.getWidth() / 2 ) );
}

if( ( ( nWidthAllowed * nHeightFactor2 ) / nWidthFactor2 ) < nHeightAllowed )
{
   LEAD2.setLeft( ( getSize().x * 3 / 4 ) - ( nWidthAllowed / 2 ) );
   LEAD2.setWidth( nWidthAllowed );
   LEAD2.setHeight( ( LEAD2.getWidth() * nHeightFactor2 ) / nWidthFactor2 );
   LEAD2.setTop( ( getSize().y - LEAD2.getHeight() ) / 2 );
}
else
{
   LEAD2.setTop( ( getSize().y - nHeightAllowed ) / 2 );
   LEAD2.setHeight( nHeightAllowed );
   LEAD2.setWidth( ( LEAD2.getHeight() * nWidthFactor2 ) / nHeightFactor2 );
   LEAD2.setLeft( ( getSize().x * 3 / 4 ) - ( LEAD2.getWidth() / 2 ) );
}

// Set the image display sizes to match the LEAD controls
LEAD1.SetDstRect( 0, 0, LEAD1.getScaleWidth(), LEAD1.getScaleHeight() );
LEAD1.SetDstClipRect( 0, 0, LEAD1.getScaleWidth(), LEAD1.getScaleHeight() );
LEAD2.SetDstRect( 0, 0, LEAD2.getScaleWidth(), LEAD2.getScaleHeight() );
LEAD2.SetDstClipRect( 0, 0, LEAD2.getScaleWidth(), LEAD2.getScaleHeight() );

// Display the images
LEAD1.ForceRepaint();
LEAD2.ForceRepaint();
setCursor( Cursor.DEFAULT );  // Default