View Javadoc

1   /**********************************************
2    * Copyright (C) 2009 Lukas Laag
3    * This file is part of lib-gwt-svg-samples.
4    * 
5    * libgwtsvg-samples is free software: you can redistribute it and/or modify
6    * it under the terms of the GNU General Public License as published by
7    * the Free Software Foundation, either version 3 of the License, or
8    * (at your option) any later version.
9    * 
10   * libgwtsvg-samples is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   * GNU General Public License for more details.
14   * 
15   * You should have received a copy of the GNU General Public License
16   * along with libgwtsvg-samples.  If not, see http://www.gnu.org/licenses/
17   **********************************************/
18  package org.vectomatic.svg.samples.client.shapes;
19  
20  import org.vectomatic.dom.svg.OMSVGDocument;
21  import org.vectomatic.dom.svg.OMSVGEllipseElement;
22  import org.vectomatic.dom.svg.OMSVGLength;
23  import org.vectomatic.dom.svg.OMSVGPathElement;
24  import org.vectomatic.dom.svg.OMSVGPathSegList;
25  import org.vectomatic.dom.svg.OMSVGRectElement;
26  import org.vectomatic.dom.svg.OMSVGSVGElement;
27  import org.vectomatic.dom.svg.OMSVGTextElement;
28  import org.vectomatic.dom.svg.utils.OMSVGParser;
29  import org.vectomatic.dom.svg.utils.SVGConstants;
30  import org.vectomatic.svg.samples.client.Main;
31  import org.vectomatic.svg.samples.client.Main.MainBundle;
32  import org.vectomatic.svg.samples.client.SampleBase;
33  
34  import com.google.gwt.core.client.GWT;
35  import com.google.gwt.dom.client.Style.Unit;
36  import com.google.gwt.uibinder.client.UiBinder;
37  import com.google.gwt.uibinder.client.UiField;
38  import com.google.gwt.user.client.Element;
39  import com.google.gwt.user.client.ui.HTML;
40  import com.google.gwt.user.client.ui.TabLayoutPanel;
41  
42  /**
43   * Class to demonstrate the creation of miscellaneous SVG shapes
44   * @author laaglu
45   */
46  public class ShapesSample extends SampleBase {
47  	interface ShapesSampleBinder extends UiBinder<TabLayoutPanel, ShapesSample> {
48  	}
49  	private static ShapesSampleBinder binder = GWT.create(ShapesSampleBinder.class);
50  
51  	@UiField(provided=true)
52  	public static MainBundle mainBundle = Main.mainBundle;
53  	@UiField
54  	HTML svgContainer;
55  	OMSVGSVGElement svg;
56  
57  	@Override
58  	public TabLayoutPanel getPanel() {
59  		if (tabPanel == null) {
60  			tabPanel = binder.createAndBindUi(this);
61  			tabPanel.setTabText(0, "Shapes");
62  			createCodeTabs("ShapesSample");
63  			
64  			// Create a SVG document
65  			OMSVGDocument doc = OMSVGParser.currentDocument();
66  			
67  			// Create the root svg element
68  			svg =  doc.createSVGSVGElement();
69  			svg.setViewBox(0f, 0f, 300f, 200f);
70  			svg.getWidth().getBaseVal().newValueSpecifiedUnits(Unit.PCT, 100);
71  			svg.getHeight().getBaseVal().newValueSpecifiedUnits(Unit.PCT, 100);
72  
73  			// Create a rect
74  			OMSVGRectElement rect = doc.createSVGRectElement(5f, 35f, 50f, 20f, 4f, 4f);
75  			rect.getStyle().setSVGProperty(SVGConstants.CSS_FILL_PROPERTY, SVGConstants.CSS_LIGHTGREEN_VALUE);
76  			rect.getStyle().setSVGProperty(SVGConstants.CSS_STROKE_PROPERTY, SVGConstants.CSS_BLACK_VALUE);
77  			
78  			// Create an ellipse
79  			OMSVGEllipseElement ellipse = doc.createSVGEllipseElement(60f, 80f, 30f, 15f);
80  			ellipse.getStyle().setSVGProperty(SVGConstants.CSS_FILL_PROPERTY, SVGConstants.CSS_YELLOW_VALUE);
81  			ellipse.getStyle().setSVGProperty(SVGConstants.CSS_STROKE_PROPERTY, SVGConstants.CSS_BLACK_VALUE);
82  			ellipse.getStyle().setSVGProperty(SVGConstants.CSS_STROKE_DASHARRAY_PROPERTY, "5,2,2,2");
83  			
84  			// Create a heart-shaped path
85  			OMSVGPathElement path = doc.createSVGPathElement();
86  			OMSVGPathSegList segs = path.getPathSegList();
87  			segs.appendItem(path.createSVGPathSegMovetoAbs(14.86487f, 27.54341f));
88  			segs.appendItem(path.createSVGPathSegCurvetoCubicAbs(2.16163f, 16.27388f, 10.93614f, 23.44857f, 5.53039f, 20.93069f));
89  			segs.appendItem(path.createSVGPathSegCurvetoCubicAbs(0.40252f, 6.50142f, 0.19540f, 13.47135f, -0.27051f, 9.80295f));
90  			segs.appendItem(path.createSVGPathSegCurvetoCubicAbs(8.61522f, 0.38648f, 1.50231f, 3.03806f, 4.88027f, 0.20151f));
91  			segs.appendItem(path.createSVGPathSegCurvetoCubicAbs(15.54304f, 4.44105f, 11.49036f, 0.13255f, 14.15935f, 2.02987f));
92  			segs.appendItem(path.createSVGPathSegCurvetoCubicRel(0.71868f, 1.07327f, 0.37594f, 0.23175f, 0.35941f, 2.21266f));		
93  			segs.appendItem(path.createSVGPathSegCurvetoCubicAbs(23.25811f, 0.39544f, 17.31181f, 2.60470f, 20.15524f, 0.48976f));
94  			segs.appendItem(path.createSVGPathSegCurvetoCubicAbs(31.97687f, 7.41380f, 27.37183f, 0.07861f, 31.56253f, 3.22656f));
95  			segs.appendItem(path.createSVGPathSegCurvetoCubicRel(-2.04024f, 9.10060f, 0.46508f, 3.14678f, -0.04068f, 6.54082f));
96  			segs.appendItem(path.createSVGPathSegCurvetoCubicRel(-10.90462f, 9.53931f, -2.92245f, 3.93336f, -7.42165f, 6.18284f));
97  			segs.appendItem(path.createSVGPathSegCurvetoCubicRel(-3.12799f, 2.94243f, -1.08820f, 0.79363f, -2.28426f, 2.46524f));
98  			segs.appendItem(path.createSVGPathSegCurvetoCubicRel(-1.03915f, -1.45272f, -0.28088f, -0.52741f, -0.64130f, -1.00829f));
99  			segs.appendItem(path.createSVGPathSegClosePath());
100 			path.getStyle().setSVGProperty(SVGConstants.CSS_FILL_PROPERTY, SVGConstants.CSS_RED_VALUE);
101 			
102 			// Create a text
103 			OMSVGTextElement text = doc.createSVGTextElement(10, 120, OMSVGLength.SVG_LENGTHTYPE_PX, "Hello, World");
104 			text.getStyle().setSVGProperty(SVGConstants.CSS_STROKE_PROPERTY, SVGConstants.CSS_BLUE_VALUE);
105 
106 			svg.appendChild(rect);
107 			svg.appendChild(ellipse);
108 			svg.appendChild(path);
109 			svg.appendChild(text);
110 			
111 			// Insert the SVG root element into the HTML UI
112 			Element div = svgContainer.getElement();
113 			div.appendChild(svg.getElement());
114 		}
115 		return tabPanel;
116 	}
117 }