Creating and Using Annotations (Visual J++)

Note: This topic is for Document/Medical only.

Take the following steps to add code that demonstrates the creation and deletion, saving and loading, and copying and pasting of annotation objects. This code also demonstrates the related events. Message boxes prompt for user actions that trigger events.

1. Start with the project that you created in Loading and Displaying an Image.

2. image\btncmd.gif At the top of your main form, add four buttons and name them as follows:

Name

Text

AnnToggle

Start

IOToggle

Save

ClipboardToggle

Copy

Realize

Realize

3. Code the AnnToggle control's Click event as follows. In online help, you can use the Edit pull-down menu to copy the block of code.

private void AnnToggle_click(Object source, Event e)
{
   // Use the button to toggle between design mode and run mode
   if( AnnToggle.getText().equals( "Start" ) )
   {
      LEAD1.setAnnUserMode( (short) LTOCXU.AnnUserModeConstants.ANNUSERMODE_DESIGN );
      LEAD1.setAnnTool( (short) LTOCXU.AnnToolConstants.ANNTOOL_BUTTON );

      // Make run mode the next thing.
      AnnToggle.setText( "Run Mode" );
      MessageBox.show( "In design mode now. Draw a button object." );
   }
   else
   if( AnnToggle.getText().equals( "Design Mode" ) )
   {
      LEAD1.setAnnUserMode( (short) LTOCXU.AnnUserModeConstants.ANNUSERMODE_DESIGN );
      LEAD1.setAnnTool( (short) LTOCXU.AnnToolConstants.ANNTOOL_BUTTON );

      // Make run mode the next thing.
      AnnToggle.setText( "Run Mode" );
   }
   else  // The button takes us to run mode
   {
      LEAD1.setAnnUserMode( (short) LTOCXU.AnnUserModeConstants.ANNUSERMODE_RUN );
      MessageBox.show( "Click on your new button" );
      AnnToggle.setText( "Design Mode" );
   }
}

4. Code the IOToggle control's Click event as follows:

private void IOToggle_click(Object source, Event e)
{
   // Disable errors so that we can trap our own.
   LEAD1.setEnableMethodErrors( false );

   // Use the button to toggle between saving and loading annotations
   if( IOToggle.getText().equals( "Save" ) )
   {
      // Do nothing if there are no annotations.
      if( LEAD1.getAnnContainer() == 0 )
         return;

      //Save the selected annotations.
      short sRet = LEAD1.AnnSave( "c:\\lead\\images\\tmp.ann", (short) LTOCXU.AnnFormatConstants.ANNFMT_NATIVE, true );
      if( sRet == 0 )
      {
         // Make loading the next thing.
         IOToggle.setText( "Load" );
         MessageBox.show( "Use the right mouse button to delete any annotations, then click Load" );
      }
      else
         MessageBox.show( "Error code: " + sRet );
   }
   else  // We are loading annotations
   {
      // Make sure we are in design mode
      LEAD1.setAnnUserMode( (short) LTOCXU.AnnUserModeConstants.ANNUSERMODE_DESIGN );

      // Load the annotations.
      short sRet = LEAD1.AnnLoad( "c:\\lead\\images\\tmp.ann", (short) 1 );
      if( sRet == 0 )
         IOToggle.setText( "Save" );
      else
         MessageBox.show( "No annotations to load" );
   }
}

5. Code the ClipboardToggle control's Click event as follows. (Note that Copy, Cut, and Paste are available as automated popup menu options. This code is for tailoring your application.)

private void ClipboardToggle_click(Object source, Event e)
{
   // Disable errors so that we can trap our own.
   LEAD1.setEnableMethodErrors( false );

   // Use the button to toggle between copying and pasting annotations
   if( ClipboardToggle.getText().equals( "Copy" ) )
   {
      // Do nothing if there are no annotations.
      if( LEAD1.getAnnContainer() == 0 )
         return;

      // Copy the selected annotations to the clipboard.
      short sRet = LEAD1.AnnCopy( (short) LTOCXU.AnnFormatConstants.ANNFMT_NATIVE, true, true );
      if( sRet == 0 )
      {
         // Make pasting the next thing.
         ClipboardToggle.setText( "Paste" );
         MessageBox.show( "Use the right mouse button to delete any annotations, then click Paste" );
      }
      else
         MessageBox.show( "Error code: " + sRet );
   }
   else  // We are pasting annotations
   {
      // Make sure we are in design mode
      LEAD1.setAnnUserMode( (short) LTOCXU.AnnUserModeConstants.ANNUSERMODE_DESIGN );

      // Paste the annotations
      if( LEAD1.getAnnPasteReady() )
      {
         LEAD1.AnnPaste();
         ClipboardToggle.setText( "Copy" );
      }
      else
         MessageBox.show( "No annotations to paste" );
   }
}

6. Code the Realize control's Click event (which renders the annotation objects to the bitmap) as follows:

private void Realize_click(Object source, Event e)
{
   LEAD1.AnnRealize( false );
   LEAD1.ForceRepaint();
   MessageBox.show( "Annotations are now rendered to the bitmap" );
}

7. Code the LEAD1 control's AnnCreate event as follows:

private int m_nNewTag = 0;
private void LEAD1_annCreate(Object source, LTOCXU.LEAD.AnnCreateEvent e)
{
   // Update the tag if we need one.
   if( LEAD1.AnnGetType( e.hObject ) == LTOCXU.AnnObjectConstants.ANNOBJECT_BUTTON ||
      LEAD1.AnnGetType( e.hObject ) == LTOCXU.AnnObjectConstants.ANNOBJECT_HOTSPOT )
   {
      m_nNewTag++;
      LEAD1.AnnSetTag( e.hObject, m_nNewTag );
   }
}

8. Code the LEAD1 control's AnnDrawn event as follows:

private void LEAD1_annDrawn(Object source, LTOCXU.LEAD.AnnDrawnEvent e)
{
   LEAD1.setAnnTool( (short) LTOCXU.AnnToolConstants.ANNTOOL_SELECT );
   MessageBox.show( "Use the right mouse button to modify this object;" + " then click on Run Mode to test it." );
}

9. Code the LEAD1 control's AnnClicked event as follows:

private void LEAD1_annClicked(Object source, LTOCXU.LEAD.AnnClickedEvent e)
{
   MessageBox.show( "This is what we do for the button tagged " + LEAD1.AnnGetTag( e.hObject ) );
}

10. Code the LEAD1 control's AnnDestroy event as follows:

private void LEAD1_annDestroy(Object source, LTOCXU.LEAD.AnnDestroyEvent e)
{
   MessageBox.show( "The object tagged " + LEAD1.AnnGetTag( e.hObject ) + " is deleted." );
}

11. Run your program to test it.