/* * Copyright © 2013 Nokia Corporation. All rights reserved. Nokia and Nokia * Connecting People are registered trademarks of Nokia Corporation. Oracle and * Java are trademarks or registered trademarks of Oracle and/or its affiliates. * Other product and company names mentioned herein may be trademarks or trade * names of their respective owners. See LICENSE.TXT for license information. */ package com.nokia.example; import com.nokia.mid.ui.orientation.Orientation; import com.nokia.mid.ui.orientation.OrientationListener; import javax.microedition.lcdui.Graphics; import javax.microedition.lcdui.game.GameCanvas; import javax.microedition.m2g.SVGImage; import javax.microedition.m2g.ScalableGraphics; import org.w3c.dom.svg.SVGElement; import org.w3c.dom.svg.SVGPath; import org.w3c.dom.svg.SVGRGBColor; import org.w3c.dom.svg.SVGSVGElement; /** * Simple canvas that uses JSR-226 to render SVG content. This class also * implements OrientationListener to detect a change in the orientation of the * device's display. Orientation is supported for Java Runtime 2.0.0 for Series * 40 onwards. */ public class SvgCanvas1 extends GameCanvas implements OrientationListener { /** * Constructor. */ public SvgCanvas1(boolean suppressKeyEvents) { super(suppressKeyEvents); /** * Orientation is supported for Java Runtime 2.0.0 for Series 40 * onwards. Registers the orientation listener. Applications that need * information about events related to display orientation changes need * to register with Orientation to get notifications of the events. */ Orientation.addOrientationListener(this); // *** setup an instance of ScalableGraphics sg = ScalableGraphics.createInstance(); sg.setRenderingQuality(ScalableGraphics.RENDERING_QUALITY_HIGH); // *** create a simple SVG image // ** create an empty image svgImage = (SVGImage) (SVGImage.createEmptyImage(null)); // ** grab the root <svg> element SVGSVGElement rootElement = (SVGSVGElement) (svgImage.getDocument().getDocumentElement()); // ** create some basic colors to work with SVGRGBColor yellow = rootElement.createSVGRGBColor(255, 255, 0); SVGRGBColor black = rootElement.createSVGRGBColor(0, 0, 0); // ** create the face and add it to the document SVGElement circle = (SVGElement) (svgImage.getDocument().createElementNS("http://www.w3.org/2000/svg", "circle")); circle.setRGBColorTrait("fill", yellow); circle.setFloatTrait("r", 50); circle.setFloatTrait("cx", 50); circle.setFloatTrait("cy", 50); rootElement.appendChild(circle); // ** create the left eye and add it to the document circle = (SVGElement) (svgImage.getDocument().createElementNS("http://www.w3.org/2000/svg", "circle")); circle.setFloatTrait("r", 5); circle.setFloatTrait("cx", 30); circle.setFloatTrait("cy", 30); rootElement.appendChild(circle); // ** create the right eye and add it to the document circle = (SVGElement) (svgImage.getDocument().createElementNS("http://www.w3.org/2000/svg", "circle")); circle.setFloatTrait("r", 5); circle.setFloatTrait("cx", 70); circle.setFloatTrait("cy", 30); rootElement.appendChild(circle); // ** create the mouth and add it to the document SVGElement path = (SVGElement) (svgImage.getDocument().createElementNS("http://www.w3.org/2000/svg", "path")); SVGPath d = rootElement.createSVGPath(); d.moveTo(20, 60); d.quadTo(50, 80, 80, 60); path.setPathTrait("d", d); path.setRGBColorTrait("stroke", black); path.setFloatTrait("stroke-width", 3); path.setTrait("fill", "none"); rootElement.appendChild(path); } /** * Paint method. */ public void paint(Graphics g) { // *** clear the display g.setColor(255, 255, 255); g.fillRect(0, 0, getWidth(), getHeight()); // *** render the SVG image sg.bindTarget(g); sg.setTransparency(1f); sg.render((getWidth() - svgImage.getViewportWidth()) >> 1, (getHeight() - svgImage.getViewportHeight()) >> 1, svgImage); sg.releaseTarget(); } /* * Private members */ private ScalableGraphics sg; private SVGImage svgImage; /** * Orientation is supported for Java Runtime 2.0.0 for Series 40 onwards. * Called when display's orientation has changed. */ public void displayOrientationChanged(int newDisplayOrientation) { /** * Change MIDlet UI orientation */ Orientation.setAppOrientation(newDisplayOrientation); } }