Source code: com/arranger/jarl/widget/base/Text.java
1 package com.arranger.jarl.widget.base;
2
3 import com.arranger.jarl.base.IContext;
4 import com.arranger.jarl.base.IJarlObjectInfo;
5 import com.arranger.jarl.util.ObjectUtil;
6 import com.arranger.jarl.util.JarlInfoUtil;
7 import com.arranger.jarl.widget.BaseWidget;
8
9 import java.awt.*;
10 import java.awt.font.FontRenderContext;
11 import java.lang.reflect.Field;
12
13 /**
14 * Text draws text on the screen.
15 *
16 * Required attributes: (should be percentages: eg. 10%)
17 * width
18 * height
19 * text
20 * fontName
21 * fontSize
22 * fontStyle (PLAIN, BOLD, ITALIC)
23 *
24 * @see GraphicsEnvironment#getAllFonts
25 * @see Font
26 * @see FontRenderContext
27 *
28 * @widgetAttribute text ## xs:string ## the text to draw
29 * @widgetAttribute fontName ## xs:string ## the name of the font (eg arial)
30 * @widgetAttribute fontStyle ## xs:string ## the style to draw (bold|plain|italic)
31 * @widgetAttribute fontSize ## xs:float ## the size of the font
32 */
33 public class Text extends BaseWidget {
34
35 protected String m_text;
36 protected String m_fontName;
37 protected int m_fontStyle;
38 protected int m_fontSize = 20;
39
40 /**
41 * Called from within {@link #paint}
42 *
43 * @param context
44 * @param graphics2D
45 */
46 protected void _paint(IContext context, Graphics2D graphics2D) {
47 FontRenderContext fontRenderContext = graphics2D.getFontRenderContext();
48 Font font = new Font(m_fontName, m_fontStyle, m_fontSize);
49
50 Shape shape = font.createGlyphVector(fontRenderContext, m_text).getOutline();
51 paintShape(context, graphics2D, centerShape(shape, context));
52 }
53
54 /**
55 * Always remember some attrs might not be there
56 * @param context
57 */
58 protected void initAttributes(IContext context) {
59 super.initAttributes(context);
60
61 ObjectUtil.initializeField("text", m_configElement, this, ObjectUtil.PRIMITIVE_CONVERSION);
62 ObjectUtil.initializeField("fontName", m_configElement, this, ObjectUtil.PRIMITIVE_CONVERSION);
63 ObjectUtil.initializeField("fontSize", m_configElement, this, ObjectUtil.PRIMITIVE_CONVERSION);
64 ObjectUtil.initializeField("fontStyle", m_configElement, this, new ObjectUtil.PrimitiveConversionFunction() {
65 public void setField(Field field, String configData, Object object) throws Exception {
66 int result = ObjectUtil.getNamedValue(Font.class, configData);
67 super.setField(field, String.valueOf(result), object);
68 }
69 });
70 }
71
72 protected void addJarlObjectInfo(IJarlObjectInfo jarlObjectInfo) {
73 super.addJarlObjectInfo(jarlObjectInfo);
74 populateInfo(jarlObjectInfo, "text", "Text", JarlInfoUtil.PRIMITIVE_DISPLAY);
75 populateInfo(jarlObjectInfo, "fontName", "Font Name", JarlInfoUtil.PRIMITIVE_DISPLAY);
76 populateInfo(jarlObjectInfo, "fontSize", "Font Size", JarlInfoUtil.PRIMITIVE_DISPLAY);
77 populateInfo(jarlObjectInfo, "fontStyle", "Font Style", JarlInfoUtil.PRIMITIVE_DISPLAY); //won't fully work, but that's ok
78 }
79 }