Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Losing the anchor when rendering SVG images in MIDlets

by Retired ‎02-16-2010 09:20 AM - edited ‎09-28-2010 12:53 PM (1,676 Views)



This article applies to the following:

  • BlackBerry® smartphones based on Java® technology
  • BlackBerry® Device Software 4.6 and later
  • SDR309780



When rendering Scalable Vector Graphics (SVG) images to a javax.microedition.lcdui.Displayable object or one of its subtypes such as a Canvas or a Screen, the top-left anchor is changed with every call to javax.microedition.m2g.ScalableGraphics.render


When you call javax.microedition.m2g.ScalableGraphics.render.(x,y,svgimage) any subsequent calls to the render method are drawn in relation to the (x,y) that you set in the previous render call. Specifically the new anchor is the bottom-right corner of the previous SVG image rendered.




To work around this issue, you must know the previous render call x and y values and offset any subsequent SVG image renders based on those values. The following is an example:

  • Render image1 at (0,0)Render image2 at (200,200)Render image3 at (100,100) [Actual render call will be with (-100,-100) to offset the previous render call.]

The following is example code for rendering SVG images to the canvas taking this issue into consideration.



import java.io.IOException;
import javax.microedition.lcdui.*;
import javax.microedition.m2g.*;
import javax.microedition.midlet.*;

public class Main extends MIDlet {

private Display display;
private MyCanvas myCanvas;

protected void destroyApp(boolean arg0) throws MIDletStateChangeException {}

protected void pauseApp() {}

protected void startApp() throws MIDletStateChangeException {
display = Display.getDisplay(this);
myCanvas = new MyCanvas();
private class MyCanvas extends Canvas {

private ScalableGraphics sg;

public MyCanvas(){
sg = ScalableGraphics.createInstance();
protected void paint(Graphics g) {
g.fillRect(0,0, getWidth(), getHeight());
ScalableImage svgImage2 = null;

try {
svgImage2 = ScalableImage.createImage("file:///SDCard/BlackBerry/pictures/2.svg", null);
} catch (IOException e) {
//paint at 0,0.
//paint at 200,200.
//paint at 100,100. (offset by previous call.)





Users Online
Currently online: 22 members 2,589 guests
Please welcome our newest community members: