0

With SvgSalamander, I am trying to build a Chess Canvas from some svg. But I can't figure how to resize a single SvgIcon element (not really the same as this case, which uses the whole client area. Furthermore, I really would like to do it without the AffineTransform class and all those computations). I've tried to use SvgIcon#setPreferredSize(java.awt.Dimension) ... but it did not change anything.

I've also seen this : also noticing that the pictures I've downloaded don't have a viewbox element : but adding a viewbox and preserveAspectRatio attributes didn't change either.

I am already able to load the SvgIcon : I am just missing a way to resize it. (The reason why I want SVG format : to be able to resize "without loss").

Community
  • 1
  • 1
loloof64
  • 5,252
  • 12
  • 41
  • 78

2 Answers2

1

setPreferredSize works nice fine in combination with setScaleToFit. At least it worked for me.

    import java.awt.*;
    import java.net.*;
    import java.io.*;

    import javax.swing.*;
    import com.kitfox.svg.*;
    import com.kitfox.svg.app.beans.*;
    class IconPanel extends JPanel { 

        final SVGIcon icon;
        public IconPanel(String name, int scalex, int scaley) { 
            icon = new SVGIcon();
            icon.setSvgURI(new SVGUniverse().loadSVG(getClass().getResource(name)));
        icon.setPreferredSize(new Dimension(scalex, scaley));
        icon.setScaleToFit(true);
        icon.setAntiAlias(true);} 
    public void paintComponent(Graphics g) { 

    icon.paintIcon(this, g, 0, 0); 
    icon.isScaleToFit();}}

public static void main(String[] args){
    JFrame frame=new JFrame();
    JPanel p=new JPanel();
    frame.setSize(800, 600);
    frame.setLayout(new BorderLayout());
    IconPanel icon=new IconPanel("cell.svg",1000,1000);
    frame.add(icon,BorderLayout.CENTER);
    frame.setVisible(false);
    frame.add(p,BorderLayout.PAGE_START);
    JButton button=new JButton();
    p.add(button);

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.show();

}
    }
  • Thanks. I tried your code, but I can't see anything in the panel. – loloof64 Sep 15 '15 at 19:45
  • 1
    I'm not that good in svg editing, but as far as I understand, it uses the absolute coordinates, which are implemented in paintIcon(comp, g, int x,int y). In this case they are 0,0. And the second thing is that the whole image being loaded to the code seems to be veery small, so to see it I had to resize it to 1000 size of both x and y. Added the main method so that the code is self-sufficient. – Andrew Anno Sep 16 '15 at 21:18
  • Hello, seems it is a problem with the build path and the path given for the picture. I am trying to change your code so that my picture path is correctly set. Just giving the string for the picture name is not such a good pattern. I do think your solution is good meanwhile. – loloof64 Sep 17 '15 at 08:10
  • didn't quite get it. You mean it's bad that the path is absolute? It can be easily changed to getClass().getResource(). I'll do it now. but the string of a name is imho necessary. My code uses svg images to load into grid of clickable jlabels, and I can't see how to do it without name - one needs to know what file he's referring to right now. – Andrew Anno Sep 26 '15 at 17:57
0

Write an component listener to your controls.

Component listener must have your SVGIcon reference and must set your icon size when component's size changes.

bahtiyartan
  • 1,010
  • 10
  • 29