Source code: com/arranger/jarl/widget/base/Line.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.*;
6 import com.arranger.jarl.widget.BaseSegmentWidget;
7 import org.w3c.dom.Element;
8
9 import java.awt.*;
10 import java.awt.geom.Line2D;
11 import java.awt.geom.Point2D;
12
13 /**
14 * Line draws a line
15 *
16 * @widgetAttribute coords ## xs:string ## the coords of this line relative to the screen (eg x1, y1, x2, y2) (50%, 10%, 50%, 90%)
17 *
18 * @widgetAttribute startCoords ## xs:string ## the starting coords of this line relative to the screen
19 * @widgetAttribute endCoords ## xs:string ## the ending coords of this line relative to the screen
20 */
21 public class Line extends BaseSegmentWidget {
22
23 protected Point2D[] m_coords;
24 protected Point2D[] m_startCoords;
25 protected Point2D[] m_endCoords;
26
27 /**
28 * Called from within {@link #paint}
29 *
30 * @param context
31 * @param graphics2D
32 */
33 protected void _paint(IContext context, Graphics2D graphics2D) {
34
35 double x1, y1, x2, y2;
36
37 if (m_coords != null) {
38 x1 = m_coords[0].getX();
39 y1 = m_coords[0].getY();
40 x2 = m_coords[1].getX();
41 y2 = m_coords[1].getY();
42 } else if (m_startCoords != null && m_endCoords != null) {
43
44 x1 = InterpolateUtil.interpolate(getStartTime(),
45 getEndTime(),
46 m_startCoords[0].getX(),
47 m_endCoords[0].getX(),
48 context.getTime());
49
50 y1 = InterpolateUtil.interpolate(getStartTime(),
51 getEndTime(),
52 m_startCoords[0].getY(),
53 m_endCoords[0].getY(),
54 context.getTime());
55
56 x2 = InterpolateUtil.interpolate(getStartTime(),
57 getEndTime(),
58 m_startCoords[1].getX(),
59 m_endCoords[1].getX(),
60 context.getTime());
61
62 y2 = InterpolateUtil.interpolate(getStartTime(),
63 getEndTime(),
64 m_startCoords[1].getY(),
65 m_endCoords[1].getY(),
66 context.getTime());
67
68 } else {
69
70 LineConfigSegment lineConfigSegment = (LineConfigSegment) getCurrentSegment(context);
71 double currentTimePct = getCurrentSegmentTimePct(context, lineConfigSegment);
72
73 x1 = InterpolateUtil.interpolate(0,
74 1,
75 lineConfigSegment.getStartCoords()[0].getX(),
76 lineConfigSegment.getEndCoords()[0].getX(),
77 currentTimePct);
78
79 y1 = InterpolateUtil.interpolate(0,
80 1,
81 lineConfigSegment.getStartCoords()[0].getY(),
82 lineConfigSegment.getEndCoords()[0].getY(),
83 currentTimePct);
84
85 x2 = InterpolateUtil.interpolate(0,
86 1,
87 lineConfigSegment.getStartCoords()[1].getX(),
88 lineConfigSegment.getEndCoords()[1].getX(),
89 currentTimePct);
90
91 y2 = InterpolateUtil.interpolate(0,
92 1,
93 lineConfigSegment.getStartCoords()[1].getY(),
94 lineConfigSegment.getEndCoords()[1].getY(),
95 currentTimePct);
96 }
97
98 Line2D line2D = new Line2D.Double(
99 x1 * context.getWidth(),
100 y1 * context.getHeight(),
101 x2 * context.getWidth(),
102 y2 * context.getHeight()
103 );
104 paintShape(context, graphics2D, line2D);
105 }
106
107 /**
108 * Always remember some attrs might not be there
109 * @param context
110 */
111 protected void initAttributes(IContext context) {
112 super.initAttributes(context);
113
114 ObjectUtil.initializeField("coords", m_configElement, this, ObjectUtil.COORDS_CONVERSION);
115 ObjectUtil.initializeField("startCoords", m_configElement, this, ObjectUtil.COORDS_CONVERSION);
116 ObjectUtil.initializeField("endCoords", m_configElement, this, ObjectUtil.COORDS_CONVERSION);
117 }
118
119 protected void addJarlObjectInfo(IJarlObjectInfo jarlObjectInfo) {
120 super.addJarlObjectInfo(jarlObjectInfo);
121 populateInfo(jarlObjectInfo, "coords", "Coordinates", JarlInfoUtil.COORDS_DISPLAY);
122 populateInfo(jarlObjectInfo, "startCoords", "Start Coordinates", JarlInfoUtil.COORDS_DISPLAY);
123 populateInfo(jarlObjectInfo, "endCoords", "End Coordinates", JarlInfoUtil.COORDS_DISPLAY);
124 }
125
126 /**
127 * Create a concrete {@link WidgetConfigSegment} based on this element
128 * @param element
129 * @return a specific {@link WidgetConfigSegment}
130 */
131 public WidgetConfigSegment createSegment(Element element) {
132 return new LineConfigSegment(element);
133 }
134
135 public static class LineConfigSegment extends WidgetConfigSegment {
136
137 protected Point2D[] m_startCoords;
138 protected Point2D[] m_endCoords;
139
140 public LineConfigSegment(Element element) {
141 super(element);
142
143 ObjectUtil.initializeFieldStrict("startCoords", element, this, ObjectUtil.COORDS_CONVERSION);
144 ObjectUtil.initializeFieldStrict("endCoords", element, this, ObjectUtil.COORDS_CONVERSION);
145 }
146
147 protected void addJarlObjectInfo(IJarlObjectInfo jarlObjectInfo) {
148 super.addJarlObjectInfo(jarlObjectInfo);
149 populateInfo(jarlObjectInfo, "startCoords", "Start Coordinates", JarlInfoUtil.COORDS_DISPLAY);
150 populateInfo(jarlObjectInfo, "endCoords", "End Coordinates", JarlInfoUtil.COORDS_DISPLAY);
151 }
152
153 public Point2D[] getStartCoords() {
154 return m_startCoords;
155 }
156
157 public Point2D[] getEndCoords() {
158 return m_endCoords;
159 }
160 }
161 }