You are hereAria User Guide / Section III: In Depth / Using SVG graphics

Using SVG graphics


By luano - Posted on 14 January 2009

Scaleable Vector Graphics ( SVG) is a vector file format from the W3C. SVG is a widely supported, resolution independant graphics file format that can be used with Aria and Aria.

SVG images can be created by experienced graphic designers and provide a relatively compact format. Sympathic use of SVG graphics can greatly enhance the overall visual impact of your application. Aria provides several painter classes, components and evan a Swing look and feel that support SVG. However, SVG is not just for eye candy, and rich, iteractive components can be built into your application to provide advanced, eye catching features.

Installing the components

Aria's SVG Components are designed for use in a wide range or Rich Internet Applications for purposes such as mapping, facilities management, interactive drawings and illustration. A rich developer API is provided that integrates with other Aria components and services. The API is designed to allow easy access to backend services so that the SVG components may be used for easy visualization of complex spatial data or just to add some extra interaction to your application.

Component details

DisplaySvg

A JComponent class that can display SVG images.

GrabMap

The GrabMap class provide a means of dragging an SVG image in conjunction with the SvgImageMap class using the mouse.

The component can be added to an application that uses the SvgImageMap quite easily, as shown below.

Setting up a grab map

  1. public class Welcome extends Page
  2. {
  3. protected SvgImageMap svgImage;
  4.  
  5. public void pageCreated()
  6. {
  7. svgImage = new SvgImageMap();
  8. svgImage.setURL( ClassLoader.getSystemResource( "images/image1.svg" ));
  9. GrabMap grabMap = new GrabMap( svgImage );
  10. }
  11. }
GroupHierarchy

The GroupHierarchy class extracts the group hierarchy from and SVG image. The group hierarchy is then stored in a DefaultMutableTreeNode instance so that the group hierarchy can be displayed in a tree component.

The code example below shows how the how the component can be used in a program by passing it an instance of the SvgImageMap and then diplaying the results in a JTree within a JFrame .

Setting up a group hierarchy

  1. public class Welcome extends Page
  2. {
  3. protected SvgImageMap svgImage;
  4. public void pageCreated()
  5. {
  6. svgImage = new SvgImageMap();
  7. svgImage.setURL( ClassLoader.getSystemResource( "images/image1.svg" ));
  8.  
  9. GroupHierarchy gh = new GroupHierarchy( svgImage );
  10. DefaultMutableTreeNode root = gh.getGroupHierarchy();
  11. JTree tree = new JTree( root );
  12.  
  13. JFrame frame = new JFrame();
  14. frame.setSize( 375, 400 );
  15. frame.setLocation( 100, 100 );
  16. frame.getContentPane().add( new JScrollPane( tree ),
  17. BorderLayout.CENTER );
  18. frame.setVisible( true );
  19. }
  20. }
HotSpotInfo

An interface that can be implemented by any class that needs to have data passed to it from InfoFileParser class.

InfoFileParser

Used to parse an xml file and pass the information to a class implmenting the StatusInfo interface.

MapArea

Defines an area's bounds based on a set of points.

OverlayComponent

A non-opaque component, in which other components can be added, that can be used as an overlay.

PointSystem

The PointSystem class allows conversion of a coordinate from the svg coordinate system to the application coordinate system and vice versa. It can be used in an application as follows.

Use a point system

  1. public class Welcome extends Page
  2. {
  3. protected SvgImageMap svgImage;
  4.  
  5. public void pageCreated()
  6. {
  7. svgImage = new SvgImageMap();
  8. svgImage.setURL( ClassLoader.getSystemResource( "images/image1.svg" ));
  9. PointSystem pointSystem = new PointSystem( svgImage );
  10. Point2D p1 = pointSystem.glassPaneToSvg( 100.0, 200.0 );
  11. }
  12. }
PopUpWindow

A pop-up window that can be used to display textual information.

RenderingSemephore

Used to insure that seperate calls to render the SVG image occur independently.

RolloverComponent

The RolloverComponent class is used to display a Swing component on the glass pane when a mouse rollover event occurs.

It can be added to an application as shown below. Setting up a rollover

  1. public class Welcome extends Page
  2. {
  3. protected SvgImageMap svgImage;
  4. protected JComponent glassPane;
  5. protected RolloverComponent rc;
  6.  
  7. public void pageCreated()
  8. {
  9. glassPane = (JComponent)( (Applet)project.getStartupObject()
  10. ).getGlassPane();
  11. glassPane.setOpaque( false );
  12. glassPane.setLayout( new BorderLayout() );
  13.  
  14. svgImage = new SvgImageMap();
  15. svgImage.setURL( ClassLoader.getSystemResource(
  16. "images/image1.svg"));
  17.  
  18. Graph graph = new Graph();
  19.  
  20. rc = new RolloverComponent( new JComponent[]{ graph } );
  21. rc.setBounds( 349, 233, 11, 11 );
  22. glassPane.add( rc );
  23. }
  24. }

In the above example, when a mouse rollover event occurs on the RolloverComponent , the Graph component will be displayed on the glass pane.

RoutePlotter

Used to plot a route within an SVG image using a series of waypoints.

SelectionZoom

The SelectionZoom class allows zooming of the Svg image displayed in the SvgImageMap class by selecting a region using the CTRL key and the mouse.

It can be added to an application as shown below.

Adding a selection area

  1. public class Welcome extends Page
  2. {
  3. protected SvgImageMap svgImage;
  4.  
  5. public void pageCreated()
  6. {
  7. svgImage = new SvgImageMap();
  8. svgImage.setURL( ClassLoader.getSystemResource( "images/image1.svg" ));
  9. SelectionZoom zoomSelection = new SelectionZoom( svgImage );
  10. }
  11. }
SelectSvgArea

Used to select a region within an svg image an return all elements within that region.

SvgImageMap

The SvgImageMap class manages the display of an SVG image. The class extends the Aria Panel class to provide a panel within which the SVG image can be displayed.

The SvgImageMap can be added to a page as follows:

Adding an SvgImageMap component

  1. <SvgImageMap name="imageMap1" x="50" y="50" w="200" h="150"
  2. content="images/image1.svg">

Or via Java code as follows: Adding an SvgImageMap via Java

  1. public class Images extends Page
  2. {
  3. protected XPanel panel;
  4. protected SvgImageMap svgImage;
  5.  
  6. public void pageCreated()
  7. {
  8. Panel panel = (Panel)findComponent( "panel1" );
  9. svgImage = new SvgImageMap();
  10. svgImage.setURL( ClassLoader.getSystemResource( "images/image1.svg" ));
  11. panel.add( svgImage );
  12. }
  13. }
SvgLayer

A layer, which elements can be added to, that can be added to an svg image.

SvgMagnifyingWindow

The SvgMagnifyingWindow class provides a means of zooming an SVG image in conjunction with the SvgImageMap class. The class provides a component for displaying the zoomed SVG image.

Below is an example of setting up the magnifying window in a page. First the window is added through XML:

Adding an SvgMagnifyingWindow via XML

  1. <SvgMagnifyingWindow name="magWindow" x="0" y="0" h="180" w="200" background="white" >

Then the remaining set-up work is done in the pageCreated method and using the mouseMoved method of the MouseMotionListener interface: Setting up the magnifier

  1. public class Magnification extends Page implements MouseMotionListener
  2. {
  3. protected SvgImageMap imageMap;
  4. protected SvgMagnifyingWindow magWindow;
  5.  
  6. public void pageCreated()
  7. {
  8. imageMap = (SvgImageMap)findComponent("imageMap");
  9. magWindow = (SvgMagnifyingWindow)findComponent("magWindow");
  10.  
  11. imageMap.addMouseMotionListener( this );
  12.  
  13. magWindow.setBorder( BorderFactory.createLineBorder(
  14. Color.GRAY, 2 ));
  15. magWindow.setDiagram( imageMap.getSvgDiagram() );
  16. magWindow.setImageMap( imageMap );
  17. magWindow.setScaleFactor( 5.0F );
  18. }
  19.  
  20. public void mouseMoved( MouseEvent e )
  21. {
  22. magWindow.setZoomPoint( e.getPoint() );
  23. }
  24. }
SvgMagnifyingGlass

The SvgMagnifyingGlass class provides a means of zooming an SVG image in conjunction with the SvgImageMap class. It differs from the SvgMagnifyingWindow in that it provides a magnifying glass which can be scrolled over the image using the mouse. The component is displayed on the glass pane and the zoomed area of the SVG image is displayed in the magnifying glass. The class provides a component for displaying the zoomed SVG image.

Then set-up work is done in the pageCreated method, the mouseMoved method of the MouseMotionListener interface and the mouseEntered and mouseExited methods of the MouseListener interface:

Setup the magnifying glass

  1. public class Magnification extends Page implements MouseMotionListener,
  2. MouseListener
  3. {
  4. protected SvgImageMap imageMap;
  5. protected SvgMagnifyingGlass magGlass;
  6. protected JComponent glassPane;
  7.  
  8. public void pageCreated()
  9. {
  10. imageMap = (SvgImageMap)findComponent( "imageMap" );
  11. magGlass = new SvgMagnifyingGlass();
  12. glassPane = imageMap.getGlassPane();
  13. glassPane.setLayout( new BorderLayout() );
  14. imageMap.addMouseMotionListener( this );
  15. imageMap.addMouseListener( this );
  16.  
  17. magGlass.setDiagram( imageMap.getSvgDiagram() );
  18. magGlass.setImageMap( imageMap );
  19. magGlass.setScaleFactor( 5.0F );
  20. glassPane.add( magGlass );
  21. new Thread( magGlass ).start ();
  22. glassPane.setVisible( true );
  23. }
  24.  
  25. public void mouseMoved( MouseEvent e )
  26. {
  27. magGlass.setZoomPoint( e.getPoint(), e.getPoint() );
  28. }
  29.  
  30. public void mouseEntered( MouseEvent e ){
  31. magGlass.setAnimationMode ( SvgMagnifyingGlass.FADE_IN, 20F );
  32. }
  33.  
  34. public void mouseExited( MouseEvent e )
  35. {
  36. magGlass.setAnimationMode ( SvgMagnifyingGlass.FADE_OUT, 10F );
  37. }
  38. }

In the above code example the mouseEntered and mouseExited methods trigger the fade-in and fade-out animations for the magnifying glass. The float value passed to the setAnimationMode method sepecifies the duration if the animation.

SvgMagnifier

The SvgMagnifier class uses the SvgMagnifyingGlass and SvgMagnifyingWindow classes to magnify an SVG image.

A window is provided for displaying the magnification using SvgMagnifyingWindow . Radio buttons are provided for switching between magnification modes i.e. Magnifying window and magnifying glass. Check boxes are provided for switching on the rotation and high quality rendering modes of the magnifying glass during run time.

Setting up the Magnifier

  1. public class Magnification extends Page implements Mouselistener,
  2. MouseMotionListener
  3. {
  4. protected SvgMagnifier magnifier;
  5. protected JComponent glassPane;
  6.  
  7. public void pageCreated()
  8. {
  9. glassPane =
  10. (JComponent)( (Applet)project.getStartupObject()
  11. ).getGlassPane();
  12.  
  13. glassPane.setOpaque( false );
  14. glassPane.setLayout( new BorderLayout() );
  15. SvgImageMap svgImage = (SvgImageMap)findComponent("svgImage");
  16. svgImage.getSvgPanel ().addMouseMotionListener (this);
  17. magnifier = new SvgMagnifier( glassPane, svgImage );
  18.  
  19. svgImage.addMouseMotionListener( this );
  20. svgImage.addMouseListener( this );
  21. }
  22.  
  23. public void mouseMoved( MouseEvent e )
  24. {
  25. magnifier.setZoomPoint( e.getPoint());
  26. }
  27.  
  28. public void mouseDragged( MouseEvent e )
  29. {
  30. magnifier.setZoomPoint( e.getPoint());
  31. }
  32.  
  33. public void mouseEntered (MouseEvent e)
  34. {
  35. magnifier.setMagGlassAnimation( SvgMagnifyingGlass.FADE_IN,
  36. 20F );
  37. }
  38.  
  39. public void mouseExited (MouseEvent e)
  40. {
  41. magnifier.setMagGlassAnimation( SvgMagnifyingGlass.FADE_OUT,
  42. 10F );
  43.  
  44.  
  45. }
  46. }
SvgRolloverFinder

An interface containing methods that can be implemented to aid the setup of rollover blocks.

ViewBoxListener

The ViewBoxListener class works in conjunction with the SvgImageMap to ensure components displayed on the glass pane above a certain point of image map remain at that location if the image map's view box is modified. This is achieved by using the PointSystem class to convert from the SVG co-ordinate system to the glass pane co-ordinate system. An example of its use in an application is shown below.

Setting up a ViewBoxListener

  1. public class Welcome extends Page
  2. {
  3. protected SvgImageMap svgImage;
  4. protected JComponent glassPane;
  5.  
  6. public void pageCreated()
  7. {
  8. glassPane = (JComponent)( (Applet)project.getStartupObject()
  9. ).getGlassPane();
  10. glassPane.setOpaque( false );
  11. glassPane.setLayout( new BorderLayout() );
  12.  
  13. svgImage = new SvgImageMap();
  14. svgImage.setURL( ClassLoader.getSystemResource(
  15. "images/image1.svg"));
  16.  
  17. JLabel label1 = new JLabel( "Label 1" );
  18. label1.setBounds( 200, 200, 50, 20 );
  19. glassPane.add( label1 );
  20.  
  21. ViewBoxListener listener = new ViewBoxListener( imageMap );
  22. listener.registerComponent( label1 );
  23. }
  24. }
ViewBoxModifier

Used to modify the viewbox size and position.

Waypoint

A waypoint representing a point within an SVG image.

WaypointFlag

A flag that can be added to an SVG image to mark a waypoint.