Java example: TryAnnotations

This example program shows how to control various annotation objects via the Java API.

Running the program

java -cp . TryAnnotations -dv -stay

Program source code

import java.lang.ArrayIndexOutOfBoundsException;
import java.util.Vector;
import llnl.visit.Axes3D;
import llnl.visit.View3DAttributes;
import llnl.visit.AnnotationAttributes;
import llnl.visit.AnnotationObject;
import llnl.visit.AnnotationObjectList;
import llnl.visit.ColorAttribute;
import llnl.visit.SaveWindowAttributes;

public class TryAnnotations extends RunViewer
{
    public TryAnnotations()
    {
        super();
    }

    protected void SetCustomDefaultAnnotations()
    {
        // Change some annotation attributes.
        AnnotationAttributes a = viewer.GetViewerState().GetAnnotationAttributes();
        a.SetBackgroundMode(AnnotationAttributes.BACKGROUNDMODE_GRADIENT);
        a.SetGradientBackgroundStyle(AnnotationAttributes.GRADIENTSTYLE_RADIAL);
        a.SetGradientColor1(new ColorAttribute(0,0,255));
        a.SetGradientColor2(new ColorAttribute(0,0,0));
        a.SetForegroundColor(new ColorAttribute(255,255,255));
        Axes3D a3d = new Axes3D(a.GetAxes3D());
        a3d.SetAxesType(Axes3D.AXES_STATICEDGES);
        a3d.SetVisible(true);
        a.SetAxes3D(a3d);
        a.Notify();
        viewer.GetViewerMethods().SetAnnotationAttributes();
    }

    protected void work(String[] args)
    {
        // Try and open a database
        if(viewer.GetViewerMethods().OpenDatabase(viewer.GetDataPath() + "wave*.silo database"))
        {
            viewer.GetViewerMethods().AddPlot("Pseudocolor", "pressure");
            viewer.GetViewerMethods().AddPlot("Mesh", "quadmesh");
            viewer.GetViewerMethods().DrawPlots();

            // Set a 3D view.
            View3DAttributes v = viewer.GetViewerState().GetView3DAttributes();
            v.SetViewNormal(-0.705386, 0.57035, 0.42087);
            v.SetFocus(5, 0.353448, 2.5);
            v.SetViewUp(0.49514, 0.821357, -0.283213);
            v.SetViewAngle(30.);
            v.SetParallelScale(5.6009);
            v.SetNearPlane(-11.2018);
            v.SetFarPlane(11.2018);
            v.SetImagePan(0.0300266, 0.0519825);
            v.SetImageZoom(1.10796);
            v.SetPerspective(true);
            v.SetEyeAngle(2.);
            v.SetCenterOfRotationSet(false);
            v.SetCenterOfRotation(5, 0.353448, 2.5);
            v.Notify();
            viewer.GetViewerMethods().SetView3D();

            SetCustomDefaultAnnotations();

            AnnotationObjectList aol = viewer.GetViewerState().GetAnnotationObjectList();

            //
            // Set up a time slider annotation.
            //
            viewer.GetViewerMethods().AddAnnotationObject(AnnotationObject.ANNOTATIONTYPE_TIMESLIDER, "timeSlider");
            aol.SetTimeSliderOptions("timeSlider",
                0.675, 0.01, 0.3, 0.1,
                "Wave time = $time", "%1.3f",
                new ColorAttribute(255,0,0,255), new ColorAttribute(255,255,0,255),
                new ColorAttribute(0,255,0,255), false,
                0,
                true, false, false);
            aol.Notify();
            viewer.GetViewerMethods().SetAnnotationObjectOptions();
            // Advance through time to test the time slider.
            viewer.GetViewerMethods().SetTimeSliderState(30);

            System.out.println("After time slider: " + viewer.GetViewerState().GetAnnotationObjectList().toString());

            //
            // Create a 2D text annotation
            //
            viewer.GetViewerMethods().AddAnnotationObject(AnnotationObject.ANNOTATIONTYPE_TEXT2D, "text");
            aol.SetText2DOptions("text", 0.4, 0.95, 0.05,
                "Wave simulation",
                new ColorAttribute(255,0,0,255), false, 
                2, true, true, true, true);
            aol.Notify();
            viewer.GetViewerMethods().SetAnnotationObjectOptions();
            System.out.println("After 2D text: " + viewer.GetViewerState().GetAnnotationObjectList().toString());

            //
            // Create a 3D text annotation
            //
            viewer.GetViewerMethods().AddAnnotationObject(AnnotationObject.ANNOTATIONTYPE_TEXT3D, "text3D");
            aol.SetText3DOptions("text3D",
                0., 2.5, 1.5, 
                "Wave simulation, 3D text",
                true, 0., 3,
                true,
                15., 0., 0.,
                new ColorAttribute(255,255,0,255), false, 
                true);
            aol.Notify();
            viewer.GetViewerMethods().SetAnnotationObjectOptions();
            System.out.println("After 3D text: " + viewer.GetViewerState().GetAnnotationObjectList().toString());

            //
            // Create a line 2D/arrow annotation
            //
            viewer.GetViewerMethods().AddAnnotationObject(AnnotationObject.ANNOTATIONTYPE_LINE2D, "line");
            aol.SetLine2DOptions("line",
                0.5, 0.9495, 0.5, 0.6,
                2,
                0,
                2,
                new ColorAttribute(255,0,0,255), false, 
                true);
            aol.Notify();
            viewer.GetViewerMethods().SetAnnotationObjectOptions();
            System.out.println("After line: " + viewer.GetViewerState().GetAnnotationObjectList().toString());

            //
            // Save a small image to use for an annotation.
            //
            AnnotationAttributes annot = viewer.GetViewerState().GetAnnotationAttributes();
            ColorAttribute transColor = new ColorAttribute(50,0,100,255);
            annot.SetBackgroundColor(transColor);
            annot.SetBackgroundMode(annot.BACKGROUNDMODE_SOLID);
            annot.Notify();
            viewer.GetViewerMethods().SetAnnotationAttributes();
            SaveImage("imageannot.png", 300, 300);
            annot.SetBackgroundColor(new ColorAttribute(0,0,0,255));
            annot.Notify();
            viewer.GetViewerMethods().SetAnnotationAttributes();

            viewer.GetViewerMethods().AddAnnotationObject(AnnotationObject.ANNOTATIONTYPE_IMAGE, "image");
            aol.SetImageOptions("image",
                "imageannot.png",
                0.02, 0.63,
                1., 1., true,
                transColor, true,
                1.,
                true);
            aol.Notify();
            viewer.GetViewerMethods().SetAnnotationObjectOptions();
            System.out.println("After image: " + viewer.GetViewerState().GetAnnotationObjectList().toString());

            // Set some legend attributes. You'd get the name from the PlotList object
            // but here we're just hard-coding the plot name since the Pseudocolor is
            // called Plot0000.
            SetCustomDefaultAnnotations();
            SetLegendOptions("Plot0000",
                false, // managePosition,
                0.2, 0.1, // x,y
                1.5, 0.5, // scaleX, scaleY
                7, // numTicks
                true, // drawBox
                false, // drawLabels,
                true,  // horizontalLegend,
                true, // alternateText, (false=normal text position, true=opposite position)
                false, // drawTitle,
                false, // drawMinMax,
                true, // controlTicks,
                true, // minMaxInclusive,
                true, // drawValues
                0.03, // fontheight
                new ColorAttribute(100,255,100), false,
                2, true, true, true,
                true);
            aol.Notify();
            viewer.GetViewerMethods().SetAnnotationObjectOptions();
        }
        else
            System.out.println("Could not open the database!");
    }

    private final static int LEGEND_MANAGE_POSITION  =0;
    private final static int LEGEND_DRAW_BOX         =1;
    private final static int LEGEND_DRAW_LABELS      =2;
    private final static int LEGEND_ORIENTATION0     =3;
    private final static int LEGEND_ORIENTATION1     =4;
    private final static int LEGEND_DRAW_TITLE       =5;
    private final static int LEGEND_DRAW_MINMAX      =6;
    private final static int LEGEND_CONTROL_TICKS    =7;
    private final static int LEGEND_MINMAX_INCLUSIVE =8;
    private final static int LEGEND_DRAW_VALUES      =9;

    private final static int LEGEND_TYPE_VARIABLE    =0;
    private final static int LEGEND_TYPE_LEVELS      =1;
    private final static int LEGEND_TYPE_OTHER       =2;

    private void SetLegendBit(AnnotationObject annot, int bit, boolean val)
    {
        int shifted = (1 << bit);
        int mask = ~shifted;
        int data = annot.GetIntAttribute1();
        annot.SetIntAttribute1((data & mask) | (val ? shifted : 0)); 
    }

    // Based on avtLegendAttributeColleague.C
    private void SetLegendOptions(String annotName, 
         boolean managePosition,
         double  x, double y,
         double  scaleX,
         double  scaleY,
         int     numTicks,
         boolean drawBox,
         boolean drawLabels,
         boolean horizontalLegend,
         boolean alternateText,
         boolean drawTitle,
         boolean drawMinMax,
         boolean controlTicks,
         boolean minMaxInclusive,
         boolean drawValues,
         double fontHeight,
         ColorAttribute textColor, boolean useForegroundColor, 
         int fontFamily, boolean bold, boolean italic, boolean shadow,
         boolean visible)      
    {
        AnnotationObjectList aol = viewer.GetViewerState().GetAnnotationObjectList();
        AnnotationObject annot = aol.GetAnnotation(annotName);
        if(annot != null)
        {
            SetLegendBit(annot, LEGEND_MANAGE_POSITION, managePosition);
            SetLegendBit(annot, LEGEND_DRAW_BOX, drawBox);
            SetLegendBit(annot, LEGEND_DRAW_LABELS, drawLabels);
            SetLegendBit(annot, LEGEND_ORIENTATION0, horizontalLegend);
            SetLegendBit(annot, LEGEND_ORIENTATION1, alternateText);
            SetLegendBit(annot, LEGEND_DRAW_TITLE, drawTitle);
            SetLegendBit(annot, LEGEND_DRAW_MINMAX, drawMinMax);
            SetLegendBit(annot, LEGEND_CONTROL_TICKS, controlTicks);
            SetLegendBit(annot, LEGEND_MINMAX_INCLUSIVE, minMaxInclusive);
            SetLegendBit(annot, LEGEND_DRAW_VALUES, drawValues);

            annot.SetPosition(x, y, 0.);
            annot.SetPosition2(scaleX, scaleY, 0.);

            annot.SetIntAttribute2(numTicks);

            annot.SetDoubleAttribute1(fontHeight);
            annot.SetTextColor(textColor);
            annot.SetUseForegroundForTextColor(useForegroundColor);
            annot.SetFontFamily(fontFamily);
            annot.SetFontBold(bold);
            annot.SetFontItalic(italic);
            annot.SetFontShadow(shadow);
            annot.SetVisible(visible);
        }
    }

    private void SaveImage(String filename, int xres, int yres)
    {
        viewer.GetViewerMethods().SetAnnotationAttributes();
        SaveWindowAttributes saveAtts = viewer.GetViewerState().GetSaveWindowAttributes();
        saveAtts.SetFileName(filename);
        saveAtts.SetWidth(xres);
        saveAtts.SetHeight(yres);
        saveAtts.SetFamily(false);
        saveAtts.SetFormat(saveAtts.FILEFORMAT_PNG);
        saveAtts.SetResConstraint(saveAtts.RESCONSTRAINT_NOCONSTRAINT);
        saveAtts.Notify();
        viewer.GetViewerMethods().SaveWindow();
    }

    public static void main(String args[])
    {
        TryAnnotations r = new TryAnnotations();
        r.run(args);
    }
}