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:
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.
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:
Create an SVGUniverse.
Call universe.loadSVG() to load the SVG file into memory.
Call universe.getDiagram() to get an SVGDiagram object that can be used to render the SVG file.
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
publicvoid 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
publicvoid 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.:
<gid="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]
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.
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.
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.
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.
Used to notify all listeners that have registered interest for notification on this event type. The ActionEvent parameter 'event' specifies the event type.
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.
There are no comments on this page. [Add comment]