Pixel example for Visual J++

This example, using the MouseDown event, gets the value of the current pixel and sets all values in the line to that value. The example shows how to convert mouse coordinates to bitmap pixel coordinates, regardless of how the image is zoomed or scrolled.

private void LEAD1_mouseDown(Object source, MouseEvent e)
{
   // Translate mouse coordinates to fractions of the destination width and height.
   int xfraction = (int) ( ( e.x - LEAD1.getDstLeft() ) / LEAD1.getDstWidth() );
   int yfraction = (int) ( ( e.y - LEAD1.getDstTop() ) / LEAD1.getDstHeight() );

   // Convert fractions to bitmap pixel coordinates. 
   int xPixel = (int) ( ( LEAD1.getSrcWidth() * xfraction) + LEAD1.getSrcLeft() );
   int yPixel = (int) ( ( LEAD1.getSrcHeight() * yfraction) + LEAD1.getSrcTop() );

   // Make the whole line the same color as the pixel.
   Color xyColor = LEAD1.getPixel( xPixel, yPixel );
   for( xPixel = 0 ; xPixel <= LEAD1.getBitmapWidth(); xPixel++ )
      LEAD1.setPixel( xPixel, yPixel, xyColor );
   LEAD1.ForceRepaint();
}