Java 中的 SVG 无法正确显示

发布于 2024-08-31 21:40:35 字数 1592 浏览 4 评论 0原文

好的,我有一个 Java 程序,它在 FlowLayout 中显示一些 SVG 的图块。它通过成为 ScrabbleRack 类并扩展 JPanel,然后向此面板添加 JSVGCanvas 磁贴来实现此目的。

之后我创建了一个框架并添加了面板,这个。 (包装并展示)。出现时,面板无法正常显示。它只显示第一个图块,然后在应显示其余图块的空间中存在白色区域。

但如果我将框架大小调整任意量,图像将正确渲染。

public class ScrabbleRackGUI extends JPanel{
    ScrabbleRack rack=new ScrabbleRack();
    JSVGCanvas rackContentsImages[]=new JSVGCanvas[8];

public ScrabbleRackGUI() {
   setLayout(new FlowLayout());
   createComponents();
}
public void createComponents() {
    //INITIALISE SOURCE IMAGES
    initImages();
    for (int i=0;i<rackContentsImages.length;i++){
        this.add(rackContentsImages[i]);
    }
}
private void initImages(){
    File tempImages[]=new File[8];
    for(int i=0;i<8;i++){
       tempImages[i]= new File("./src/res/rackBackground.svg");
       rackContentsImages[i]=new JSVGCanvas();
       try {
           rackContentsImages[i].setURI(tempImages[i].toURL().toString());
       } catch (MalformedURLException ex) {
           Logger.getLogger(ScrabbleBoardGUI.class.getName()).log(Level.SEVERE, null, ex);
       }
    }
}
public static void main(String args[])
{
    JFrame frame = new JFrame("ScrabbleTest");
    ScrabbleRackGUI rack= new ScrabbleRackGUI(1);
    frame.add(rack);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.pack();
    frame.setSize(214,70);
    frame.setVisible(true);

}
}

关于如何让这个面板第一次正确显示的任何想法。

或者一些技巧可以在程序结束时调整它的大小。


我使用 batik 在 Java 中渲染 SVG,供那些想要重现此问题的人使用。

Ok, I have a Java program, that displays some tiles that are SVGs in a FlowLayout. It does this by being a class ScrabbleRack, and extending JPanel, then adding JSVGCanvas tiles to this panel.

Afterwards I created a frame and added the panel, this. (packed it and displayed it). On appearing, the panel does not display properly. It just displays the first tile and then in the space where the rest of the tiles should be displayed, there is whitearea.

But if I resize the frame by any amount, the image will render correctly.

public class ScrabbleRackGUI extends JPanel{
    ScrabbleRack rack=new ScrabbleRack();
    JSVGCanvas rackContentsImages[]=new JSVGCanvas[8];

public ScrabbleRackGUI() {
   setLayout(new FlowLayout());
   createComponents();
}
public void createComponents() {
    //INITIALISE SOURCE IMAGES
    initImages();
    for (int i=0;i<rackContentsImages.length;i++){
        this.add(rackContentsImages[i]);
    }
}
private void initImages(){
    File tempImages[]=new File[8];
    for(int i=0;i<8;i++){
       tempImages[i]= new File("./src/res/rackBackground.svg");
       rackContentsImages[i]=new JSVGCanvas();
       try {
           rackContentsImages[i].setURI(tempImages[i].toURL().toString());
       } catch (MalformedURLException ex) {
           Logger.getLogger(ScrabbleBoardGUI.class.getName()).log(Level.SEVERE, null, ex);
       }
    }
}
public static void main(String args[])
{
    JFrame frame = new JFrame("ScrabbleTest");
    ScrabbleRackGUI rack= new ScrabbleRackGUI(1);
    frame.add(rack);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.pack();
    frame.setSize(214,70);
    frame.setVisible(true);

}
}

Any ideas on how I can get this panel to display properly, first time.

Or some hack that will resize it at the end of the program.


I used batik to render the SVGs in Java, for those who want to reproduce this problem.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

守护在此方 2024-09-07 21:40:35

您的问题可能是您的 GUI 的构建没有在 EDT 上完成。

您的 main 应该类似于:

public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            MyWindow window = new MyWindow();
            MyWindow.setVisible(true);
            }
        });
    }

当前 main 中的其余代码应该位于 MyWindow 构造函数中。

更详细的信息可以在 http://leepoint.net/ 找到JavaBasics/gui/gui-commentary/guicom-main-thread.html (以及其他地方)

You problem may be that the construction of your GUI is not being done on the EDT.

Your main should look something like:

public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            MyWindow window = new MyWindow();
            MyWindow.setVisible(true);
            }
        });
    }

and the rest of your code in your current main should be in the MyWindow constructor.

More detailed information can be found at http://leepoint.net/JavaBasics/gui/gui-commentary/guicom-main-thread.html (among other places)

吃→可爱长大的 2024-09-07 21:40:35

这可能与此处报告的 Batik 问题 35922 有关:https://issues.apache。 org/bugzilla/show_bug.cgi?id=35922

如果我正确理解该错误报告,您可以通过添加 JSVGCanvas 实例(和 ScrabbleRackGUI 实例)并首先调用 pack() 来解决该问题,然后设置每个 JSVGCanvas 上的 URI。

This might be related to Batik issue 35922 reported here: https://issues.apache.org/bugzilla/show_bug.cgi?id=35922

If I understand that bug report correctly, you can workaround the problem by adding the JSVGCanvas instances (and the ScrabbleRackGUI instance) and calling pack() first, and then set the URIs on each JSVGCanvas.

李不 2024-09-07 21:40:35

首先,您写道:
ScrabbleRackGUI 机架 = 新 ScrabbleRackGUI(1);
并且您没有采用 int 的构造函数。

其次,您将 FlowLayout 设置为 JPanel 组件,并且 JPanel 默认情况下将 FlowLayout 作为布局。最好调用 super();获得 JPanel 的所有好处。

尝试在事件调度线程 (EDT) 内运行您的应用程序,正如其他人已经提到的那样。
SwingUtilities.invokeLater(new Runnable() {
// 这里是你的代码
另外

你应该像这样设置你的 URI:
setURI(f.toURI().toURL().toString());
因为 f.toURL() 已被弃用。

我希望它有帮助。

First of all, you wrote:
ScrabbleRackGUI rack= new ScrabbleRackGUI(1);
and you don't have constructor that takes int.

Secondly, you're setting FlowLayout to JPanel component, and JPanel by default has FlowLayout as layout. better call super(); to get all the benefits of JPanel.

Try to run your application inside of Event Dispatching Thread (EDT), as others mentioned already.
SwingUtilities.invokeLater(new Runnable() {
// your code here
}

Also you should set your URI like this:
setURI(f.toURI().toURL().toString());
because f.toURL() is deprecated.

I hope it helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文