Xoetrope

 Wiki : XSvgImageMap

HomePage :: Categories :: PageIndex :: RecentChanges :: RecentlyCommented :: Login/Register
Carousel logo

XSvgImageMap

class com.xoetrope.svg.XSvgImageMap

The XSvgImageMap class manages the display of an SVG (Scalable Vector Graphics) image. SVG is a language for describing two-dimensional graphics in XML. The class extends the XUI XPanel class to provide a panel to display the SVG image in and implements the MouseMotionListener and XAttributedComponent interfaces. The class also makes use of the SVG Salamander framework. SVG Salamander is an open source SVG engine that makes it easy to integrate SVG images into a Java application.

1. Example


Here is a simple Java swing example to display an SVG file using the XSvgImageMap class:

import com.xoetrope.swing.svg.XSvgImageMap;
import javax.swing.JFrame

public class TestSvg
{
    public static void main(String [] args)
    {
            XSvgImageMap svgImage = new XSvgImageMap();
            svgImage.setURL( ClassLoader.getSystemResource( "images/image1.svg" ) );
            JFrame frame = new JFrame( "SVG Test" );
            frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
            frame.setBackground( Color.WHITE );
            frame.setSize( 400, 300 );
            frame.setVisible( true );
            frame.add( svgImage );
    } 
}


In the above example "images/image1.svg" specifies the path to the SVG file you wish to display, which is passed as a URL using the setURL method. Calling the display() method renders the SVG image and displays it in the XPanel. In order to have the image resize when you resize the frame, you will need to specify a viewBox attribute within your SVG file.

2. Constructor


public XSvgImageMap ()

The constructor shown does not take any parameters. It initialises the SVGUniverse and SVGDisplayPanel objects, adds a MouseMotionListener to the SVGDisplayPanel and adds a component listener to the SVGDisplayPanel which will listen for resize events and call the resize() method.

3. Methods



1. setURL

public void setURL(URL url)

The setURL method sets the URL pointing to the SVG image that is to be displayed. The method takes a URL object as a parameter specifying the location of the SVG image.


2. display

public SVGDiagram display()

The display method is called to display the SVG image. This is accomplished primarily through the use of the Salamander framework in the following steps:

  1. Create an SVGUniverse.
  2. Call universe.loadSVG() to load the SVG file into memory.
  3. Call universe.getDiagram() to get an SVGDiagram object that can be used to render the SVG file.
  4. Add the SVGDiagram object to the XPanel.

A component listener to listen for component resize events is also implemented in the display method. If a resize event occurs, the SVG image is resized to fit the panel and centered within the panel. During the resizing, the aspect ratio of the image is maintained. The display method returns the instance of SVGDiagram that is instantiated within the method, which can be used to acces the root node of the SVG document.


3. resize

public void resize()

Called by the componenet resized listener to resize the SVG image to fit the panel. This method can be called if you wish to resize the image independent of a cmponenet resized event.


4. loadArrays

public void loadArrays()

The loadArrays method is called to load grouped shapes into arrays for display. This is useful when a different shape is to be displayed when a mouse rollover occurs. For example, a set of base shapes may have a corresponding shape that is to be displayed when the mouse pointer is detected within one of the base shapes. In order for this feature to function correctly, grouped shapes must have prefixes set. i.e.:

<g id="b_Block8">

The above XML code corresponds to a base group named "b_Block8", which could have a roll-over group named r_Block8 (below). The naming format is therefore:

Base groups: b_[group name]
Rollover groups: r_[group name]

<g id="r_Block8" visibility="hidden">



5. setVisibility

public void setVisibility(String group, boolean v)

The setVisibility method is called by the mouseMoved method to display a rollover group. The string parameter 'group' specifies the name of the group to be displayed and the boolean parameter 'v' specifies whether the group is to be made hidden or visible. In order to change the visibility of the group, the group's 'visibility' attribute is modified.


6. setImageName

public void setImageName( String name )

Loads a new SVG image into the image map and displays it.


7. mouseMoved

public void mouseMoved (MouseEvent e)

The mouseMoved method, belonging to the MouseMotionListener interface, monitors mouse movement over the SVG image and checks if the mouse pointer is contained within one of the base group shapes by calling the checkBlock method. If the mouse pointer is contained within one of the base group shapes the setVisibility method is called to make its coreesponding rollover shape visible.


8. checkBlock

private boolean checkBlock (Point p, String s)

The checkBlock method is called by the mouseMoved method to check if the mouse pointer is contained within one of the base group shapes. The Point parameter 'p' corresponds to the position of the mouse pointer and the String parameter 's' corresponds to the name of the base group to be checked.


9. getSvgPanel

public SVGDisplayPanel getSvgPanel()

Returns the instance of the SVG display panel used to display the image.


10. setAttribute

public int setAttribute (String attribName, Object attribValue)

A method belonging to the XAttributedComponent interface, an interface that allows the component builders to set the attributes of a component without knowing the individual accessor methods. The String parameter 'attribName' corresponds to the name of the attribute to be set and the Object parameter 'attribValue' corresponds to the value that the attribute is to be set to.


11. addActionListener

public void addActionListener( ActionListener al )

Used to add an ActionListener to the list of available listeners. The ActionListener parameter 'al' corresponds to the ActionListener object that is to be added.


12. removeActionListener

public void removeActionListener( ActionListener l )

Used to remove an ActionListener from the list of available listeners. The ActionListener parameter 'l' corresponds to the ActionListener object that is to be removed.


13. fireActionPerformed

protected void fireActionPerformed( ActionEvent event )

Used to notify all listeners that have registered interest for notification on this event type. The ActionEvent parameter 'event' specifies the event type.


14. setSvgRolloverFinder

public void setSvgRolloverFinder( XSvgRolloverFinder sim )

Used to set the compare interfaces for comparing groups/elements for identification of the rollovers


15. getSvgRolloverFinder

public XSvgRolloverFinder getSvgRolloverFinder()

Returns the instance of the compare interface currently in use.


16. setBlockPrefix

public void setBlockPrefix( String prefix )

Set the prefix to be used for the base groups.


17. setRolloverPrefix

public void setRolloverPrefix( String prefix )

Set the prefix to be used for the rollover groups.

18. paintComponent

public void paintComponent( Graphics g )

Renders the SVG diagram to a BufferedImage instance and paints it within the component.

19. setImageLocation

public void setImageLocation( int x, int y )

Sets the location of the image within the component.

19. setImage

public void setImage( BufferedImage b )

Sets the BufferedImage instance displayed within this component to a new BufferedImage instance.

20. getBufferedImage

public BufferedImage getBufferedImage()

Returns the BufferedImage instance currently displayed by this class.

21. getSVGDiagram

public SVGDiagram getSvgDiagram()

Returns an instance of the SVG diagram belonging to the currently displayed image.

22. setAttribute

public int setAttribute( String attribName, Object attribValue )


Sets one of the image map attributes to the specified attribValue parameter. This method can be used to set the content of the image map or the opacity.

23. getImageName

public String getImageName()

Returns the name if the image currently displayed in the image map.

23. addMapMouseListener

public void addMapMouseListener( MouseListener l )

Registers a mouse listener to listen for mouse events on the component.

24. removeMapMouseListener

public void removesMapMouseListener( MouseListener l )

Removes a mouse listener registered on the component.

25. setPreserveAspect

public void setPreserveAspect( boolean preserve )

Sets whether the aspect ration of the image is to be maintained when resixing the image.

26. getPreserveAspect

public boolean setPreserveAspect()

Returns a boolean value specifying whether the aspect ratio of the image is currently being preserved or not.

image

There are no comments on this page. [Add comment]

Page was generated in 1.2690 seconds