[英]SVGSalamander : how can I resize a single SvgIcon?
With SvgSalamander , I am trying to build a Chess Canvas from some svg. 我正在尝试使用SvgSalamander从某些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).
但是我无法弄清楚如何调整单个SvgIcon元素的大小(与这种情况并不完全相同,它使用了整个工作区。此外,我真的很想在没有AffineTransform类和所有这些计算的情况下做到这一点)。 I've tried to use SvgIcon#setPreferredSize(java.awt.Dimension) ... but it did not change anything.
我试图使用SvgIcon#setPreferredSize(java.awt.Dimension)...但它没有改变任何东西。
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. 我也看到了这一点 :还注意到我下载的图片没有viewbox元素:但是添加viewbox和preserveAspectRatio属性也没有改变。
I am already able to load the SvgIcon : I am just missing a way to resize it. 我已经能够加载SvgIcon:我只是缺少一种调整其大小的方法。 (The reason why I want SVG format : to be able to resize "without loss").
(我之所以要SVG格式的原因是:能够“无损失”地调整大小)。
setPreferredSize works nice fine in combination with setScaleToFit. setPreferredSize与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();
}
}
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. 组件侦听器必须具有SVGIcon参考,并且必须在组件大小更改时设置图标大小。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.