1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.gwtwidgets.client.wrap;
18
19 import java.util.ArrayList;
20 import org.gwtwidgets.client.style.Color;
21 import org.gwtwidgets.client.style.Coords;
22 import org.gwtwidgets.client.util.ArrayUtils;
23 import org.gwtwidgets.client.wwrapper.ElementNotFoundException;
24 import org.gwtwidgets.client.wwrapper.WBuilder;
25 import com.google.gwt.core.client.JavaScriptObject;
26 import com.google.gwt.user.client.DOM;
27 import com.google.gwt.user.client.Element;
28 import com.google.gwt.user.client.ui.HTMLPanel;
29
30
31 /***
32 * Wrapper Panel for Walter Zorn's JsGraphics library. To use this wrapper
33 * you will need to download the JsGraphics library from
34 * http://www.walterzorn.com/jsgraphics/jsgraphics_e.htm. This code was
35 * built and tested for JsGraphics version 2.35.
36 *
37 * To use this panel you will need top include the JsGraphics library
38 * in the <head> of the HTMl page.
39 *
40 * <script src="script/wz_jsgraphics.js" type="text/javascript"></script>
41 *
42 * You will also need to place a DIV in the HTML page with an ID and must specify
43 * the "position:relative" style attribute. If you don't specify this you
44 * will find that when you draw shapes, they will be relative to the top-left
45 * corner of the page, and not relative to the placement of the DIV.
46 *
47 * <div style="position:relative;width:350px;height:300px;" id="g"></div>
48 *
49 * In your code you will create the JsGraphicsPanel, passing the ID
50 * of the DIV that will be used as your canvas. You DO NOT need to add
51 * this panel to the RootPanel, although there (seems) to be no harm
52 * in doing so.
53 */
54 public class JsGraphicsPanel extends HTMLPanel
55 {
56
57 private JavaScriptObject graphics;
58
59
60 public final static Style PLAIN = new Style("font-weight:normal;");
61 public final static Style BOLD = new Style("font-weight:bold;");
62 public final static Style ITALIC = new Style("font-style:italic;");
63 public final static Style ITALIC_BOLD = new Style("font-style:italic;" + "font-weight:bold;");
64 public final static Style BOLD_ITALIC = ITALIC_BOLD;
65
66
67
68 /***
69 * @param id ID of the DIV element to turn into a canvas.
70 * @throws ElementNotFoundException
71 */
72 public JsGraphicsPanel (String id) throws ElementNotFoundException
73 {
74 super("");
75 Element e = DOM.getElementById(id);
76 if (e == null) {
77 throw new ElementNotFoundException(id);
78 }
79 setElement(e);
80 WBuilder.resetElement(this);
81 this.graphics = newJsGraphics(id);
82 }
83
84
85 private native JavaScriptObject newJsGraphics (String id)
86
87 ;
88
89
90 public void setColor (Color color)
91 {
92 setColor(graphics, color.getHexValue());
93 }
94
95 private native void setColor (JavaScriptObject g, String color)
96
97 ;
98
99
100
101 public native void setStrokeWidth (int width)
102
103 ;
104
105 public native void setStrokeDotted ()
106
107 ;
108
109 public native void drawLine (int x1, int y1, int x2, int y2)
110
111 ;
112
113
114
115 public void drawPolyline (int[] xPoints, int[] yPoints)
116 {
117 drawPolyline(graphics, ArrayUtils.toJsArray(xPoints), ArrayUtils.toJsArray(yPoints));
118 }
119
120 private native void drawPolyline (JavaScriptObject g, JavaScriptObject xPoints, JavaScriptObject yPoints)
121
122 ;
123
124
125
126 public native void drawRect (int x, int y, int width, int height)
127
128 ;
129
130 public native void fillRect (int x, int y, int width, int height)
131
132 ;
133
134
135
136 public void drawPolygon (int[] xPoints, int[] yPoints)
137 {
138 drawPolygon(graphics, ArrayUtils.toJsArray(xPoints), ArrayUtils.toJsArray(yPoints));
139 }
140
141 public void drawPolygon (ArrayList coords)
142 {
143 int[] x = new int[coords.size()];
144 int[] y = new int[coords.size()];
145
146 for (int i = 0; i < coords.size(); i++) {
147 x[i] = ((Coords)coords.get(i)).getX();
148 y[i] = ((Coords)coords.get(i)).getY();
149 }
150
151 drawPolygon(x, y);
152 }
153
154 private native void drawPolygon (JavaScriptObject g, JavaScriptObject xPoints, JavaScriptObject yPoints)
155
156 ;
157
158
159
160 public void fillPolygon (int[] xPoints, int[] yPoints)
161 {
162 fillPolygon(graphics, ArrayUtils.toJsArray(xPoints), ArrayUtils.toJsArray(yPoints));
163 }
164
165 private native void fillPolygon (JavaScriptObject g, JavaScriptObject xPoints, JavaScriptObject yPoints)
166
167 ;
168
169
170
171 public native void drawEllipse (int x, int y, int width, int height)
172
173 ;
174
175 public native void fillEllipse (int x, int y, int width, int height)
176
177 ;
178
179
180 public void setFont (String text, String size, Style style)
181 {
182 setFont(text, size, style.toString());
183 }
184
185 private native void setFont (String text, String size, String style)
186
187 ;
188
189 public native void drawString (String text, int x, int y)
190
191 ;
192
193 public native void drawStringRect (String text, int x, int y, int width, int height)
194
195 ;
196
197 public native void drawImage (String src, int x, int y, int width, int height)
198
199 ;
200
201
202
203
204 public native void paint ()
205
206 ;
207
208 public native void clear ()
209
210 ;
211
212 public native void setPrintable (boolean val)
213
214 ;
215
216
217 private static class Style {
218
219 private String val;
220
221 public Style (String val)
222 {
223 this.val = val;
224 }
225
226 public String toString ()
227 {
228 return val;
229 }
230 }
231
232 }