| 1 |
/* ===================== |
| 2 |
* SwingUIToSVGDemo.java |
| 3 |
* ===================== |
| 4 |
* |
| 5 |
* Copyright (c) 2013-2021, Object Refinery Limited. |
| 6 |
* All rights reserved. |
| 7 |
* |
| 8 |
* http://www.jfree.org/jfreesvg/index.html |
| 9 |
* |
| 10 |
* Redistribution and use in source and binary forms, with or without |
| 11 |
* modification, are permitted provided that the following conditions are met: |
| 12 |
* - Redistributions of source code must retain the above copyright |
| 13 |
* notice, this list of conditions and the following disclaimer. |
| 14 |
* - Redistributions in binary form must reproduce the above copyright |
| 15 |
* notice, this list of conditions and the following disclaimer in the |
| 16 |
* documentation and/or other materials provided with the distribution. |
| 17 |
* - Neither the name of the Object Refinery Limited nor the |
| 18 |
* names of its contributors may be used to endorse or promote products |
| 19 |
* derived from this software without specific prior written permission. |
| 20 |
* |
| 21 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 22 |
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 23 |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 24 |
* ARE DISCLAIMED. IN NO EVENT SHALL OBJECT REFINERY LIMITED BE LIABLE FOR ANY |
| 25 |
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 26 |
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 27 |
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
| 28 |
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 29 |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 30 |
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 |
* |
| 32 |
*/ |
| 33 |
|
| 34 |
package org.jfree.svg.demo; |
| 35 |
|
| 36 |
import java.awt.BorderLayout; |
| 37 |
import java.awt.event.ActionEvent; |
| 38 |
import java.awt.event.ActionListener; |
| 39 |
import java.io.File; |
| 40 |
import java.io.IOException; |
| 41 |
import javax.swing.JButton; |
| 42 |
import javax.swing.JComponent; |
| 43 |
import javax.swing.JFrame; |
| 44 |
import javax.swing.JPanel; |
| 45 |
import javax.swing.JTabbedPane; |
| 46 |
import javax.swing.UIManager; |
| 47 |
import javax.swing.UIManager.LookAndFeelInfo; |
| 48 |
import org.jfree.svg.SVGGraphics2D; |
| 49 |
import org.jfree.svg.SVGUtils; |
| 50 |
|
| 51 |
/** |
| 52 |
* This demo shows how to export a Swing UI to SVG. |
| 53 |
*/ |
| 54 |
public class SwingUIToSVGDemo extends JFrame implements ActionListener { |
| 55 |
|
| 56 |
public SwingUIToSVGDemo(String title) { |
| 57 |
super(title); |
| 58 |
add(createContent()); |
| 59 |
} |
| 60 |
|
| 61 |
private JComponent createContent() { |
| 62 |
JPanel content = new JPanel(new BorderLayout()); |
| 63 |
JTabbedPane tabs = new JTabbedPane(); |
| 64 |
tabs.add("Tab 1", new JButton("First Tab")); |
| 65 |
tabs.add("Tab 2", new JButton("Second Tab")); |
| 66 |
JButton button = new JButton("Save to SVG"); |
| 67 |
button.addActionListener(this); |
| 68 |
content.add(tabs); |
| 69 |
content.add(button, BorderLayout.SOUTH); |
| 70 |
return content; |
| 71 |
} |
| 72 |
|
| 73 |
@Override |
| 74 |
public void actionPerformed(ActionEvent e) { |
| 75 |
JComponent c = (JComponent) getContentPane().getComponent(0); |
| 76 |
SVGGraphics2D g2 = new SVGGraphics2D(c.getWidth(), c.getHeight()); |
| 77 |
c.paint(g2); |
| 78 |
File f = new File("SwingUIToSVGDemo.svg"); |
| 79 |
try { |
| 80 |
SVGUtils.writeToSVG(f, g2.getSVGElement()); |
| 81 |
} catch (IOException ex) { |
| 82 |
System.err.println(ex); |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
public static void main(String[] args) { |
| 87 |
try { |
| 88 |
for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) { |
| 89 |
if ("Nimbus".equals(info.getName())) { |
| 90 |
UIManager.setLookAndFeel(info.getClassName()); |
| 91 |
break; |
| 92 |
} |
| 93 |
} |
| 94 |
} catch (Exception e) { |
| 95 |
// just take the default look and feel |
| 96 |
} |
| 97 |
|
| 98 |
SwingUIToSVGDemo app = new SwingUIToSVGDemo("SwingUIToSVGDemo.java"); |
| 99 |
app.pack(); |
| 100 |
app.setVisible(true); |
| 101 |
|
| 102 |
} |
| 103 |
|
| 104 |
} |