1

我正在尝试重现这个示例,该示例应该使用 SVGSalamander API 创建笑脸图像。但是,当我运行我的程序时,图片没有出现。只有主要的 JFrame 在那里。由于使用 NetBeans 生成的代码,某些语法必须有所不同。不幸的是,我无法更改生成的代码。有谁知道我需要进行哪些更改才能运行该示例?我的代码中唯一的区别是 initComponents() 方法的生成部分。

这是我的代码:

class IconPanel extends javax.swing.JPanel {

public static final long serialVersioUID = 0;
final SVGIcon icon;

public IconPanel() {
    StringReader reader = new StringReader(makeDynamicSVG());
    URI uri = SVGCache.getSVGUniverse().loadSVG(reader, "myImage"); 
    icon = new SVGIcon();
    icon.setSvgURI(uri);       

    setPreferredSize(new Dimension(400, 400));   
}

@Override
public void paintComponent(Graphics g) {
    final int width = getWidth();
    final int height = getHeight();

    g.setColor(getBackground());
    g.fillRect(0, 0, width, height);

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

private String makeDynamicSVG()
{
    StringWriter sw = new StringWriter();
    PrintWriter pw = new PrintWriter(sw);

   pw.println("<svg width=\"400\" height=\"400\" style=\"fill:none;stroke-width:4\">");
    pw.println("    <circle cx=\"200\" cy=\"200\" r=\"200\" style=\"stroke:blue\"/>");
    pw.println("    <circle cx=\"140\" cy=\"140\" r=\"40\" style=\"stroke:red\"/>");
    pw.println("    <circle cx=\"260\" cy=\"140\" r=\"40\" style=\"stroke:red\"/>");
    pw.println("    <polyline points=\"100 300 150 340 250 240 300 300\" style=\"stroke:red\"/>");
    pw.println("</svg>");

    pw.close();
    return sw.toString();
}

}

public class MainFrame extends javax.swing.JFrame {

/**
 * Creates new form MainFrame
 */

public static final long serialVersionUID = 0;

IconPanel panel = new IconPanel();

public MainFrame() {
    initComponents();    

    this.getContentPane().add(panel, BorderLayout.CENTER);
    pack();
}


@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">                          
private void initComponents() {

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 400, Short.MAX_VALUE)
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 300, Short.MAX_VALUE)
    );

    pack();
}// </editor-fold>                        

/**
 * @param args the command line arguments
 */
public static void main(String args[]) {
    /*
     * Set the Nimbus look and feel
     */
    //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
    /*
     * If Nimbus (introduced in Java SE 6) is not available, stay with the
     * default look and feel. For details see
     * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
     */
    try {
        for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
            if ("Nimbus".equals(info.getName())) {
                javax.swing.UIManager.setLookAndFeel(info.getClassName());
                break;
            }
        }
    } catch (ClassNotFoundException ex) {
        java.util.logging.Logger.getLogger(MainFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (InstantiationException ex) {
        java.util.logging.Logger.getLogger(MainFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex) {
        java.util.logging.Logger.getLogger(MainFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (javax.swing.UnsupportedLookAndFeelException ex) {
        java.util.logging.Logger.getLogger(MainFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    }
    //</editor-fold>

    /*
     * Create and display the form
     */
    java.awt.EventQueue.invokeLater(new Runnable() {

        @Override
        public void run() {
            new MainFrame().setVisible(true);
        }
    });
}
// Variables declaration - do not modify                     
// End of variables declaration                   
}
4

0 回答 0