Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/arranger/jarl/widget/base/StarField.java


1   package com.arranger.jarl.widget.base;
2   
3   import com.arranger.jarl.widget.BaseSegmentWidget;
4   import com.arranger.jarl.base.IContext;
5   import com.arranger.jarl.base.IJarlObjectInfo;
6   import com.arranger.jarl.util.*;
7   
8   import java.awt.*;
9   import java.awt.geom.GeneralPath;
10  import java.awt.geom.Ellipse2D;
11  import java.util.Map;
12  import java.util.HashMap;
13  
14  import org.w3c.dom.Element;
15  
16  /**
17   * StarField creates a field of stars
18   *
19   * @widgetAttribute density ## xs:string ## the density of stars (0% to 100%)
20   * @widgetAttribute startDensity ## xs:string ## the starting density of stars (0% to 100%)
21   * @widgetAttribute endDensity ## xs:string ## the ending density of stars (0% to 100%)
22   */
23  public class StarField extends BaseSegmentWidget {
24  
25      protected double m_density = Double.NEGATIVE_INFINITY;
26      protected double m_startDensity = Double.NEGATIVE_INFINITY;
27      protected double m_endDensity = Double.NEGATIVE_INFINITY;
28  
29      protected transient Map m_cachedShapes = new HashMap();
30  
31      /**
32       * Called from within {@link #paint}
33       *
34       * @param context
35       * @param graphics2D
36       */
37      protected void _paint(IContext context, Graphics2D graphics2D) {
38  
39          double density;
40          if (m_density != Double.NEGATIVE_INFINITY) {
41              density = m_density;
42          } else if (m_startDensity != Double.NEGATIVE_INFINITY && m_endDensity != Double.NEGATIVE_INFINITY) {
43              density = InterpolateUtil.interpolate(getStartTime(),
44                      getEndTime(),
45                      m_startDensity,
46                      m_endDensity,
47                      context.getTime());
48          } else {
49              StarFieldConfigSegment starFieldConfigSegment = (StarFieldConfigSegment) getCurrentSegment(context);
50              double currentTimePct = getCurrentSegmentTimePct(context, starFieldConfigSegment);
51  
52              density = InterpolateUtil.interpolate(0,
53                      1,
54                      starFieldConfigSegment.getStartDensity(),
55                      starFieldConfigSegment.getEndDensity(),
56                      currentTimePct);
57          }
58  
59          Object key = new Integer(context.getTime().getFrame());
60          Shape shape = (Shape) m_cachedShapes.get(key);
61  
62          if (shape == null) {
63              GeneralPath generalPath = new GeneralPath();
64              int width = (int) getWidth(context);
65              int height = (int) getHeight(context);
66              for (int y = 0; y < height; y++) {
67                  for (int x = 0; x < width; x++) {
68                      if (RandomUtil.inRange(density)) {
69                          generalPath.append(new Ellipse2D.Double(x, y, 1, 1), false);
70                      }
71                  }
72              }
73              m_cachedShapes.put(key, generalPath);
74              shape = generalPath;
75          }
76          paintShape(context, graphics2D, centerShape(shape, context));
77      }
78  
79      /**
80       * Always remember some attrs might not be there
81       * @param context
82       */
83      protected void initAttributes(IContext context) {
84          super.initAttributes(context);
85  
86          ObjectUtil.initializeField("density", m_configElement, this, ObjectUtil.NORMALIZING_CONVERSION);
87          ObjectUtil.initializeField("startDensity", m_configElement, this, ObjectUtil.NORMALIZING_CONVERSION);
88          ObjectUtil.initializeField("endDensity", m_configElement, this, ObjectUtil.NORMALIZING_CONVERSION);
89      }
90  
91      protected void addJarlObjectInfo(IJarlObjectInfo jarlObjectInfo) {
92          super.addJarlObjectInfo(jarlObjectInfo);
93          populateInfo(jarlObjectInfo, "density", "Density", JarlInfoUtil.PRIMITIVE_DISPLAY);
94          populateInfo(jarlObjectInfo, "startDensity", "Start Density", JarlInfoUtil.PRIMITIVE_DISPLAY);
95          populateInfo(jarlObjectInfo, "endDensity", "End Density", JarlInfoUtil.PRIMITIVE_DISPLAY);
96      }
97  
98      /**
99       * Create a concrete {@link WidgetConfigSegment} based on this element
100      * @param element
101      * @return a specific {@link WidgetConfigSegment}
102      */
103     public WidgetConfigSegment createSegment(Element element) {
104         return new StarFieldConfigSegment(element);
105     }
106 
107     public static class StarFieldConfigSegment extends WidgetConfigSegment {
108 
109         protected double m_startDensity = Double.NEGATIVE_INFINITY;
110         protected double m_endDensity = Double.NEGATIVE_INFINITY;
111 
112         public StarFieldConfigSegment(Element element) {
113             super(element);
114 
115             ObjectUtil.initializeFieldStrict("startDensity", element, this, ObjectUtil.NORMALIZING_CONVERSION);
116             ObjectUtil.initializeFieldStrict("endDensity", element, this, ObjectUtil.NORMALIZING_CONVERSION);
117         }
118 
119         protected void addJarlObjectInfo(IJarlObjectInfo jarlObjectInfo) {
120             super.addJarlObjectInfo(jarlObjectInfo);
121             populateInfo(jarlObjectInfo, "startDensity", "Start Density", JarlInfoUtil.PRIMITIVE_DISPLAY);
122             populateInfo(jarlObjectInfo, "endDensity", "End Density", JarlInfoUtil.PRIMITIVE_DISPLAY);
123         }
124 
125         public double getStartDensity() {
126             return m_startDensity;
127         }
128 
129         public double getEndDensity() {
130             return m_endDensity;
131         }
132     }
133 }