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

Quick Search    Search Deep

Source code: com/arranger/jarl/trait/base/GradientPainter.java


1   package com.arranger.jarl.trait.base;
2   
3   import com.arranger.jarl.base.IContext;
4   import com.arranger.jarl.base.IGradientManager;
5   import com.arranger.jarl.base.IJarlObjectInfo;
6   import com.arranger.jarl.trait.BaseSegmentTrait;
7   import com.arranger.jarl.util.InterpolateUtil;
8   import com.arranger.jarl.util.ObjectUtil;
9   import com.arranger.jarl.util.WidgetConfigSegment;
10  import com.arranger.jarl.util.JarlInfoUtil;
11  import com.arranger.jarl.widget.IWidget;
12  import com.jhlabs.image.Gradient;
13  import org.w3c.dom.Element;
14  
15  import java.awt.*;
16  
17  /**
18   * GradientPainter created on Mar 26, 2003
19   *
20   * @traitAttribute gradientIndex ## xs:integer ## the index of the gradient to use to paint this object
21   *
22   * @traitAttribute startGradientIndex ## xs:integer ## the starting gradient index
23   * @traitAttribute endGradientIndex ## xs:integer ## the ending gradient index
24   */
25  public class GradientPainter extends BaseSegmentTrait {
26  
27      protected int m_gradientIndex = Integer.MAX_VALUE;
28      protected int m_startGradientIndex = Integer.MAX_VALUE;
29      protected int m_endGradientIndex = Integer.MAX_VALUE;
30  
31      /**
32       * Prior to the {@link IWidget#paint} the trait can perform some work
33       * @param widget the widget that is being painted
34       * @param context the current context
35       * @param graphics2D the current graphics object
36       * @return the new graphics, or the same
37       */
38      public Graphics2D prePaint(IWidget widget, IContext context, Graphics2D graphics2D) {
39          store(widget.getColor());
40  
41          IGradientManager gradientManager = context.getGradientManager();
42  
43          Gradient gradient;
44          if (m_gradientIndex != Integer.MAX_VALUE) {
45              gradient = gradientManager.getGradient(m_gradientIndex);
46          } else if (m_startGradientIndex != Integer.MAX_VALUE && m_endGradientIndex != Integer.MAX_VALUE) {
47              //interpolate
48              double currentTimePct = InterpolateUtil.interpolate(widget, context.getTime());
49              Gradient gradient1 = gradientManager.getGradient(m_startGradientIndex);
50              Gradient gradient2 = gradientManager.getGradient(m_endGradientIndex);
51              gradient = gradientManager.interpolate(gradient1, gradient2, currentTimePct);
52          } else {
53              GradientConfigSegment gradientConfigSegment = (GradientConfigSegment) getCurrentSegment(widget, context);
54              double currentTimePct = getCurrentSegmentTimePct(widget, context, gradientConfigSegment);
55  
56              //interpolate
57              Gradient gradient1 = gradientManager.getGradient(gradientConfigSegment.getStartGradientIndex());
58              Gradient gradient2 = gradientManager.getGradient(gradientConfigSegment.getEndGradientIndex());
59              gradient = gradientManager.interpolate(gradient1, gradient2, currentTimePct);
60          }
61  
62          widget.setColor(new GradientColor(gradient));
63  
64          return graphics2D;
65      }
66  
67      /**
68       * After the call to {@link IWidget#paint} is called
69       * the trait can restore some state
70       * @param widget the widget that is being painted
71       * @param context the current context
72       * @param graphics2D the current graphics object
73       * @return the new graphics, or the same
74       */
75      public Graphics2D postPaint(IWidget widget, IContext context, Graphics2D graphics2D) {
76          restorePaint(widget);
77          return graphics2D;
78      }
79  
80      /**
81       * Always remember some attrs might not be there
82       * @param context
83       */
84      protected void initAttributes(IContext context) {
85          super.initAttributes(context);
86  
87          ObjectUtil.initializeField("gradientIndex", m_configElement, this, ObjectUtil.PRIMITIVE_CONVERSION);
88          ObjectUtil.initializeField("startGradientIndex", m_configElement, this, ObjectUtil.PRIMITIVE_CONVERSION);
89          ObjectUtil.initializeField("endGradientIndex", m_configElement, this, ObjectUtil.PRIMITIVE_CONVERSION);
90      }
91  
92      /**
93       * Override this, and for every field that you're using, call {@link #populateInfo}
94       * for example:
95       * <code>
96       *   populateInfo(jarlObjectInfo, "zOrder", "Z-Order", JarlInfoUtil.PRIMITIVE_DISPLAY);
97       * </code>
98       *
99       * @param jarlObjectInfo
100      *
101      * @see JarlInfoUtil#PRIMITIVE_DISPLAY
102      * @see #populateInfo
103      * @see ObjectUtil#initializeField
104      */
105     protected void addJarlObjectInfo(IJarlObjectInfo jarlObjectInfo) {
106         super.addJarlObjectInfo(jarlObjectInfo);
107         populateInfo(jarlObjectInfo, "gradientIndex", "Gradient Index", JarlInfoUtil.PRIMITIVE_DISPLAY);
108         populateInfo(jarlObjectInfo, "startGradientIndex", "Start Gradient Index", JarlInfoUtil.PRIMITIVE_DISPLAY);
109         populateInfo(jarlObjectInfo, "endGradientIndex", "End Gradient Index", JarlInfoUtil.PRIMITIVE_DISPLAY);
110     }
111 
112     public WidgetConfigSegment createSegment(Element element) {
113         return new GradientConfigSegment(element);
114     }
115 
116     public static class GradientConfigSegment extends WidgetConfigSegment {
117 
118         protected int m_startGradientIndex = Integer.MAX_VALUE;
119         protected int m_endGradientIndex = Integer.MAX_VALUE;
120 
121         public GradientConfigSegment(Element element) {
122             super(element);
123 
124             ObjectUtil.initializeFieldStrict("startGradientIndex", element, this, ObjectUtil.PRIMITIVE_CONVERSION);
125             ObjectUtil.initializeFieldStrict("endGradientIndex", element, this, ObjectUtil.PRIMITIVE_CONVERSION);
126         }
127 
128         protected void addJarlObjectInfo(IJarlObjectInfo jarlObjectInfo) {
129             super.addJarlObjectInfo(jarlObjectInfo);
130             populateInfo(jarlObjectInfo, "startGradientIndex", "Start Gradient Index", JarlInfoUtil.PRIMITIVE_DISPLAY);
131             populateInfo(jarlObjectInfo, "endGradientIndex", "End Gradient Index", JarlInfoUtil.PRIMITIVE_DISPLAY);
132         }
133 
134         public int getStartGradientIndex() {
135             return m_startGradientIndex;
136         }
137 
138         public int getEndGradientIndex() {
139             return m_endGradientIndex;
140         }
141     }
142 }
143 
144 
145