View Javadoc

1   /*
2    * Copyright 2006 Robert Hanson <iamroberthanson AT gmail.com>
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *    http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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 &lt;head> of the HTMl page.
39  * 
40  * &lt;script src="script/wz_jsgraphics.js" type="text/javascript">&lt;/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  * &lt;div style="position:relative;width:350px;height:300px;" id="g">&lt;/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          return new $wnd.jsGraphics(id);
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          g.setColor(color);
97      }-*/;
98  
99  
100 
101     public native void setStrokeWidth (int width) /*-{
102         this.@org.gwtwidgets.client.wrap.JsGraphicsPanel::graphics.setStroke(width);
103     }-*/;
104    
105     public native void setStrokeDotted () /*-{
106         this.@org.gwtwidgets.client.wrap.JsGraphicsPanel::graphics.setStroke($wnd.Stroke.DOTTED);
107     }-*/;
108 
109     public native void drawLine (int x1, int y1, int x2, int y2) /*-{
110         this.@org.gwtwidgets.client.wrap.JsGraphicsPanel::graphics.drawLine(x1, y1, x2, y2);
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         g.drawPolyline(xPoints, yPoints);
122     }-*/;
123 
124     
125 
126     public native void drawRect (int x, int y, int width, int height) /*-{
127         this.@org.gwtwidgets.client.wrap.JsGraphicsPanel::graphics.drawRect(x, y, width, height);
128     }-*/;
129     
130     public native void fillRect (int x, int y, int width, int height) /*-{
131         this.@org.gwtwidgets.client.wrap.JsGraphicsPanel::graphics.fillRect(x, y, width, height);
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         g.drawPolygon(xPoints, yPoints);
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         g.fillPolygon(xPoints, yPoints);
167     }-*/;
168 
169     
170     
171     public native void drawEllipse (int x, int y, int width, int height) /*-{
172         this.@org.gwtwidgets.client.wrap.JsGraphicsPanel::graphics.drawEllipse(x, y, width, height); 
173     }-*/;
174 
175     public native void fillEllipse (int x, int y, int width, int height) /*-{
176         this.@org.gwtwidgets.client.wrap.JsGraphicsPanel::graphics.fillEllipse(x, y, width, height); 
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         this.@org.gwtwidgets.client.wrap.JsGraphicsPanel::graphics.setFont(text, size, style); 
187     }-*/;
188 
189     public native void drawString (String text, int x, int y) /*-{
190         this.@org.gwtwidgets.client.wrap.JsGraphicsPanel::graphics.drawString(text, x, y); 
191     }-*/;
192 
193     public native void drawStringRect (String text, int x, int y, int width, int height) /*-{
194         this.@org.gwtwidgets.client.wrap.JsGraphicsPanel::graphics.drawStringRect(text, x, y, width, height); 
195     }-*/;
196 
197     public native void drawImage (String src, int x, int y, int width, int height) /*-{
198         this.@org.gwtwidgets.client.wrap.JsGraphicsPanel::graphics.drawImage(src, x, y, width, height); 
199     }-*/;
200 
201     
202     
203 
204     public native void paint () /*-{
205         this.@org.gwtwidgets.client.wrap.JsGraphicsPanel::graphics.paint();
206     }-*/;
207 
208     public native void clear () /*-{
209         this.@org.gwtwidgets.client.wrap.JsGraphicsPanel::graphics.clear();
210     }-*/;
211 
212     public native void setPrintable (boolean val) /*-{
213         this.@org.gwtwidgets.client.wrap.JsGraphicsPanel::graphics.setPrintable(val);
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 }