A full working example of an SVG About box

/*
	555 Timer Design Tool
    Copyright (C) 2000-2005 Andrew J. Armstrong

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

	Author:
	Andrew J. Armstrong <andrew_armstrong(ad)unwired(dod)com(dod)au>

    Created using IntelliJ IDEA 4.5 <www.jetbrains.com>
*/
package au.com.hpo.c555.panel;

import org.apache.batik.bridge.ExternalResourceSecurity;
import org.apache.batik.bridge.ScriptSecurity;
import org.apache.batik.dom.svg.SAXSVGDocumentFactory;
import org.apache.batik.swing.JSVGCanvas;
import org.apache.batik.swing.svg.LinkActivationEvent;
import org.apache.batik.swing.svg.LinkActivationListener;
import org.apache.batik.swing.svg.SVGUserAgent;
import org.apache.batik.util.ParsedURL;
import org.apache.batik.util.XMLResourceDescriptor;
import org.w3c.dom.Element;
import org.w3c.dom.svg.SVGDocument;

import javax.swing.*;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;
import java.io.StringReader;
import java.net.URL;

/**
 * Displays an about box using inline SVG
 */
public class About extends JSVGCanvas
{
    private static final boolean EVENTS_ENABLED = true;
    private static final boolean SELECTABLE_TEXT = true;

    private static final String LOGO = "555 Timer";
    private static final String DESIGN_TOOL = "Design Tool";
    private static final String VERSION = "Version " + Version.getVersion();
    private static final String AUTHOR = "Copyright &#169; 1999-2005 Andrew J. Armstrong";
    private static final String AUTHOR_EMAIL = "andrew_armstrong(ad)unwired(dod)com(dod)au";

    private static final String ABOUT_BOX_INFO =
    "<?xml version='1.0' standalone='no'?>"
    + "<!DOCTYPE svg PUBLIC '-//W3C//DTD SVG 1.0//EN' 'http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd'>"
    + "<svg xmlns='http://www.w3.org/2000/svg' xmlns:xlink='http://www.w3.org/1999/xlink' width='450' height='500' viewBox='0 0 450 500'>"
    + "<rect x='0' y='0' width='100%' height='100%' style='fill:white' />"
    + "<defs>"

    + "<filter id='dropShadow'  filterUnits='objectBoundingBox' width='1.4' height='1.4'>"
    + "<feGaussianBlur in='SourceAlpha' stdDeviation='4' />"
    + "<feOffset dx='4' dy='4' />"
    + "<feComponentTransfer result='shadow'>"
    + "<feFuncA type='linear' slope='.5' intercept='0' />"
    + "</feComponentTransfer>"
    + "</filter>"

    + "<filter id='emboss' >"
    + "<feGaussianBlur in='SourceAlpha' stdDeviation='2' result='blur'/>"
    + "<feSpecularLighting in='blur' surfaceScale='-3' style='lighting-color:white'"
    + " specularConstant='1' specularExponent='16'"
    + " result='spec' kernelUnitLength='1' >"
    + "<feDistantLight azimuth='45' elevation='45' />"
    + "</feSpecularLighting>"
    + "<feComposite in='spec' in2='SourceGraphic' operator='in' result='specOut'/>"
    + "</filter>"
    + "</defs>"
    + getEmbossedString(72, 50, 35, "172,20,20", LOGO)
    + getEmbossedString(48, 50, 48, "172,20,20", DESIGN_TOOL)
    + getEmbossedString(32, 50, 60, "0,40,220", VERSION)
    + getString(14, 50, 75, "0,40,220", AUTHOR)
    + getString(14, 50, 80, "0,40,220", AUTHOR_EMAIL)
    + "</svg>"
    ;

    private static String getEmbossedString(int size, int x, int y, String color, String text)
    {
        return
        "<g style='font-size:" + size + "; font-family:Dialog; text-anchor:middle;'>"
        + "<text x='" + x + "%' y='" + y + "%' style='filter:url(#dropShadow)'>" + text + "</text>"
        + "<text x='" + x + "%' y='" + y + "%' style='fill:rgb(" + color + ")'>" + text + "</text>"
        + "<text x='" + x + "%' y='" + y + "%' style='filter:url(#emboss)'>" + text + "</text>"
        + "</g>";
    }

    private static String getString(int size, int x, int y, String color, String text)
    {
        return
        "<g style='font-size:" + size + "; font-family:Dialog; text-anchor:middle;'>"
        + "<a xlink:href='mailto:" + AUTHOR_EMAIL + "'>"
        + "<text x='" + x + "%' y='" + y + "%' style='fill:rgb(" + color + ")'>"
        + text
        + "</text>"
        + "</a>"
        + "	</g>";
    }

    public About()
    {

        super(new SVGUserAgent()
        {

            public void displayError(Exception e)
            {
            }

            public void displayError(String s)
            {
            }

            public void displayMessage(String s)
            {
            }

            public String getLanguages()
            {
                return "en";
            }

            public float getPixelToMM()
            {
                return 25.4f / 96; // = 0.264583333333333333333f; // 96 dpi
            }

            /**
             * Returns the user stylesheet uri.
             *
             * @return null if no user style sheet was specified.
             */
            public String getUserStyleSheetURI()
            {
                return null;
            }

            public String getXMLParserClassName()
            {
                String parserName = "org.apache.xerces.parsers.SAXParser";
                System.out.println("About.getXMLParserClassName " + parserName);
                return parserName;
            }

            public void handleElement(Element element, Object o)
            {
            }

            public void openLink(String uri, boolean activateInNewComponent)
            {
            }

            public boolean supportExtension(String s)
            {
                return false;
            }

            public boolean isXMLParserValidating()
            {
                return false;
            }

            /**
             * Returns this user agent's CSS media.
             */
            public String getMedia()
            {
                return "screen";
            }

            /**
             * Shows an alert dialog box.
             */
            public void showAlert(String message)
            {
            }

            /**
             * Returns this user agent's alternate style-sheet title.
             */
            public String getAlternateStyleSheet()
            {
                return null;
            }

            /**
             * Shows a confirm dialog box.
             */
            public boolean showConfirm(String message)
            {
                return false;
            }

            /**
             * Shows a prompt dialog box.
             */
            public String showPrompt(String message)
            {
                return null;
            }

            /**
             * Shows a prompt dialog box.
             */
            public String showPrompt(String message, String defaultValue)
            {
                return null;
            }

            public ScriptSecurity getScriptSecurity(String scriptType,
                                                    URL scriptURL,
                                                    URL docURL)
            {
                return null;
            }

            public void checkLoadExternalResource(ParsedURL resourceURL,
                                                  ParsedURL docURL) throws SecurityException
            {
            }

            public void checkLoadScript(String scriptType,
                                        ParsedURL scriptURL,
                                        ParsedURL docURL) throws SecurityException
            {
            }

            public float getMediumFontSize()
            {
                return 0;
            }

            public float getBolderFontWeight(float f)
            {
                return 0;
            }

            public float getLighterFontWeight(float f)
            {
                return 0;
            }

            public ExternalResourceSecurity
            getExternalResourceSecurity(ParsedURL resourceURL,
                                        ParsedURL docURL)
            {
                return null;
            }

            public float getPixelUnitToMillimeter()
            {
                return 0;
            }

            public ScriptSecurity getScriptSecurity(String scriptType,
                                                    ParsedURL scriptURL,
                                                    ParsedURL docURL)
            {
                return null;
            }

            public String getDefaultFontFamily()
            {
                return null;
            }

        }, EVENTS_ENABLED, SELECTABLE_TEXT);

        this.isDynamic();
        setEnableImageZoomInteractor(false);
        setEnableResetTransformInteractor(false);
        setEnableZoomInteractor(false);
        this.addLinkActivationListener(new LinkActivationListener()
        {
            public void linkActivated(LinkActivationEvent e)
            {
                try
                {
                    if (isWindowsPlatform())
                    {
                        String[] commandLine = {"rundll32", "url.dll,FileProtocolHandler", e.getReferencedURI()};
                        Runtime.getRuntime().exec(commandLine);
                    }
                }
                catch (IOException ioe)
                {
                    System.out.println(ioe);
                }

            }
        });

        try
        {
            String parser = XMLResourceDescriptor.getXMLParserClassName();
            SAXSVGDocumentFactory f = new SAXSVGDocumentFactory(parser);
            String uri = "http:internal";
            SVGDocument doc = (SVGDocument) f.createDocument(uri, new StringReader(ABOUT_BOX_INFO));
            setSVGDocument(doc);
        }
        catch (IOException e)
        {
            System.out.println("e = " + e);
        }
    }

    private boolean isWindowsPlatform()
    {
        String os = System.getProperty("os.name");
        return os != null && os.startsWith("Windows");

    }

    public static void main(String[] args)
    {
        JFrame frame = new JFrame("About");
        frame.addWindowListener
        (new WindowAdapter()
        {
            public void windowClosing(WindowEvent e)
            {
                System.exit(0);
            }
        });

        About aboutBox = new About();
        frame.getContentPane().add(aboutBox);
        frame.pack();
        Dimension dimScreen = Toolkit.getDefaultToolkit().getScreenSize();
        Dimension dimFrame = frame.getSize();
        frame.setLocation((dimScreen.width - dimFrame.width) / 2,
                          (dimScreen.height - dimFrame.height) / 2);
        frame.show();
    }

    public Dimension getPreferredSize()
    {
        return new Dimension(450, 500);
    }

}

class Version
{
    private static final String VERSION = "1.1.20020612";

    private Version()
    {
    }

    public static String getVersion()
    {
        return VERSION;
    }
}
  • No labels