Advertisement
Guest User

Swing+svgSalamander

a guest
Feb 1st, 2015
272
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.05 KB | None | 0 0
  1. package org.fullthrust.client;
  2.  
  3. import java.awt.BorderLayout;
  4. import java.awt.Dimension;
  5. import java.awt.Graphics;
  6. import java.io.PrintWriter;
  7. import java.io.StringReader;
  8. import java.io.StringWriter;
  9. import java.net.URI;
  10.  
  11. import javax.swing.JPanel;
  12.  
  13. import com.kitfox.svg.SVGCache;
  14. import com.kitfox.svg.SVGElement;
  15. import com.kitfox.svg.SVGElementException;
  16. import com.kitfox.svg.SVGException;
  17. import com.kitfox.svg.animation.AnimationElement;
  18. import com.kitfox.svg.app.beans.SVGIcon;
  19.  
  20.  
  21. class TestPanel extends JPanel
  22. {
  23.     public static final long serialVersionUID = 0;
  24.    
  25.     final SVGIcon icon;
  26.     URI uri;
  27.     SVGElement root;
  28.    
  29.     public TestPanel()
  30.     {
  31.         StringReader reader = new StringReader(makeDynamicSVG());
  32.         uri = SVGCache.getSVGUniverse().loadSVG(reader, "myImage");
  33.         icon = new SVGIcon();
  34.         icon.setAntiAlias(true);
  35.         icon.setSvgURI(uri);
  36.         root = icon.getSvgUniverse().getDiagram(uri).getRoot();
  37.        
  38.         setPreferredSize(new Dimension(400, 400));
  39.     }
  40.    
  41.     public void paintComponent(Graphics g)
  42.     {
  43.         final int width = getWidth();
  44.         final int height = getHeight();
  45.        
  46.         g.setColor(getBackground());
  47.         g.fillRect(0, 0, width, height);
  48.        
  49.         icon.paintIcon(this, g, 0, 0);
  50.     }
  51.    
  52.     private String makeDynamicSVG()
  53.     {
  54.         StringWriter sw = new StringWriter();
  55.         PrintWriter pw = new PrintWriter(sw);
  56.        
  57.         pw.println("<svg width=\"400\" height=\"400\" style=\"fill:none;stroke-width:16\" viewBox=\"781 -391 1177 826\">");
  58.         pw.println("<rect x=\"0\" y=\"0\" width=\"2000\" height=\"1000\" style=\"stroke:blue;fill:white\"/>");
  59.         pw.println("</svg>");
  60.        
  61.         pw.close();
  62.         return sw.toString();
  63.     }
  64.    
  65.    
  66.    
  67. }
  68.  
  69. /**
  70.  *
  71.  * @author  kitfox
  72.  */
  73. public class TestClient2 extends javax.swing.JFrame
  74. {
  75.     public static final long serialVersionUID = 0;
  76.    
  77.     TestPanel panel = new TestPanel();
  78.    
  79.     public TestClient2()
  80.     {
  81.         initComponents();
  82.  
  83.         panel_display.add(panel, BorderLayout.CENTER);
  84.        
  85.        
  86.         pack();
  87.     }
  88.    
  89.     /** This method is called from within the constructor to
  90.      * initialize the form.
  91.      * WARNING: Do NOT modify this code. The content of this method is
  92.      * always regenerated by the Form Editor.
  93.      */
  94.     // <editor-fold defaultstate="collapsed" desc=" Generated Code ">                          
  95.     private void initComponents()
  96.     {
  97.         panel_display = new javax.swing.JPanel();
  98.         button1 = new javax.swing.JButton();
  99.         button2 = new javax.swing.JButton();
  100.  
  101.         setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  102.         panel_display.setLayout(new java.awt.BorderLayout());
  103.  
  104.         getContentPane().add(panel_display, java.awt.BorderLayout.CENTER);
  105.  
  106.         button1.setText("Visible");
  107.         button1.addActionListener(new java.awt.event.ActionListener()
  108.         {
  109.             public void actionPerformed(java.awt.event.ActionEvent evt)
  110.             {
  111.                 SVGElement root = TestClient2.this.panel.root;
  112.                 try {
  113.                     root.setAttribute("viewBox", AnimationElement.AT_XML, "0 0 2368 1652");
  114.                     root.updateTime(0f);
  115.                     repaint();
  116.                 } catch (SVGElementException e) {
  117.                     e.printStackTrace();
  118.                 } catch (SVGException e) {
  119.                     e.printStackTrace();
  120.                 }
  121.             }
  122.         });
  123.        
  124.         button2.setText("Invisible");
  125.         button2.addActionListener(new java.awt.event.ActionListener()
  126.         {
  127.             public void actionPerformed(java.awt.event.ActionEvent evt)
  128.             {
  129.                 SVGElement root = TestClient2.this.panel.root;
  130.                 try {
  131.                     root.setAttribute("viewBox", AnimationElement.AT_XML, "800 0 2368 1652");
  132.                     root.updateTime(0f);
  133.                     repaint();
  134.                 } catch (SVGElementException e) {
  135.                     e.printStackTrace();
  136.                 } catch (SVGException e) {
  137.                     e.printStackTrace();
  138.                 }
  139.             }
  140.         });
  141.  
  142.         panel_display.add(button1, java.awt.BorderLayout.EAST);
  143.         panel_display.add(button2, java.awt.BorderLayout.WEST);
  144.  
  145.         pack();
  146.     }// </editor-fold>                        
  147.  
  148.                                        
  149.    
  150.     /**
  151.      * @param args the command line arguments
  152.      */
  153.     public static void main(String args[])
  154.     {
  155.         java.awt.EventQueue.invokeLater(new Runnable()
  156.         {
  157.             public void run()
  158.             {
  159.                 new TestClient2().setVisible(true);
  160.             }
  161.         });
  162.     }
  163.    
  164.     // Variables declaration - do not modify                    
  165.     private javax.swing.JButton button1;
  166.     private javax.swing.JButton button2;
  167.     private javax.swing.JPanel panel_display;
  168.     // End of variables declaration                  
  169.    
  170. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement