Java application JFrame with SVG Salamander not visibile

203 views Asked by At

I want to display my SVG document in the main frame of a Java application.

    mframe = new JFrame();          
    mframe.setBackground(Color.BLACK);
    mframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    mframe.setExtendedState(JFrame.MAXIMIZED_BOTH);
    mframe.setUndecorated(true);
    mframe.setMaximumSize(dimMax);
    mgfxdevice.setFullScreenWindow(mframe);

dimMax is set-up and contains: 1680x1050

I create the SVG document:

    StringReader srdrSVG = new StringReader(sbSVG.toString());
    URI uriSVG = SVGCache.getSVGUniverse().loadSVG(srdrSVG, SVG_MIMIC);
    msvgPanel = new SVGPanel();
    msvgPanel.setAntiAlias(true);
    msvgPanel.setSvgURI(uriSVG);
    mframe.add(msvgPanel);
    //mframe.pack();
    mframe.setVisible(true);

However the window isn't visible, if I position the cursor over the icon in the taskbar I see an empty window but no window is displayed.

Here is the content of the SVG (StringBuilder sbSVG):

    <?xml version="1.0" encoding="UTF-8"?><svg xmlns="http://www.w3.org/2000/svg" width="1400" height="1050" id="root" style="background-color:#000000"><defs> <linearGradient id="lamp1rim" x1="0" y1="0" x2="1" y2="0"> <stop id="lamp1rimstp0" stop-color="#bfbfbf" offset="0"/> <stop id="lamp1rimstp1" stop-color="#404040" offset="1"/> </linearGradient> <linearGradient id="lamp1cap" x1="0" y1="0" x2="1" y2="1" spreadMethod="pad"> <stop id="lamp1capstp0" stop-color="#00ff00" stop-opacity="0.992188" offset="0"/> <stop id="lamp1capstp1" stop-color="#018201" stop-opacity="0.988281" offset="1"/> </linearGradient> <linearGradient id="lamp1spec" x1="0" y1="0" x2="1" y2="1" spreadMethod="pad"> <stop id="lamp1specstp0" stop-color="#ffffff" stop-opacity="0.996094" offset="0"/> <stop id="lamp1specstp1" stop-color="#06d306" stop-opacity="0.984375" offset="0.703125"/> </linearGradient>  <linearGradient id="lamp2rim" x1="0" y1="0" x2="1" y2="0"> <stop id="lamp2rimstp0" stop-color="#bfbfbf" offset="0"/> <stop id="lamp2rimstp1" stop-color="#404040" offset="1"/> </linearGradient> <linearGradient id="lamp2cap" x1="0" y1="0" x2="1" y2="1" spreadMethod="pad"> <stop id="lamp2capstp0" stop-color="#00ff00" stop-opacity="0.992188" offset="0"/> <stop id="lamp2capstp1" stop-color="#018201" stop-opacity="0.988281" offset="1"/> </linearGradient> <linearGradient id="lamp2spec" x1="0" y1="0" x2="1" y2="1" spreadMethod="pad"> <stop id="lamp2specstp0" stop-color="#ffffff" stop-opacity="0.996094" offset="0"/> <stop id="lamp2specstp1" stop-color="#06d306" stop-opacity="0.984375" offset="0.703125"/> </linearGradient> </defs><g transform="translate(860.0,-16.0) scale(0.25)"> <title id="lamp1title">Lamp 1</title> <circle id="lamp1shroud" cx="320" cy="240" fill="#212121" r="167" stroke-linecap="round" stroke-width="17.5" transform="rotate(90 320 240)"/> <circle id="lamp1outline" cx="319.252837" cy="239.999045" fill="url(#lamp1rim)" fill-opacity="0.64" r="160" stroke-width="17.5" stroke-linecap="round"/> <circle id="lamp1lense" cx="320.000535" cy="240.00169" fill="url(#lamp1cap)" r="150" stroke-linecap="round" stroke-width="17.5"/> <ellipse id="lamp1highlight" cx="249.179609" cy="168.124194" fill="url(#lamp1spec)" rx="75.675959" ry="44.402987" stroke-linecap="round" stroke-width="17.5" transform="rotate(-47.7626 249.18 168.124)"/> <text id="lamp1label" font-family="Verdana" font-size="55pt" text-anchor="middle" x="320" y="260">Lamp 1</text> </g> <g transform="translate(942.0,-16.0) scale(0.25)"> <title id="lamp2title">Lamp 2</title> <circle id="lamp2shroud" cx="320" cy="240" fill="#212121" r="167" stroke-linecap="round" stroke-width="17.5" transform="rotate(90 320 240)"/> <circle id="lamp2outline" cx="319.252837" cy="239.999045" fill="url(#lamp2rim)" fill-opacity="0.64" r="160" stroke-width="17.5" stroke-linecap="round"/> <circle id="lamp2lense" cx="320.000535" cy="240.00169" fill="url(#lamp2cap)" r="150" stroke-linecap="round" stroke-width="17.5"/> <ellipse id="lamp2highlight" cx="249.179609" cy="168.124194" fill="url(#lamp2spec)" rx="75.675959" ry="44.402987" stroke-linecap="round" stroke-width="17.5" transform="rotate(-47.7626 249.18 168.124)"/> <text id="lamp2label" font-family="Verdana" font-size="55pt" text-anchor="middle" x="320" y="260">Lamp 2</text> </g></svg>

To view the above SVG, just copy into a new text file, save as something like 'test.svg' and open in a web-browser. I did exactly this with Firefox and it renders fine, showing two green lamps.

1

There are 1 answers

0
SPlatten On BEST ANSWER

Final code, works:

    //Create panel for the SVG content
    StringReader srdrSVG = new StringReader(sbSVG.toString());
    URI uriSVG = SVGCache.getSVGUniverse().loadSVG(srdrSVG, SVG_MIMIC);
    SVGPanel svgPanel = new SVGPanel();
    svgPanel.setBackground(Color.BLACK);
    svgPanel.setMaximumSize(dimMax);
    svgPanel.setAntiAlias(false);
    svgPanel.setSvgURI(uriSVG);
    //Create application window/container           
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
    frame.setUndecorated(true);
    frame.setMaximumSize(dimMax);
    gfxdevice.setFullScreenWindow(frame);
    frame.add(svgPanel);            
    frame.setVisible(true);