Creating SVG diagrams in Java

I am trying to reproduce this example , which should create an emoticon image using the SVGSalamander API. However, when I run my program, the image does not appear. There is only the main JFrame. Some of the syntaxes should have been different due to the generated code using NetBeans. Unfortunately, I can’t change the generated code. Does anyone have an idea of ​​what changes I need to make to run the example? The only difference in my code is the generated part of the initComponents () method.

This is my code:

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 } 
+1
java svg
source share

No one has answered this question yet.

See similar questions:

6
Example SVG Salamander?
3
UML API for Java

or similar:

6170
Is Java pass-by-reference or pass-by-value?
3799
How do I read / convert an InputStream to a string in Java?
3324
How to generate random integers in a specific range in Java?
3073
How to efficiently iterate over each entry on a Java map?
3044
Creating a memory leak using Java
2956
What is the difference between public, secure, batch, and private in Java?
2936
When to use LinkedList over ArrayList in Java?
2853
How to convert String to int in Java?
2248
Is the finally block always executable in Java?
2171
How to determine if an array contains a specific value in Java?

All Articles