loloof64
loloof64

Reputation: 5392

SVGSalamander : how can I resize a single SvgIcon?

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").

Upvotes: 0

Views: 715

Answers (2)

Andrew Anno
Andrew Anno

Reputation: 11

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();

}
    }

Upvotes: 1

bahtiyartan
bahtiyartan

Reputation: 1030

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.

Upvotes: 0

Related Questions