View Javadoc

1   // Copyright 2004-2005, FreeHEP.
2   package hep.wired.heprep.representation;
3   
4   import java.awt.*;
5   import java.util.*;
6   
7   import org.freehep.graphics2d.TagString;
8   import org.freehep.graphics2d.VectorGraphics;
9   import hep.graphics.heprep.HepRepInstance;
10  import hep.graphics.heprep.HepRepPoint;
11  import hep.graphics.heprep.util.HepRepFont;
12  
13  import hep.wired.heprep.services.GraphicsMode;
14  import hep.wired.heprep.services.Attributes;
15  import hep.wired.heprep.services.Projection;
16  import hep.wired.heprep.services.DrawAs;
17  import hep.wired.viewport.RectangularViewPort;
18  import hep.wired.services.ViewPort;
19  
20  /***
21   * Draws text in the plot, positioned using "HPos" and "VPos" and aligned using "HAlign" and 
22   * "VAlign".
23   * @author Mark Donszelmann
24   * @version $Id: DrawAsText.java 645 2005-02-27 02:23:41Z duns $
25   */
26  
27  public class DrawAsText extends AbstractDrawAs {
28  
29      public String getKey() {
30          return "text";
31      }
32  
33      public String getName() {
34          return "Text";
35      }
36      
37      public String getDescription() {
38          return "Draws Text.";
39      }
40      
41      public void draw(VectorGraphics graphics, HepRepInstance instance,
42                       Attributes atts, GraphicsMode mode, 
43                       Projection projection, ViewPort viewPort) {
44  
45  
46          Font font = graphics.getFont();
47          Color color = graphics.getColor();
48          graphics.setFont(new Font(instance.getAttValue("fontName").getString(), 
49                                    HepRepFont.getStyle(instance.getAttValue("fontStyle").getString()), 
50                                    instance.getAttValue("fontSize").getInteger()));
51          graphics.setColor(instance.getAttValue("fontColor").getColor());
52          TagString string = new TagString(instance.getAttValue("text").getString());
53  
54          if (viewPort instanceof RectangularViewPort) {
55              RectangularViewPort rvp = (RectangularViewPort)viewPort;
56      
57              graphics.drawString(string,
58                                  instance.getAttValue("hPos").getDouble()*rvp.getComponentWidth(),
59                                  instance.getAttValue("vPos").getDouble()*rvp.getComponentHeight(),
60                                  VectorGraphics.getTextAlignment(instance.getAttValue("hAlign").getString()), 
61                                  VectorGraphics.getTextAlignment(instance.getAttValue("vAlign").getString()),
62                                  instance.getAttValue("fontHasFrame").getBoolean(), 
63                                  instance.getAttValue("fontFrameColor").getColor(), 
64                                  instance.getAttValue("fontFrameWidth").getDouble(),
65                                  instance.getAttValue("fontHasBanner").getBoolean(), 
66                                  instance.getAttValue("fontBannerColor").getColor());
67          }
68          
69          graphics.setColor(color);
70          graphics.setFont(font);
71      }
72  
73      public void frame(VectorGraphics graphics, HepRepInstance instance,
74                        Attributes atts, GraphicsMode mode, 
75                        Projection projection, ViewPort viewPort) {
76  
77          // Framing done in draw
78      }
79  }