Introduction to SVG (JSR 266) Development Guide

Source: Internet
Author: User
Tags border color

This article reprinted from the J2ME Development Network, this article copyright belongs to the original author and J2medev all.
SonyEricsson's W950 M600 and P990 are based on Symbian platform handsets, and Symbian Java Platform 3 is implemented on the phone. Support for the scalable Vector graphics is provided in SJP-3, and this article shows how to load the generated SVG image from a file and how to create SVG image in code.

The following is a file svgimage.svg for creating SVG image.

<?xml version= "1.0" encoding= "UTF-8"?>
<! DOCTYPE svg public "-//W3C//DTD svg Tiny 1.1//en"
"Http://www.w3.org/Graphics/SVG/1.1/DTD/svg11-tiny.dtd" >

<svg width= "height=" version= "1.1" xmlns= "Http://www.w3.org/2000/svg" baseprofile= "Tiny" >
<circle id= "Circle" cx= "The Cy=" "r=" "fill=" "#C8C800" fill-opacity= "0.5" stroke= "#000000" stroke-width= "5.0" >
<rect id= "rect" x= "y=" "width=" "height=" and "#640000" fill= "fill-opacity=" 0.5 "#000000" stroke= = "5.0"/>

</svg>

In this file we will create a circle and a rectangle with the following information.
Circle:
id = Circle//ID is used to mark this circle, so that you can
CX = 75//Origin X
cy = 75/O Origin Y
R = 25//Radius
Fill = c8c800//Fill Color
Fill-opacity = 0.5//50% transparency
Stroke = black//Border color
STROKE-WITDH = 5//Border width

The following code shows how to load the above file in the MIDlet

Private Svgimage svgimage = null;
Private Scalablegraphics GC;
...
InputStream is = GetClass (). getResourceAsStream ("/svgimage.svg");
Svgimage = (svgimage) svgimage.createimage (is, null);
public void Paint (Graphics g) {
Gc.bindtarget (g);
Gc.render (0, 0, svgimage);
Gc.releasetarget ();
}

When image is created, you can modify it, for example, by modifying the radius of the circle below.

Document document = Svgimage.getdocument ();
Svgelement circle = (svgelement) document.getElementById ("Circle");
Circle.setfloattrait ("R", 40.0f);

This article describes how to create an SVG Image from a file, and the following code shows how to create it in code.
Scalablegraphics GC = Scalablegraphics.createinstance ();

Svgimage svgimage = Svgimage.createemptyimage (null);
Svgimage.setviewportwidth (GetWidth ());
Svgimage.setviewportheight (GetHeight ());
Document document = Svgimage.getdocument ();
Svgsvgelement root = (svgsvgelement) document.getdocumentelement ();
String namespace = Root.getnamespaceuri ();

svgelement element = null;
try{
element = (svgelement) document.createelementns (namespace, "Circle");
}catch (Exception e) {
Svgdynamic.addinfo (E.tostring ());
E.printstacktrace ();
}
Svgrgbcolor FillColor = Root.createsvgrgbcolor (0xc8, 0xc8, 0);
Svgrgbcolor strokecolor = root.createsvgrgbcolor (0, 0, 0);

Element.setid ("Circle");
Element.setfloattrait ("CX", 75.0f);
Element.setfloattrait ("Cy", 75.0f);
Element.setfloattrait ("R", 25.0f);
Element.setfloattrait ("Fill-opacity", 0.5f);
Element.setfloattrait ("Stroke-width", 5.0f);
Element.setrgbcolortrait ("Fill", FillColor);
Element.setrgbcolortrait ("Stroke", strokecolor);

Root.appendchild (Element);

public void Paint (Graphics g) {
Gc.bindtarget (g);
Gc.render (0, 0, svgimage);
Gc.releasetarget ();
}

The WTK 2.5 provides a JSR 226 implementation environment and provides examples for developers to refer to.

Import javax.microedition.midlet.*;
Import javax.microedition.lcdui.*;

Import Javax.microedition.m2g.ScalableGraphics;
Import Javax.microedition.m2g.SVGImage;

Import org.w3c.dom.Document;
Import org.w3c.dom.svg.SVGElement;
Import org.w3c.dom.svg.SVGSVGElement;
Import Org.w3c.dom.svg.SVGRGBColor;


public class Hellosvg extends MIDlet {

protected Svgimagecanvas Svgcanvas = null;

Public Hellosvg () {

}

public void startApp () {
Svgimage svgimage = Svgimage.createemptyimage (null);
Document doc = Svgimage.getdocument ();
Svgsvgelement svg = (svgsvgelement) doc.getdocumentelement ();
Svgelement textelement = (svgelement) doc.createelementns ("Http://www.w3.org/2000/svg", "text");
Textelement.settrait ("#text", "Hello JSR-226!");
Textelement.setfloattrait ("x", 50.0f);
Textelement.setfloattrait ("Y", 50.0f);
Svgrgbcolor textcolor = svg.createsvgrgbcolor (0, 0, 0);
Textelement.setrgbcolortrait ("Stroke", textcolor);

Svg.appendchild (TextElement);

Svgcanvas = new Svgimagecanvas (svgimage);
Display.getdisplay (This). Setcurrent (Svgcanvas);
}

public void Pauseapp () {
}

public void Destroyapp (Boolean unconditional) {
}
}

Class Svgimagecanvas extends Canvas {

protected Svgimage svgimage;

protected Scalablegraphics sg = scalablegraphics.createinstance ();


Protected Svgimagecanvas (Final svgimage svgimage) {
This.svgimage = Svgimage;
}

public void Paint (Graphics g) {
G.setcolor (255, 255, 255);
G.fillrect (0, 0, getwidth (), getheight ());
Sg.bindtarget (g);
Svgimage.setviewportwidth (GetWidth ());
Svgimage.setviewportheight (GetHeight ());
Sg.render (0, 0, svgimage);
Sg.releasetarget ();
}
}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.