Results 1 to 5 of 5

Thread: Vertical Alignment on ScreenAnnotation

  1. #1
    Junior Member
    Join Date
    May 2010
    Location
    Layton, UT
    Posts
    25

    Question Vertical Alignment on ScreenAnnotation

    So I have a ScreenRelativeAnnotation object and for the project I need to set a certain size for it instead of just letting it wrap the text. Which works fine except for one thing, the text is no longer centered vertically, and I can't for the life of me figure out how to fix that?

    Here's a simple example:

    Code:
    import gov.nasa.worldwind.BasicModel;
    import gov.nasa.worldwind.awt.WorldWindowGLCanvas;
    import gov.nasa.worldwind.layers.AnnotationLayer;
    import gov.nasa.worldwind.render.ScreenRelativeAnnotation;
    
    import java.awt.Dimension;
    
    import javax.swing.JFrame;
    
    public class RelativeAnnotationTest {
    
    	public static void main(String[] args) {
    
    		JFrame frame = new JFrame();
    
    		WorldWindowGLCanvas worldWindow = new WorldWindowGLCanvas();
    		worldWindow.setPreferredSize(new Dimension(600, 600));
    		worldWindow.setModel(new BasicModel());
    
    		AnnotationLayer annotationLayer = new AnnotationLayer();
    		worldWindow.getModel().getLayers().add(annotationLayer);
    
    		{
    			ScreenRelativeAnnotation annotation = new ScreenRelativeAnnotation("Tall", 0, .5);
    			annotation.getAttributes().setSize(new Dimension(100, 100));
    			annotationLayer.addAnnotation(annotation);
    		}
    
    		{
    			ScreenRelativeAnnotation annotation = new ScreenRelativeAnnotation("Normal", 1, .5);
    			annotationLayer.addAnnotation(annotation);
    		}
    
    		frame.add(worldWindow);
    		frame.pack();
    		frame.setVisible(true);
    	}
    
    }
    If you run it you can see how normally the annotation wraps the text so the text is centered (right side), but when I set a custom hard coded height (left side) the text is top aligned. Any help?

  2. #2
    Junior Member
    Join Date
    May 2010
    Location
    Layton, UT
    Posts
    25

    Default

    Been digging through the source code of AbstractAnnotation and MultiLineTextRenderer and it's not looking good... I don't think there is anything that affects the y location where the text is being drawn. Bleh. Might have to add this myself! Still open to suggestions!

  3. #3
    Junior Member
    Join Date
    May 2010
    Location
    Layton, UT
    Posts
    25

    Default

    Alright well for future reference in case anybody else runs into this, I ended up having to implement a vertical alignment myself in the AbstractAnnotation and MultiLineRenderer classes to get this to work. I'm using HTML for my text so I only implemented it in the drawHTML call, not the drawPlainText call. But yeah.

  4. #4
    Senior Member heidtmare's Avatar
    Join Date
    Feb 2008
    Location
    Melbourne, FL, USA
    Posts
    676

    Default

    not exactly answering your question, but here is an info box i implemented on a previous application.

    PHP Code:
    package REMOVED;

    import gov.nasa.worldwind.avlist.AVKey;
    import gov.nasa.worldwind.layers.AnnotationLayer;
    import gov.nasa.worldwind.render.AnnotationAttributes;
    import gov.nasa.worldwind.render.DrawContext;
    import gov.nasa.worldwind.render.ScreenAnnotation;
    import java.awt.Color;
    import java.awt.Insets;
    import java.awt.Point;

    /**
     *
     * @author heidtmare
     */
    public class InformationBoxLayer extends AnnotationLayer {
        
        private final 
    ScreenAnnotation sa;

        
    /**
         *
         */
        
    public InformationBoxLayer() {
            
    setName("Information Box");
            
    AnnotationAttributes attrs = new AnnotationAttributes();
            
    attrs.setAdjustWidthToText(AVKey.SIZE_FIT_TEXT);
            
    attrs.setBackgroundColor(new Color(0f0f0f.5f));
            
    attrs.setBorderWidth(0);
            
    attrs.setCornerRadius(0);
            
    attrs.setInsets(new Insets(8888));
            
    attrs.setTextAlign(AVKey.CENTER);
            
    attrs.setTextColor(Color.WHITE);
            
            
    sa = new ScreenAnnotation("", new Point(00), attrs);
            
    this.addAnnotation(sa);
            
    this.setPickEnabled(false);
        }

        
    /**
         *
         * @param info
         */
        
    public void setInformation(String info) {
            
    sa.setText(info);
        }

        
    /**
         * 
         * @param dc
         */
        
    @Override
        
    protected void doRender(DrawContext dc) {
            if (!
    sa.getText().isEmpty()) {
                
    Point centerScreenPoint dc.getViewportCenterScreenPoint();
                
    sa.setScreenPoint(new Point(centerScreenPoint.x20));
                
    super.doRender(dc);
            }
        }