001: /*
002: *
003: * Copyright (c) 2007, Sun Microsystems, Inc.
004: *
005: * All rights reserved.
006: *
007: * Redistribution and use in source and binary forms, with or without
008: * modification, are permitted provided that the following conditions
009: * are met:
010: *
011: * * Redistributions of source code must retain the above copyright
012: * notice, this list of conditions and the following disclaimer.
013: * * Redistributions in binary form must reproduce the above copyright
014: * notice, this list of conditions and the following disclaimer in the
015: * documentation and/or other materials provided with the distribution.
016: * * Neither the name of Sun Microsystems nor the names of its contributors
017: * may be used to endorse or promote products derived from this software
018: * without specific prior written permission.
019: *
020: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
021: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
022: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
023: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
024: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
025: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
026: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
027: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
028: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
029: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
030: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
031: */
032: package com.sun.perseus.demo;
033:
034: import javax.microedition.lcdui.*;
035: import javax.microedition.m2g.SVGImage;
036: import javax.microedition.m2g.ScalableGraphics;
037: import javax.microedition.midlet.*;
038:
039: import org.w3c.dom.Document;
040: import org.w3c.dom.svg.SVGElement;
041: import org.w3c.dom.svg.SVGRGBColor;
042: import org.w3c.dom.svg.SVGRect;
043: import org.w3c.dom.svg.SVGSVGElement;
044:
045: /**
046: * Simple demo which creates an empty SVGImage, populates it with
047: * a graphical content and then displays that content.
048: */
049: public class CreateEmptyImageDemo extends MIDlet implements
050: CommandListener {
051: public static final String SVG_NAMESPACE_URI = "http://www.w3.org/2000/svg";
052: private final Command exitCommand = new Command("Exit",
053: Command.EXIT, 1);
054: SVGImageCanvas svgCanvas = null;
055:
056: public CreateEmptyImageDemo() {
057: }
058:
059: public void startApp() {
060: if (svgCanvas == null) {
061: System.err.print("Building SVGImage .... ");
062:
063: SVGImage svgImage = SVGImage.createEmptyImage(null);
064: Document doc = svgImage.getDocument();
065: SVGSVGElement svg = (SVGSVGElement) doc
066: .getDocumentElement();
067:
068: // First, set the viewBox so that we can work in
069: // a 100 by 100 area.
070: SVGRect vb = svg.createSVGRect();
071: vb.setWidth(100);
072: vb.setHeight(100);
073: svg.setRectTrait("viewBox", vb);
074:
075: SVGElement r = (SVGElement) doc.createElementNS(
076: SVG_NAMESPACE_URI, "rect");
077: SVGRGBColor bkgColor = svg.createSVGRGBColor(99, 128, 147);
078: r.setRGBColorTrait("fill", bkgColor);
079: r.setFloatTrait("x", 25);
080: r.setFloatTrait("y", 25);
081: r.setFloatTrait("rx", 5);
082: r.setFloatTrait("ry", 5);
083: r.setFloatTrait("width", 50);
084: r.setFloatTrait("height", 50);
085: svg.appendChild(r);
086:
087: SVGElement c = (SVGElement) doc.createElementNS(
088: SVG_NAMESPACE_URI, "circle");
089: SVGRGBColor fgColor = svg.createSVGRGBColor(255, 255, 255);
090: c.setRGBColorTrait("fill", fgColor);
091: c.setFloatTrait("cx", 50);
092: c.setFloatTrait("cy", 50);
093: c.setFloatTrait("r", 20);
094: svg.appendChild(c);
095:
096: System.err.println(" ... Done");
097:
098: svgCanvas = new SVGImageCanvas(svgImage);
099: svgCanvas.addCommand(exitCommand);
100: svgCanvas.setCommandListener(this );
101: }
102:
103: Display.getDisplay(this ).setCurrent(svgCanvas);
104: }
105:
106: public void pauseApp() {
107: }
108:
109: public void destroyApp(boolean unconditional) {
110: }
111:
112: public void commandAction(Command c, Displayable d) {
113: if (c == exitCommand) {
114: destroyApp(false);
115: notifyDestroyed();
116: }
117: }
118: }
|