-1

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.

Robert Longson
  • 118,664
  • 26
  • 252
  • 242
SPlatten
  • 5,334
  • 11
  • 57
  • 128
  • When I open your svg with inkscape it doesn't show up. – matt Feb 21 '16 at 19:39
  • Works fine for me...I've just copied the SVG into a new test file and opened in firefox, I've edited original post to show what I did. – SPlatten Feb 22 '16 at 08:27
  • It ''works'' in firefox, but it is layed out oddly. It also opens in inkscape, but the image is not on the document it is far away. You might want to check the layout of your svg file. Also, because of the way you describe the problem, it sounds like everything is working except your svg. – matt Feb 22 '16 at 08:46
  • http://imgur.com/eWa6dS9 in case you're curious, the box is the document and you know the lamps. – matt Feb 22 '16 at 08:49
  • The svg in this example isn't final, the lamps are aligned to the left of the right of the display. – SPlatten Feb 22 '16 at 08:52
  • But the issue is, I see no frame and no content in the frame. – SPlatten Feb 22 '16 at 08:53
  • Instead of using mframe.pack try to set it's size manually. I suspect since the svg document isn't bounded ''correctly'', it might not get sized correctly. – matt Feb 22 '16 at 08:57
  • On Linux (Ubuntu) the window is displayed but its empty, on Windows, no window is displayed. This is with pack commented out. – SPlatten Feb 22 '16 at 09:00
  • Without pack you still might have to use something to set the size. What about wrapping a jpanel around your image, and forcing that to have a min/max and preferred sizes? – matt Feb 22 '16 at 09:02
  • In the original code I posted I set the size of the window to full size: mframe.setMaximumSize(dimMax);mgfxdevice.setFullScreenWindow(mframe); – SPlatten Feb 22 '16 at 09:05
  • I don't know what mgfxdevice is. If you set the max size on a JFrame, that does not mean that is the size it will take. – matt Feb 22 '16 at 09:11
  • mgfxdevice is an instance of 'GraphicsDevice' and is setup with mgfcdevice = GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices()[0]; – SPlatten Feb 22 '16 at 09:15
  • Should you pass the GraphicsConfiguration from your graphics device to the JFrame when you create it? Also, If you might want to check 'isFullScreenSupported' it seems you will go back to window mode if it isn't. I would try to separate these two issues though. FullScreen'd vs Displaying an SVG file. – matt Feb 22 '16 at 09:19
  • I've modified the SVG setting the size of the document according to the screen resolution. The window is set to full size of the desktop in Ubuntu, but its still empy. – SPlatten Feb 22 '16 at 09:21

1 Answers1

0

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);             
SPlatten
  • 5,334
  • 11
  • 57
  • 128