• Advertisement
Sign in to follow this  
  • entries
    21
  • comments
    30
  • views
    12109

java svg code

Sign in to follow this  

383 views

I figured someone might be interested, so here is my java code for getting a simple SVG-backed sprite up and running:


import org.apache.batik.gvt.renderer.StaticRenderer;
import org.w3c.dom.svg.SVGDocument;
import org.apache.batik.bridge.*;
import org.apache.batik.gvt.*;

import java.awt.geom.*;
import java.awt.*;

class SVGSprite
{
// svg data
SVGDocument document;
BridgeContext bridgeContext;
StaticRenderer renderer = new StaticRenderer();
SVGUserAgent userAgent = new SVGUserAgent();
static GVTBuilder builder = new GVTBuilder();

double documentW, documentH;
double preferedW, preferedH;
int offscreenW, offscreenH;

// sprite data
double rotation = 0;
double translate_x = 0, translate_y = 0;
boolean needs_redraw = true;

// flags
public static final int CENTER = 1;


public SVGSprite(String svg_file_name)
{
document = SVGLayer.loadDocument(svg_file_name);

bridgeContext = new BridgeContext(userAgent);

GraphicsNode root = builder.build(bridgeContext, document);
Dimension2D docSize = bridgeContext.getDocumentSize();
documentW = docSize.getWidth();
documentH = docSize.getHeight();
setPreferedSize(documentW, documentH);
renderer.setTree(root);

needs_redraw = true;
}

void setPreferedSize(double size_x, double size_y)
{
preferedW = size_x;
preferedH = size_y;
double max_diam = Math.max(preferedW, preferedH);
max_diam = max_diam * 1.5;
offscreenW = (int) (max_diam);
offscreenH = (int) (max_diam);
renderer.updateOffScreen(offscreenW, offscreenH);
needs_redraw = true;
}

void setRotation(double rot)
{
rotation = rot;
needs_redraw = true;
}
void setTranslation(double x, double y)
{
// this method can useful for setting the draw coordinates with float-precision
translate_x = x;
translate_y = y;
needs_redraw = true;
}


AffineTransform cached_transform = new AffineTransform();
void redraw()
{
cached_transform.setToIdentity();
cached_transform.translate(translate_x, translate_y);
cached_transform.rotate(rotation, offscreenW/2f, offscreenH/2f);
cached_transform.translate((offscreenW - documentW)/2f, (offscreenH - documentH)/2f);
cached_transform.scale(preferedW/documentW, preferedH/documentH);
renderer.setTransform(cached_transform);
renderer.repaint(new Rectangle(0, 0, offscreenW, offscreenH));
needs_redraw = false;
}

void draw(Graphics2D g, int loc_x, int loc_y, int flags)
{
if (needs_redraw) redraw();

if ((flags & CENTER) != 0)
{
loc_x -= offscreenW;
loc_y -= offscreenH;
}

g.drawImage(renderer.getOffScreen(), null, loc_x, loc_y);
//g.setColor(Color.GRAY);
//g.drawRect(loc_x, loc_y, offscreenW, offscreenH);
}

}
// *************
import java.awt.*;
import java.awt.geom.*;
import java.io.IOException;

import org.apache.batik.dom.svg.SAXSVGDocumentFactory;
import org.apache.batik.dom.svg.SVGDocumentFactory;
import org.apache.batik.util.XMLResourceDescriptor;
import org.w3c.dom.svg.SVGDocument;
import org.apache.batik.bridge.*;
import org.apache.batik.transcoder.*;
import org.apache.batik.gvt.GraphicsNode;
import org.apache.batik.gvt.renderer.StaticRenderer;


public class SVGLayer
{
protected static SVGDocumentFactory factory;

static SVGDocument loadDocument(String uri)
{
if (factory == null)
{
// create document factory
String parser = XMLResourceDescriptor.getXMLParserClassName();
factory = new SAXSVGDocumentFactory(parser);
}


try {
return factory.createSVGDocument(uri);
} catch (IOException e)
{
e.printStackTrace();
return null;
}
}
}
// **************

import java.awt.*;
import java.awt.geom.*;
import org.apache.batik.bridge.UserAgentAdapter;

class SVGUserAgent extends UserAgentAdapter
{
AffineTransform transform = new AffineTransform();

public AffineTransform getTransform()
{
return transform;
}

public Dimension2D getViewportSize()
{
return new Dimension(100,100);
}

public void displayError(String message)
{
throw new RuntimeException(message);
}
public void displayError(Exception e)
{
e.printStackTrace();
}
public void displayMessage(String message)
{
throw new RuntimeException(message);
}
}

// ****************

and here is sample code of it being used:

public void paint(Graphics2D g2d)
{
SVGSprite = new SVGSprite("file:a.svg");
sprite.setRotation(Math.PI/2);
sprite.setPreferedSize(20, 20);
sprite.draw(g2d, 10, 10, SVGSprite.CENTER);
}


Sign in to follow this  


0 Comments


Recommended Comments

There are no comments to display.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Advertisement