6

I want to display some images on a canvas accordingly to screen dimension without losing the image resolution.

If I use a simple png or jpeg, I will see the image pixelized on a big screen.

What I am thinking is to use svg files and display them on the janvas. Is something like that possible? (I tried to use Apache Batik without a possitive result)

if this is not possible, can you think any other alternative?

1 Answers1

4

Apache Commons has Batik Swing component containing special canvas JSVGCanvas. This is the best approach you can find.

The goal of the Batik Swing component module is to provide a Swing component that can used to display SVG documents. With the JSVGCanvas class, you can easily display an SVG document (from a URI or a DOM tree) and allow the user to manipulate it, such as rotating, zooming, panning, selecting text or activating hyperlinks.


I tried to use Apache Batik without a possitive result

Any problem you can share with us? Here and here you will find how to implement it.

public SVGCanvasDemo(JFrame frame) {
    frame.getContentPane().setLayout(new BorderLayout());
    frame.getContentPane().add("Center", svgCanvas);
    frame.setVisible(true);
    svgCanvas.setURI("file:/c:/files/hungryminds/rectangles.svg");
}
Jordi Castilla
  • 26,609
  • 8
  • 70
  • 109
  • thank you for your reply. One more question just to understand how this works. if I want to display multiple images on the canvas, I have to use multiple JSVGCanvas instances? – Giannis Tzagarakis Nov 23 '15 at 12:29
  • yes, as described in the component: *while displaying or manipulating **an SVG document.*** – Jordi Castilla Nov 23 '15 at 12:47
  • I have the following error "org.w3c.dom.svg does not exist" when I import batik's files in my project. "org/w3c/dom/svg" folder does not exist inside the downloaded zip files – Giannis Tzagarakis Nov 23 '15 at 16:25