diff --git a/examples/src/main/java/edu/umd/cs/piccolo/examples/pswing/PSwingExample1.java b/examples/src/main/java/edu/umd/cs/piccolo/examples/pswing/PSwingExample1.java new file mode 100644 index 0000000..8e879f9 --- /dev/null +++ b/examples/src/main/java/edu/umd/cs/piccolo/examples/pswing/PSwingExample1.java @@ -0,0 +1,176 @@ +/* + * Copyright (c) 2008-2009, Piccolo2D project, http://piccolo2d.org + * Copyright (c) 1998-2008, University of Maryland + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, are permitted provided + * that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice, this list of conditions + * and the following disclaimer. + * + * Redistributions in binary form must reproduce the above copyright notice, this list of conditions + * and the following disclaimer in the documentation and/or other materials provided with the + * distribution. + * + * None of the name of the University of Maryland, the name of the Piccolo2D project, or the names of its + * contributors may be used to endorse or promote products derived from this software without specific + * prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A + * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR + * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package edu.umd.cs.piccolo.examples.pswing; + +import java.awt.Color; +import java.awt.Dimension; +import java.awt.Font; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; + +import javax.swing.BorderFactory; +import javax.swing.JButton; +import javax.swing.JCheckBox; +import javax.swing.JColorChooser; +import javax.swing.JFrame; +import javax.swing.JPanel; +import javax.swing.JScrollPane; +import javax.swing.JSlider; +import javax.swing.JSpinner; +import javax.swing.JTextArea; +import javax.swing.JTree; +import javax.swing.border.LineBorder; +import javax.swing.event.ChangeEvent; +import javax.swing.event.ChangeListener; + +import edu.umd.cs.piccolo.PNode; +import edu.umd.cs.piccolo.event.PZoomEventHandler; +import edu.umd.cs.piccolo.nodes.PText; +import edu.umd.cs.piccolox.pswing.PComboBox; +import edu.umd.cs.piccolox.pswing.PSwing; +import edu.umd.cs.piccolox.pswing.PSwingCanvas; + +/** + * User: Sam Reid Date: Jul 11, 2005 Time: 12:15:55 PM + */ + +public class PSwingExample1 { + public static void main(String[] args) { + PSwingCanvas pCanvas = new PSwingCanvas(); + final PText pText = new PText("PText"); + pCanvas.getLayer().addChild(pText); + JFrame frame = new JFrame("Test Piccolo"); + + frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + frame.setContentPane(pCanvas); + frame.setSize(600, 800); + frame.setVisible(true); + + PText text2 = new PText("Text2"); + text2.setFont(new Font("Lucida Sans", Font.BOLD, 18)); + pCanvas.getLayer().addChild(text2); + text2.translate(100, 100); + text2.addInputEventListener(new PZoomEventHandler()); + + pCanvas.removeInputEventListener(pCanvas.getPanEventHandler()); + + JButton jButton = new JButton("MyButton!"); + jButton.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + System.out.println("TestZSwing.actionPerformed!!!!!!!!!!!!!!*********************"); + } + }); + final PSwing pSwing = new PSwing(jButton); + pCanvas.getLayer().addChild(pSwing); + pSwing.repaint(); + + JSpinner jSpinner = new JSpinner(); + jSpinner.setPreferredSize(new Dimension(100, jSpinner.getPreferredSize().height)); + PSwing pSpinner = new PSwing(jSpinner); + pCanvas.getLayer().addChild(pSpinner); + pSpinner.translate(0, 150); + + JCheckBox jcb = new JCheckBox("CheckBox", true); + jcb.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + System.out.println("TestZSwing.JCheckBox.actionPerformed"); + } + }); + jcb.addChangeListener(new ChangeListener() { + public void stateChanged(ChangeEvent e) { + System.out.println("TestPSwing.JChekbox.stateChanged@" + System.currentTimeMillis()); + } + }); + PSwing pCheckBox = new PSwing(jcb); + pCanvas.getLayer().addChild(pCheckBox); + pCheckBox.translate(100, 0); + + // Growable JTextArea + JTextArea textArea = new JTextArea("This is a growable TextArea.\nTry it out!"); + textArea.setBorder(new LineBorder(Color.blue, 3)); + PSwing swing = new PSwing(textArea); + swing.translate(150, 150); + pCanvas.getLayer().addChild(swing); + + // A Slider + JSlider slider = new JSlider(); + PSwing pSlider = new PSwing(slider); + pSlider.translate(200, 200); + pCanvas.getLayer().addChild(pSlider); + + // A Scrollable JTree + JTree tree = new JTree(); + tree.setEditable(true); + JScrollPane p = new JScrollPane(tree); + p.setPreferredSize(new Dimension(150, 150)); + PSwing pTree = new PSwing(p); + pCanvas.getLayer().addChild(pTree); + pTree.translate(0, 250); + + // A JColorChooser - also demonstrates JTabbedPane + JColorChooser chooser = new JColorChooser(); + PSwing pChooser = new PSwing(chooser); + pCanvas.getLayer().addChild(pChooser); + pChooser.translate(100, 300); + + JPanel myPanel = new JPanel(); + myPanel.setBorder(BorderFactory.createTitledBorder("Titled Border")); + myPanel.add(new JCheckBox("CheckBox")); + PSwing panelSwing = new PSwing(myPanel); + pCanvas.getLayer().addChild(panelSwing); + panelSwing.translate(400, 50); + + // A Slider + JSlider slider2 = new JSlider(); + PSwing pSlider2 = new PSwing(slider2); + pSlider2.translate(200, 200); + PNode root = new PNode(); + root.addChild(pSlider2); + root.scale(1.5); + root.rotate(Math.PI / 4); + root.translate(300, 200); + pCanvas.getLayer().addChild(root); + + // A Combo Box + JPanel comboPanel = new JPanel(); + comboPanel.setBorder( BorderFactory.createTitledBorder( "Combo Box" ) ); + String[] listItems = { "Summer Teeth", "Mermaid Avenue", "Being There", "A.M." }; + PComboBox box = new PComboBox(listItems); + comboPanel.add(box); + swing = new PSwing(comboPanel); + swing.translate( 200, 230 ); + pCanvas.getLayer().addChild(swing); + box.setEnvironment(swing, pCanvas);// has to be done manually at present + + // Revalidate and repaint + pCanvas.revalidate(); + pCanvas.repaint(); + } + +} diff --git a/examples/src/main/java/edu/umd/cs/piccolo/examples/pswing/PSwingExample2.java b/examples/src/main/java/edu/umd/cs/piccolo/examples/pswing/PSwingExample2.java new file mode 100644 index 0000000..9b5d601 --- /dev/null +++ b/examples/src/main/java/edu/umd/cs/piccolo/examples/pswing/PSwingExample2.java @@ -0,0 +1,468 @@ +/* + * Copyright (c) 2008-2009, Piccolo2D project, http://piccolo2d.org + * Copyright (c) 1998-2008, University of Maryland + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, are permitted provided + * that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice, this list of conditions + * and the following disclaimer. + * + * Redistributions in binary form must reproduce the above copyright notice, this list of conditions + * and the following disclaimer in the documentation and/or other materials provided with the + * distribution. + * + * None of the name of the University of Maryland, the name of the Piccolo2D project, or the names of its + * contributors may be used to endorse or promote products derived from this software without specific + * prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A + * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR + * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package edu.umd.cs.piccolo.examples.pswing; + +import java.awt.BorderLayout; +import java.awt.Color; +import java.awt.Cursor; +import java.awt.Dimension; +import java.awt.event.ActionEvent; +import java.io.IOException; +import java.util.Vector; + +import javax.swing.AbstractAction; +import javax.swing.Action; +import javax.swing.JButton; +import javax.swing.JCheckBox; +import javax.swing.JComponent; +import javax.swing.JEditorPane; +import javax.swing.JFrame; +import javax.swing.JInternalFrame; +import javax.swing.JLabel; +import javax.swing.JList; +import javax.swing.JOptionPane; +import javax.swing.JPanel; +import javax.swing.JScrollPane; +import javax.swing.JSlider; +import javax.swing.JSpinner; +import javax.swing.JSplitPane; +import javax.swing.JTabbedPane; +import javax.swing.JTable; +import javax.swing.JTextArea; +import javax.swing.JTextField; +import javax.swing.JToolBar; +import javax.swing.JTree; +import javax.swing.SpinnerNumberModel; +import javax.swing.SwingConstants; +import javax.swing.border.EmptyBorder; +import javax.swing.border.EtchedBorder; +import javax.swing.border.LineBorder; +import javax.swing.border.TitledBorder; +import javax.swing.event.HyperlinkEvent; +import javax.swing.event.HyperlinkListener; +import javax.swing.table.TableColumn; + +import edu.umd.cs.piccolo.PNode; +import edu.umd.cs.piccolox.pswing.PSwing; +import edu.umd.cs.piccolox.pswing.PSwingCanvas; + +/** + * User: Sam Reid Date: Jul 11, 2005 Time: 12:15:55 PM + */ + +public class PSwingExample2 extends JFrame { + public PSwingExample2() { + setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + ClassLoader loader; + PSwingCanvas canvas; + + // Set up basic frame + setBounds(50, 50, 750, 750); + setResizable(true); + setBackground(null); + setVisible(true); + canvas = new PSwingCanvas(); + canvas.setPanEventHandler(null); + getContentPane().add(canvas); + validate(); + loader = getClass().getClassLoader(); + + ZVisualLeaf leaf; + PNode transform; + PSwing swing; + PSwing swing2; + + // JButton + JButton button = new JButton("Button"); + button.setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR)); + swing = new PSwing(button); + leaf = new ZVisualLeaf(swing); + transform = new PNode(); + transform.translate(-500, -500); + transform.addChild(leaf); + canvas.getLayer().addChild(transform); + + // JButton + JSpinner spinner = new JSpinner(new SpinnerNumberModel(0, 0, 10, 1)); + spinner.setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR)); + swing = new PSwing(spinner); + leaf = new ZVisualLeaf(swing); + transform = new PNode(); + transform.translate(-800, -500); + transform.addChild(leaf); + canvas.getLayer().addChild(transform); + + // 2nd Copy of JButton + leaf = new ZVisualLeaf(swing); + transform = new PNode(); + transform.translate(-450, -450); + transform.rotate(Math.PI / 2); + transform.scale(0.5); + transform.addChild(leaf); + canvas.getLayer().addChild(transform); + + // Growable JTextArea + JTextArea textArea = new JTextArea("This is a growable TextArea.\nTry it out!"); + textArea.setBorder(new LineBorder(Color.blue, 3)); + swing = new PSwing(textArea); + leaf = new ZVisualLeaf(swing); + transform = new PNode(); + transform.translate(-250, -500); + transform.addChild(leaf); + canvas.getLayer().addChild(transform); + + // Growable JTextField + JTextField textField = new JTextField("A growable text field"); + swing = new PSwing(textField); + leaf = new ZVisualLeaf(swing); + transform = new PNode(); + transform.translate(0, -500); + transform.addChild(leaf); + canvas.getLayer().addChild(transform); + + // A Slider + JSlider slider = new JSlider(); + swing = new PSwing(slider); + leaf = new ZVisualLeaf(swing); + transform = new PNode(); + transform.translate(250, -500); + transform.addChild(leaf); + canvas.getLayer().addChild(transform); + + // A Scrollable JTree + JTree tree = new JTree(); + tree.setEditable(true); + JScrollPane p = new JScrollPane(tree); + p.setPreferredSize(new Dimension(150, 150)); + swing = new PSwing(p); + leaf = new ZVisualLeaf(swing); + transform = new PNode(); + transform.translate(-500, -250); + transform.addChild(leaf); + canvas.getLayer().addChild(transform); + + // A Scrollable JTextArea + JScrollPane pane = new JScrollPane(new JTextArea("A Scrollable Text Area\nTry it out!")); + pane.setPreferredSize(new Dimension(150, 150)); + swing = new PSwing(pane); + leaf = new ZVisualLeaf(swing); + transform = new PNode(); + transform.translate(-250, -250); + transform.addChild(leaf); + canvas.getLayer().addChild(transform); + swing2 = swing; + + // A non-scrollable JTextField + // A panel MUST be created with double buffering off + JPanel panel = new JPanel(false); + textField = new JTextField("A fixed-size text field"); + panel.setLayout(new BorderLayout()); + panel.add(textField); + swing = new PSwing(panel); + leaf = new ZVisualLeaf(swing); + transform = new PNode(); + transform.translate(0, -250); + transform.addChild(leaf); + canvas.getLayer().addChild(transform); + + // // A JComboBox + // String[] listItems = {"Summer Teeth", "Mermaid Avenue", + // "Being There", "A.M."}; + // ZComboBox box = new ZComboBox( listItems ); + // swing = new PSwing( canvas, box ); + // leaf = new ZVisualLeaf( swing ); + // transform = new PNode(); + // transform.translate( 0, -150 ); + // transform.addChild( leaf ); + // canvas.getLayer().addChild( transform ); + + // A panel with TitledBorder and JList + panel = new JPanel(false); + panel.setBackground(Color.lightGray); + panel.setLayout(new BorderLayout()); + panel.setBorder(new TitledBorder(new EtchedBorder(EtchedBorder.RAISED), "A JList", TitledBorder.LEFT, + TitledBorder.TOP)); + panel.setPreferredSize(new Dimension(200, 200)); + Vector data = new Vector(); + data.addElement("Choice 1"); + data.addElement("Choice 2"); + data.addElement("Choice 3"); + data.addElement("Choice 4"); + data.addElement("Choice 5"); + JList list = new JList(data); + list.setBackground(Color.lightGray); + panel.add(list); + swing = new PSwing(panel); + leaf = new ZVisualLeaf(swing); + transform = new PNode(); + transform.translate(250, -250); + transform.addChild(leaf); + canvas.getLayer().addChild(transform); + + // A JLabel + JLabel label = new JLabel("A JLabel", SwingConstants.CENTER); + + swing = new PSwing(label); + leaf = new ZVisualLeaf(swing); + transform = new PNode(); + transform.translate(-500, 0); + transform.addChild(leaf); + canvas.getLayer().addChild(transform); + + // Rotated copy of the Scrollable JTextArea + leaf = new ZVisualLeaf(swing2); + transform = new PNode(); + transform.translate(-100, 0); + transform.rotate(Math.PI / 2); + transform.addChild(leaf); + canvas.getLayer().addChild(transform); + + // A panel with layout + // A panel MUST be created with double buffering off + panel = new JPanel(false); + panel.setLayout(new BorderLayout()); + JButton button1 = new JButton("Button 1"); + JButton button2 = new JButton("Button 2"); + label = new JLabel("A Panel with Layout"); + label.setHorizontalAlignment(SwingConstants.CENTER); + label.setForeground(Color.white); + panel.setBackground(Color.red); + panel.setPreferredSize(new Dimension(150, 150)); + panel.setBorder(new EmptyBorder(5, 5, 5, 5)); + panel.add(button1, "North"); + panel.add(button2, "South"); + panel.add(label, "Center"); + panel.revalidate(); + swing = new PSwing(panel); + leaf = new ZVisualLeaf(swing); + transform = new PNode(); + transform.translate(0, 0); + transform.addChild(leaf); + canvas.getLayer().addChild(transform); + + // JTable Example + Vector columns = new Vector(); + columns.addElement("Check Number"); + columns.addElement("Description"); + columns.addElement("Amount"); + Vector rows = new Vector(); + Vector row = new Vector(); + row.addElement("101"); + row.addElement("Sandwich"); + row.addElement("$20.00"); + rows.addElement(row); + row = new Vector(); + row.addElement("102"); + row.addElement("Monkey Wrench"); + row.addElement("$100.00"); + rows.addElement(row); + row = new Vector(); + row.addElement("214"); + row.addElement("Ant farm"); + row.addElement("$55.00"); + rows.addElement(row); + row = new Vector(); + row.addElement("215"); + row.addElement("Self-esteem tapes"); + row.addElement("$37.99"); + rows.addElement(row); + row = new Vector(); + row.addElement("216"); + row.addElement("Tube Socks"); + row.addElement("$7.45"); + rows.addElement(row); + row = new Vector(); + row.addElement("220"); + row.addElement("Ab Excerciser"); + row.addElement("$56.95"); + rows.addElement(row); + row = new Vector(); + row.addElement("319"); + row.addElement("Y2K Supplies"); + row.addElement("$4624.33"); + rows.addElement(row); + row = new Vector(); + row.addElement("332"); + row.addElement("Tie Rack"); + row.addElement("$15.20"); + rows.addElement(row); + row = new Vector(); + row.addElement("344"); + row.addElement("Swing Set"); + row.addElement("$146.59"); + rows.addElement(row); + JTable table = new JTable(rows, columns); + table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF); + table.setRowHeight(30); + TableColumn c = table.getColumn(table.getColumnName(0)); + c.setPreferredWidth(150); + c = table.getColumn(table.getColumnName(1)); + c.setPreferredWidth(150); + c = table.getColumn(table.getColumnName(2)); + c.setPreferredWidth(150); + pane = new JScrollPane(table); + pane.setPreferredSize(new Dimension(200, 200)); + table.setDoubleBuffered(false); + swing = new PSwing(pane); + leaf = new ZVisualLeaf(swing); + transform = new PNode(); + transform.translate(250, 0); + transform.addChild(leaf); + canvas.getLayer().addChild(transform); + + // JEditorPane - HTML example + try { + + final JEditorPane editorPane = new JEditorPane(loader.getResource("csdept.html")); + editorPane.setDoubleBuffered(false); + editorPane.setEditable(false); + pane = new JScrollPane(editorPane); + pane.setDoubleBuffered(false); + pane.setPreferredSize(new Dimension(400, 400)); + editorPane.addHyperlinkListener(new HyperlinkListener() { + public void hyperlinkUpdate(HyperlinkEvent e) { + if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) { + try { + editorPane.setPage(e.getURL()); + } + catch (IOException ioe) { + System.out.println("Couldn't Load Web Page"); + } + } + } + }); + swing = new PSwing(pane); + leaf = new ZVisualLeaf(swing); + transform = new PNode(); + transform.translate(-500, 250); + transform.addChild(leaf); + canvas.getLayer().addChild(transform); + + } + catch (IOException ioe) { + System.out.println("Couldn't Load Web Page"); + } + + // A JInternalFrame with a JSplitPane - a JOptionPane - and a + // JToolBar + JInternalFrame iframe = new JInternalFrame("JInternalFrame"); + iframe.getRootPane().setDoubleBuffered(false); + ((JComponent) iframe.getContentPane()).setDoubleBuffered(false); + iframe.setPreferredSize(new Dimension(500, 500)); + JTabbedPane tabby = new JTabbedPane(); + tabby.setDoubleBuffered(false); + iframe.getContentPane().setLayout(new BorderLayout()); + JOptionPane options = new JOptionPane("This is a JOptionPane!", JOptionPane.INFORMATION_MESSAGE, + JOptionPane.DEFAULT_OPTION); + options.setDoubleBuffered(false); + options.setMinimumSize(new Dimension(50, 50)); + options.setPreferredSize(new Dimension(225, 225)); + JPanel tools = new JPanel(false); + tools.setMinimumSize(new Dimension(150, 150)); + tools.setPreferredSize(new Dimension(225, 225)); + JToolBar bar = new JToolBar(); + Action letter = new AbstractAction("Big A!") { + + public void actionPerformed(ActionEvent e) { + } + }; + + Action hand = new AbstractAction("Hi!") { + public void actionPerformed(ActionEvent e) { + } + }; + Action select = new AbstractAction("There!") { + public void actionPerformed(ActionEvent e) { + } + }; + + label = new JLabel("A Panel with a JToolBar"); + label.setHorizontalAlignment(SwingConstants.CENTER); + bar.add(letter); + bar.add(hand); + bar.add(select); + bar.setFloatable(false); + bar.setBorder(new LineBorder(Color.black, 2)); + tools.setLayout(new BorderLayout()); + tools.add(bar, "North"); + tools.add(label, "Center"); + + JSplitPane split = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, options, tools); + split.setDoubleBuffered(false); + iframe.getContentPane().add(split); + swing = new PSwing(iframe); + leaf = new ZVisualLeaf(swing); + transform = new PNode(); + transform.translate(0, 250); + transform.addChild(leaf); + canvas.getLayer().addChild(transform); + + // JMenuBar menuBar = new JMenuBar(); + // ZMenu menu = new ZMenu( "File" ); + // ZMenu sub = new ZMenu( "Export" ); + // JMenuItem gif = new JMenuItem( "Funds" ); + // sub.add( gif ); + // menu.add( sub ); + // menuBar.add( menu ); + // iframe.setJMenuBar( menuBar ); + + iframe.setVisible(true); + + // A JColorChooser - also demonstrates JTabbedPane + // JColorChooser chooser = new JColorChooser(); + JCheckBox chooser = new JCheckBox("Check Box"); + swing = new PSwing(chooser); + leaf = new ZVisualLeaf(swing); + transform = new PNode(); + transform.translate(-250, 850); + transform.addChild(leaf); + canvas.getLayer().addChild(transform); + + // Revalidate and repaint + canvas.revalidate(); + canvas.repaint(); + + PSwing message = new PSwing(new JTextArea("Click-drag to zoom in and out.")); + message.translate(0, -50); + canvas.getLayer().addChild(message); + + canvas.getCamera().animateViewToCenterBounds(message.getFullBounds(), false, 1200); + } + + public static void main(String[] args) { + new PSwingExample2().setVisible(true); + } + + public static class ZVisualLeaf extends PNode { + public ZVisualLeaf(PNode node) { + addChild(node); + } + } + +} diff --git a/examples/src/main/java/edu/umd/cs/piccolo/examples/swt/SWTBasicExample.java b/examples/src/main/java/edu/umd/cs/piccolo/examples/swt/SWTBasicExample.java new file mode 100644 index 0000000..27b495d --- /dev/null +++ b/examples/src/main/java/edu/umd/cs/piccolo/examples/swt/SWTBasicExample.java @@ -0,0 +1,97 @@ +/* + * Copyright (c) 2008-2009, Piccolo2D project, http://piccolo2d.org + * Copyright (c) 1998-2008, University of Maryland + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, are permitted provided + * that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice, this list of conditions + * and the following disclaimer. + * + * Redistributions in binary form must reproduce the above copyright notice, this list of conditions + * and the following disclaimer in the documentation and/or other materials provided with the + * distribution. + * + * None of the name of the University of Maryland, the name of the Piccolo2D project, or the names of its + * contributors may be used to endorse or promote products derived from this software without specific + * prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A + * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR + * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package edu.umd.cs.piccolo.examples.swt; + +import java.awt.Color; + +import org.eclipse.swt.layout.FillLayout; +import org.eclipse.swt.widgets.Display; +import org.eclipse.swt.widgets.Shell; + +import edu.umd.cs.piccolox.swt.PSWTCanvas; +import edu.umd.cs.piccolox.swt.PSWTPath; +import edu.umd.cs.piccolox.swt.PSWTText; + +/** + * @author good + */ +public class SWTBasicExample { + + /** + * Constructor for SWTBasicExample. + */ + public SWTBasicExample() { + super(); + } + + public static void main(String[] args) { + Display display = new Display(); + Shell shell = open(display); + while (!shell.isDisposed()) { + if (!display.readAndDispatch()) + display.sleep(); + } + display.dispose(); + } + + public static Shell open(Display display) { + final Shell shell = new Shell(display); + shell.setLayout(new FillLayout()); + PSWTCanvas canvas = new PSWTCanvas(shell, 0); + + PSWTPath rect = PSWTPath.createRectangle(25, 25, 50, 50); + rect.setPaint(Color.red); + canvas.getLayer().addChild(rect); + + rect = PSWTPath.createRectangle(300, 25, 100, 50); + rect.setPaint(Color.blue); + canvas.getLayer().addChild(rect); + + PSWTPath circle = PSWTPath.createEllipse(100, 200, 50, 50); + circle.setPaint(Color.green); + canvas.getLayer().addChild(circle); + + circle = PSWTPath.createEllipse(400, 400, 75, 150); + circle.setPaint(Color.yellow); + canvas.getLayer().addChild(circle); + + PSWTText text = new PSWTText("Hello World"); + text.translate(350, 150); + text.setPenColor(Color.gray); + canvas.getLayer().addChild(text); + + text = new PSWTText("Goodbye World"); + text.translate(50, 400); + text.setPenColor(Color.magenta); + canvas.getLayer().addChild(text); + + shell.open(); + return shell; + } +} diff --git a/examples/src/main/java/edu/umd/cs/piccolo/examples/swt/SWTBenchTest.java b/examples/src/main/java/edu/umd/cs/piccolo/examples/swt/SWTBenchTest.java new file mode 100644 index 0000000..9aee2f9 --- /dev/null +++ b/examples/src/main/java/edu/umd/cs/piccolo/examples/swt/SWTBenchTest.java @@ -0,0 +1,453 @@ +/* + * Copyright (c) 2008-2009, Piccolo2D project, http://piccolo2d.org + * Copyright (c) 1998-2008, University of Maryland + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, are permitted provided + * that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice, this list of conditions + * and the following disclaimer. + * + * Redistributions in binary form must reproduce the above copyright notice, this list of conditions + * and the following disclaimer in the documentation and/or other materials provided with the + * distribution. + * + * None of the name of the University of Maryland, the name of the Piccolo2D project, or the names of its + * contributors may be used to endorse or promote products derived from this software without specific + * prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A + * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR + * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package edu.umd.cs.piccolo.examples.swt; + +import java.util.Random; +import java.io.*; +import java.awt.*; +import java.awt.geom.*; + +import org.eclipse.swt.SWT; +import org.eclipse.swt.events.PaintEvent; +import org.eclipse.swt.events.PaintListener; +import org.eclipse.swt.graphics.GC; +import org.eclipse.swt.graphics.Image; +import org.eclipse.swt.graphics.ImageData; +import org.eclipse.swt.graphics.Point; +import org.eclipse.swt.graphics.Rectangle; +import org.eclipse.swt.layout.FillLayout; +import org.eclipse.swt.widgets.Canvas; +import org.eclipse.swt.widgets.Composite; +import org.eclipse.swt.widgets.Display; +import org.eclipse.swt.widgets.Shell; + +import edu.umd.cs.piccolox.swt.SWTGraphics2D; + +/** + * Benchmarking test suite for SWT package + */ +public class SWTBenchTest extends Canvas { + + // Paths + GeneralPath testShape = new GeneralPath(); + + // Images + Image testImageOpaque, testImageBitmask, testImageTranslucent, testImageARGB; + + // Transforms + AffineTransform transform = new AffineTransform(); + static final AffineTransform IDENTITY = new AffineTransform(); + + // Geometry + double pts[] = new double[20]; + + // Colors + static final Color colors[] = { Color.red, Color.green, Color.blue, Color.white, Color.yellow, }; + + // Flags + boolean offscreen; + boolean antialiased; + + // Statistics + int results[][] = new int[NUM_CONTEXTS][NUM_TESTS]; + + // Constants + + static final int CTX_NORMAL = 0; + // static final int CTX_CLIPPED = 1; + static final int CTX_TRANSFORMED = 1; + // static final int CTX_BLENDED = 3; + static final int NUM_CONTEXTS = 2; + + // static String contextNames[] = { + // "normal", + // "clip", + // "transform", + // "alpha", + // }; + + static String contextNames[] = { "normal", "transform" }; + + // + // TEST METHODS + // + + static final int DRAW_LINE = 0; + static final int DRAW_RECT = 1; + static final int FILL_RECT = 2; + static final int DRAW_OVAL = 3; + static final int FILL_OVAL = 4; + static final int DRAW_POLY = 5; + static final int FILL_POLY = 6; + static final int DRAW_TEXT = 7; + static final int DRAW_IMG1 = 8; + static final int DRAW_IMG2 = 9; + static final int DRAW_IMG3 = 10; + static final int DRAW_IMG4 = 11; + static final int DRAW_IMG5 = 12; + static final int NUM_TESTS = 13; + + static String testNames[] = { "line", "rect", "fill rect", "oval", "fill oval", "poly", "fill poly", "text", + "image", "scaled image", "mask image", "alpha image", "argb image", }; + + void testDrawLine(SWTGraphics2D g, Random r) { + g.drawLine(rand(r), rand(r), rand(r), rand(r)); + } + + void testDrawRect(SWTGraphics2D g, Random r) { + g.drawRect(rand(r), rand(r), rand(r), rand(r)); + } + + void testFillRect(SWTGraphics2D g, Random r) { + g.fillRect(rand(r), rand(r), rand(r), rand(r)); + } + + void testDrawOval(SWTGraphics2D g, Random r) { + g.drawOval(rand(r), rand(r), rand(r), rand(r)); + } + + void testFillOval(SWTGraphics2D g, Random r) { + g.fillOval(rand(r), rand(r), rand(r), rand(r)); + } + + void genPoly(Random r) { + for (int i = 0; i < pts.length / 2; i++) { + pts[2 * i] = rand(r); + pts[2 * i + 1] = rand(r); + } + } + + void testDrawPoly(SWTGraphics2D g, Random r) { + genPoly(r); + g.drawPolyline(pts); + } + + void testFillPoly(SWTGraphics2D g, Random r) { + genPoly(r); + g.fillPolygon(pts); + } + + void testDrawText(SWTGraphics2D g, Random r) { + g.drawString("Abcdefghijklmnop", rand(r), rand(r)); + } + + // Basic image + void testDrawImg1(SWTGraphics2D g, Random r) { + g.drawImage(testImageOpaque, rand(r), rand(r)); + } + + // Scaled image + void testDrawImg2(SWTGraphics2D g, Random r) { + Rectangle rect = testImageOpaque.getBounds(); + g.drawImage(testImageOpaque, 0, 0, rect.width, rect.height, rand(r), rand(r), rand(r), rand(r)); + } + + // Bitmask image (unscaled) + void testDrawImg3(SWTGraphics2D g, Random r) { + g.drawImage(testImageBitmask, rand(r), rand(r)); + } + + // Translucent image (unscaled) + void testDrawImg4(SWTGraphics2D g, Random r) { + g.drawImage(testImageTranslucent, rand(r), rand(r)); + } + + // Buffered image (unscaled) + void testDrawImg5(SWTGraphics2D g, Random r) { + g.drawImage(testImageARGB, rand(r), rand(r)); + } + + Image loadImage(Display display, String name) { + try { + InputStream stream = SWTBenchTest.class.getResourceAsStream(name); + if (stream != null) { + ImageData imageData = new ImageData(stream); + return new Image(display, imageData); + // if (imageData != null) { + // ImageData mask = imageData.getTransparencyMask(); + // return new Image(display, imageData, mask); + // } + + } + } + catch (Exception e) { + } + return null; + } + + SWTBenchTest(Composite parent, int style) { + super(parent, style); + + testImageOpaque = loadImage(getDisplay(), "opaque.jpg"); + testImageBitmask = loadImage(getDisplay(), "bitmask.gif"); + testImageTranslucent = loadImage(getDisplay(), "translucent.png"); + testImageARGB = new Image(getDisplay(), 128, 128); + + GC tmpGC = new GC(testImageARGB); + tmpGC.drawImage(testImageTranslucent, 0, 0); + tmpGC.dispose(); + + addPaintListener(new PaintListener() { + public void paintControl(PaintEvent pe) { + runAll(new SWTGraphics2D(pe.gc, getDisplay())); + } + }); + } + + void setupTransform(Graphics2D g, Random r) { + transform.setToIdentity(); + + switch (abs(r.nextInt()) % 5) { + default: + // case 0: // UNIFORM SCALE + double s = r.nextDouble(); + transform.scale(5 * s + 0.1, 5 * s + 0.1); + break; + // case 1: // NON-UNIFORM SCALE + // transform.scale(5 * r.nextDouble() + 0.1, 5 * r.nextDouble() + + // 0.1); + // break; + // case 2: // ROTATION + // transform.rotate(r.nextDouble() * Math.PI * 2); + // break; + // case 3: // TRANSLATION + // transform.translate(r.nextDouble() * 500, r.nextDouble() * 500); + // break; + // case 4: // TRANSLATE + ROTATE + SCALE + // s = r.nextDouble(); + // transform.translate(r.nextDouble() * 500, r.nextDouble() * 500); + // transform.rotate(r.nextDouble() * Math.PI * 2); + // transform.scale(5*s + 0.1, 5*s + 0.1); + // break; + } + + g.setTransform(transform); + } + + void setupClip(Graphics2D g, Random r) { + // g.setClip(rand(r), rand(r), rand(r), rand(r)); + } + + void setupBlend(Graphics2D g, Random r) { + g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, r.nextFloat())); + } + + void setup(int ctx, Graphics2D g, Random r) { + switch (ctx) { + case CTX_NORMAL: + break; + + case CTX_TRANSFORMED: + setupTransform(g, r); + break; + + // case CTX_CLIPPED: + // setupClip(g, r); + // break; + // + // case CTX_BLENDED: + // setupBlend(g, r); + // break; + } + } + + void test(int testNum, SWTGraphics2D g, Random r) { + + g.setColor(colors[abs(r.nextInt()) % colors.length]); + g.setBackground(colors[abs(r.nextInt()) % colors.length]); + + switch (testNum) { + case DRAW_LINE: + testDrawLine(g, r); + break; + case DRAW_RECT: + testDrawRect(g, r); + break; + case FILL_RECT: + testFillRect(g, r); + break; + case DRAW_OVAL: + testDrawOval(g, r); + break; + case FILL_OVAL: + testFillOval(g, r); + break; + case DRAW_POLY: + testDrawPoly(g, r); + break; + case FILL_POLY: + testFillPoly(g, r); + break; + case DRAW_TEXT: + testDrawText(g, r); + break; + case DRAW_IMG1: + testDrawImg1(g, r); + break; + case DRAW_IMG2: + testDrawImg2(g, r); + break; + case DRAW_IMG3: + testDrawImg3(g, r); + break; + case DRAW_IMG4: + testDrawImg4(g, r); + break; + case DRAW_IMG5: + testDrawImg5(g, r); + break; + } + } + + void runTest(SWTGraphics2D g, int ctx, int testNum) { + Random r1 = new Random(1); + Random r2 = new Random(1); + + System.out.println("Test: " + testNames[testNum]); + long t1 = System.currentTimeMillis(); + int i = 0; + while (true) { + if (i % 10 == 0) + setup(ctx, g, r1); + test(testNum, g, r2); + i++; + long t2 = System.currentTimeMillis(); + if (t2 - t1 >= 5000) { + break; + } + } + results[ctx][testNum] += i / 5; + System.out.println("Shapes per second: " + (results[ctx][testNum])); + } + + void runAll(SWTGraphics2D g) { + System.out.println("BENCHMARKING: " + g); + + if (antialiased) { + System.out.println("ANTIALIASED"); + g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); + g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON); + } + + for (int ctx = 0; ctx < NUM_CONTEXTS; ctx++) { + System.out.println("Context: " + contextNames[ctx]); + for (int i = 0; i < NUM_TESTS; i++) { + g.setClip(null); + g.setTransform(IDENTITY); + runTest(g, ctx, i); + } + } + + if (offscreen) { + g.dispose(); + } + + String fileName = g.getClass().getName().replace('.', '_'); + if (offscreen) + fileName += "-offscreen"; + if (antialiased) + fileName += "-antialiased"; + dumpResults(fileName + ".txt"); + System.exit(0); + } + + void dumpResults(String fileName) { + try { + FileOutputStream fout = new FileOutputStream(fileName); + PrintWriter out = new PrintWriter(fout); + out.print('\t'); + for (int i = 0; i < NUM_TESTS; i++) { + out.print(testNames[i]); + out.print('\t'); + } + out.println(""); + for (int ctx = 0; ctx < NUM_CONTEXTS; ctx++) { + out.print(contextNames[ctx]); + for (int i = 0; i < NUM_TESTS; i++) { + out.print('\t'); + out.print(results[ctx][i]); + } + out.println(""); + } + out.close(); + results = new int[NUM_CONTEXTS][NUM_TESTS]; + } + catch (IOException e) { + e.printStackTrace(); + System.exit(1); + } + } + + public Point computeSize(int wHint, int hHint) { + return new Point(512, 512); + } + + public Point computeSize(int wHint, int hHint, boolean changed) { + return computeSize(wHint, hHint); + } + + final static int abs(int x) { + return (x < 0 ? -x : x); + } + + final static double rand(Random r) { + return abs(r.nextInt()) % 500; + } + + public static void main(String args[]) { + // Create frame + Display display = new Display(); + Shell shell = new Shell(display); + shell.setLayout(new FillLayout()); + + // Add bench test + SWTBenchTest m = new SWTBenchTest(shell, SWT.NO_REDRAW_RESIZE | SWT.NO_BACKGROUND); + m.setSize(512, 512); + for (int i = 0; i < args.length; i++) { + if (args[i].intern() == "-offscreen") + m.offscreen = true; + else if (args[i].intern() == "-anti") + m.antialiased = true; + else { + System.out.println("Usage: java BenchTest [-anti] [-offscreen]"); + System.exit(1); + } + } + + shell.pack(); + shell.open(); + + while (!shell.isDisposed()) { + if (!display.readAndDispatch()) + display.sleep(); + } + display.dispose(); + } + +} \ No newline at end of file diff --git a/examples/src/main/java/edu/umd/cs/piccolo/examples/swt/SWTHelloWorld.java b/examples/src/main/java/edu/umd/cs/piccolo/examples/swt/SWTHelloWorld.java new file mode 100644 index 0000000..a20dcf4 --- /dev/null +++ b/examples/src/main/java/edu/umd/cs/piccolo/examples/swt/SWTHelloWorld.java @@ -0,0 +1,71 @@ +/* + * Copyright (c) 2008-2009, Piccolo2D project, http://piccolo2d.org + * Copyright (c) 1998-2008, University of Maryland + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, are permitted provided + * that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice, this list of conditions + * and the following disclaimer. + * + * Redistributions in binary form must reproduce the above copyright notice, this list of conditions + * and the following disclaimer in the documentation and/or other materials provided with the + * distribution. + * + * None of the name of the University of Maryland, the name of the Piccolo2D project, or the names of its + * contributors may be used to endorse or promote products derived from this software without specific + * prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A + * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR + * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package edu.umd.cs.piccolo.examples.swt; + +import org.eclipse.swt.layout.FillLayout; +import org.eclipse.swt.widgets.Display; +import org.eclipse.swt.widgets.Shell; + +import edu.umd.cs.piccolox.swt.PSWTCanvas; +import edu.umd.cs.piccolox.swt.PSWTText; + +/** + * @author good + */ +public class SWTHelloWorld { + + /** + * Constructor for SWTBasicExample. + */ + public SWTHelloWorld() { + super(); + } + + public static void main(String[] args) { + Display display = new Display(); + Shell shell = open(display); + while (!shell.isDisposed()) { + if (!display.readAndDispatch()) + display.sleep(); + } + display.dispose(); + } + + public static Shell open(Display display) { + final Shell shell = new Shell(display); + shell.setLayout(new FillLayout()); + PSWTCanvas canvas = new PSWTCanvas(shell, 0); + + PSWTText text = new PSWTText("Hello World"); + canvas.getLayer().addChild(text); + + shell.open(); + return shell; + } +} diff --git a/examples/src/main/java/edu/umd/cs/piccolo/examples/swt/bitmask.gif b/examples/src/main/java/edu/umd/cs/piccolo/examples/swt/bitmask.gif new file mode 100644 index 0000000..053a5da --- /dev/null +++ b/examples/src/main/java/edu/umd/cs/piccolo/examples/swt/bitmask.gif Binary files differ diff --git a/examples/src/main/java/edu/umd/cs/piccolo/examples/swt/opaque.jpg b/examples/src/main/java/edu/umd/cs/piccolo/examples/swt/opaque.jpg new file mode 100644 index 0000000..2ff5ba6 --- /dev/null +++ b/examples/src/main/java/edu/umd/cs/piccolo/examples/swt/opaque.jpg Binary files differ diff --git a/examples/src/main/java/edu/umd/cs/piccolo/examples/swt/translucent.png b/examples/src/main/java/edu/umd/cs/piccolo/examples/swt/translucent.png new file mode 100644 index 0000000..07601a7 --- /dev/null +++ b/examples/src/main/java/edu/umd/cs/piccolo/examples/swt/translucent.png Binary files differ diff --git a/examples/src/main/java/edu/umd/cs/piccolo/pswingexamples/PSwingExample1.java b/examples/src/main/java/edu/umd/cs/piccolo/pswingexamples/PSwingExample1.java deleted file mode 100644 index a02ff07..0000000 --- a/examples/src/main/java/edu/umd/cs/piccolo/pswingexamples/PSwingExample1.java +++ /dev/null @@ -1,176 +0,0 @@ -/* - * Copyright (c) 2008-2009, Piccolo2D project, http://piccolo2d.org - * Copyright (c) 1998-2008, University of Maryland - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without modification, are permitted provided - * that the following conditions are met: - * - * Redistributions of source code must retain the above copyright notice, this list of conditions - * and the following disclaimer. - * - * Redistributions in binary form must reproduce the above copyright notice, this list of conditions - * and the following disclaimer in the documentation and/or other materials provided with the - * distribution. - * - * None of the name of the University of Maryland, the name of the Piccolo2D project, or the names of its - * contributors may be used to endorse or promote products derived from this software without specific - * prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED - * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A - * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR - * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR - * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF - * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ -package edu.umd.cs.piccolo.pswingexamples; - -import java.awt.Color; -import java.awt.Dimension; -import java.awt.Font; -import java.awt.event.ActionEvent; -import java.awt.event.ActionListener; - -import javax.swing.BorderFactory; -import javax.swing.JButton; -import javax.swing.JCheckBox; -import javax.swing.JColorChooser; -import javax.swing.JFrame; -import javax.swing.JPanel; -import javax.swing.JScrollPane; -import javax.swing.JSlider; -import javax.swing.JSpinner; -import javax.swing.JTextArea; -import javax.swing.JTree; -import javax.swing.border.LineBorder; -import javax.swing.event.ChangeEvent; -import javax.swing.event.ChangeListener; - -import edu.umd.cs.piccolo.PNode; -import edu.umd.cs.piccolo.event.PZoomEventHandler; -import edu.umd.cs.piccolo.nodes.PText; -import edu.umd.cs.piccolox.pswing.PComboBox; -import edu.umd.cs.piccolox.pswing.PSwing; -import edu.umd.cs.piccolox.pswing.PSwingCanvas; - -/** - * User: Sam Reid Date: Jul 11, 2005 Time: 12:15:55 PM - */ - -public class PSwingExample1 { - public static void main(String[] args) { - PSwingCanvas pCanvas = new PSwingCanvas(); - final PText pText = new PText("PText"); - pCanvas.getLayer().addChild(pText); - JFrame frame = new JFrame("Test Piccolo"); - - frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); - frame.setContentPane(pCanvas); - frame.setSize(600, 800); - frame.setVisible(true); - - PText text2 = new PText("Text2"); - text2.setFont(new Font("Lucida Sans", Font.BOLD, 18)); - pCanvas.getLayer().addChild(text2); - text2.translate(100, 100); - text2.addInputEventListener(new PZoomEventHandler()); - - pCanvas.removeInputEventListener(pCanvas.getPanEventHandler()); - - JButton jButton = new JButton("MyButton!"); - jButton.addActionListener(new ActionListener() { - public void actionPerformed(ActionEvent e) { - System.out.println("TestZSwing.actionPerformed!!!!!!!!!!!!!!*********************"); - } - }); - final PSwing pSwing = new PSwing(jButton); - pCanvas.getLayer().addChild(pSwing); - pSwing.repaint(); - - JSpinner jSpinner = new JSpinner(); - jSpinner.setPreferredSize(new Dimension(100, jSpinner.getPreferredSize().height)); - PSwing pSpinner = new PSwing(jSpinner); - pCanvas.getLayer().addChild(pSpinner); - pSpinner.translate(0, 150); - - JCheckBox jcb = new JCheckBox("CheckBox", true); - jcb.addActionListener(new ActionListener() { - public void actionPerformed(ActionEvent e) { - System.out.println("TestZSwing.JCheckBox.actionPerformed"); - } - }); - jcb.addChangeListener(new ChangeListener() { - public void stateChanged(ChangeEvent e) { - System.out.println("TestPSwing.JChekbox.stateChanged@" + System.currentTimeMillis()); - } - }); - PSwing pCheckBox = new PSwing(jcb); - pCanvas.getLayer().addChild(pCheckBox); - pCheckBox.translate(100, 0); - - // Growable JTextArea - JTextArea textArea = new JTextArea("This is a growable TextArea.\nTry it out!"); - textArea.setBorder(new LineBorder(Color.blue, 3)); - PSwing swing = new PSwing(textArea); - swing.translate(150, 150); - pCanvas.getLayer().addChild(swing); - - // A Slider - JSlider slider = new JSlider(); - PSwing pSlider = new PSwing(slider); - pSlider.translate(200, 200); - pCanvas.getLayer().addChild(pSlider); - - // A Scrollable JTree - JTree tree = new JTree(); - tree.setEditable(true); - JScrollPane p = new JScrollPane(tree); - p.setPreferredSize(new Dimension(150, 150)); - PSwing pTree = new PSwing(p); - pCanvas.getLayer().addChild(pTree); - pTree.translate(0, 250); - - // A JColorChooser - also demonstrates JTabbedPane - JColorChooser chooser = new JColorChooser(); - PSwing pChooser = new PSwing(chooser); - pCanvas.getLayer().addChild(pChooser); - pChooser.translate(100, 300); - - JPanel myPanel = new JPanel(); - myPanel.setBorder(BorderFactory.createTitledBorder("Titled Border")); - myPanel.add(new JCheckBox("CheckBox")); - PSwing panelSwing = new PSwing(myPanel); - pCanvas.getLayer().addChild(panelSwing); - panelSwing.translate(400, 50); - - // A Slider - JSlider slider2 = new JSlider(); - PSwing pSlider2 = new PSwing(slider2); - pSlider2.translate(200, 200); - PNode root = new PNode(); - root.addChild(pSlider2); - root.scale(1.5); - root.rotate(Math.PI / 4); - root.translate(300, 200); - pCanvas.getLayer().addChild(root); - - // A Combo Box - JPanel comboPanel = new JPanel(); - comboPanel.setBorder( BorderFactory.createTitledBorder( "Combo Box" ) ); - String[] listItems = { "Summer Teeth", "Mermaid Avenue", "Being There", "A.M." }; - PComboBox box = new PComboBox(listItems); - comboPanel.add(box); - swing = new PSwing(comboPanel); - swing.translate( 200, 230 ); - pCanvas.getLayer().addChild(swing); - box.setEnvironment(swing, pCanvas);// has to be done manually at present - - // Revalidate and repaint - pCanvas.revalidate(); - pCanvas.repaint(); - } - -} diff --git a/examples/src/main/java/edu/umd/cs/piccolo/pswingexamples/PSwingExample2.java b/examples/src/main/java/edu/umd/cs/piccolo/pswingexamples/PSwingExample2.java deleted file mode 100644 index 38b5fb3..0000000 --- a/examples/src/main/java/edu/umd/cs/piccolo/pswingexamples/PSwingExample2.java +++ /dev/null @@ -1,468 +0,0 @@ -/* - * Copyright (c) 2008-2009, Piccolo2D project, http://piccolo2d.org - * Copyright (c) 1998-2008, University of Maryland - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without modification, are permitted provided - * that the following conditions are met: - * - * Redistributions of source code must retain the above copyright notice, this list of conditions - * and the following disclaimer. - * - * Redistributions in binary form must reproduce the above copyright notice, this list of conditions - * and the following disclaimer in the documentation and/or other materials provided with the - * distribution. - * - * None of the name of the University of Maryland, the name of the Piccolo2D project, or the names of its - * contributors may be used to endorse or promote products derived from this software without specific - * prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED - * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A - * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR - * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR - * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF - * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ -package edu.umd.cs.piccolo.pswingexamples; - -import java.awt.BorderLayout; -import java.awt.Color; -import java.awt.Cursor; -import java.awt.Dimension; -import java.awt.event.ActionEvent; -import java.io.IOException; -import java.util.Vector; - -import javax.swing.AbstractAction; -import javax.swing.Action; -import javax.swing.JButton; -import javax.swing.JCheckBox; -import javax.swing.JComponent; -import javax.swing.JEditorPane; -import javax.swing.JFrame; -import javax.swing.JInternalFrame; -import javax.swing.JLabel; -import javax.swing.JList; -import javax.swing.JOptionPane; -import javax.swing.JPanel; -import javax.swing.JScrollPane; -import javax.swing.JSlider; -import javax.swing.JSpinner; -import javax.swing.JSplitPane; -import javax.swing.JTabbedPane; -import javax.swing.JTable; -import javax.swing.JTextArea; -import javax.swing.JTextField; -import javax.swing.JToolBar; -import javax.swing.JTree; -import javax.swing.SpinnerNumberModel; -import javax.swing.SwingConstants; -import javax.swing.border.EmptyBorder; -import javax.swing.border.EtchedBorder; -import javax.swing.border.LineBorder; -import javax.swing.border.TitledBorder; -import javax.swing.event.HyperlinkEvent; -import javax.swing.event.HyperlinkListener; -import javax.swing.table.TableColumn; - -import edu.umd.cs.piccolo.PNode; -import edu.umd.cs.piccolox.pswing.PSwing; -import edu.umd.cs.piccolox.pswing.PSwingCanvas; - -/** - * User: Sam Reid Date: Jul 11, 2005 Time: 12:15:55 PM - */ - -public class PSwingExample2 extends JFrame { - public PSwingExample2() { - setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); - ClassLoader loader; - PSwingCanvas canvas; - - // Set up basic frame - setBounds(50, 50, 750, 750); - setResizable(true); - setBackground(null); - setVisible(true); - canvas = new PSwingCanvas(); - canvas.setPanEventHandler(null); - getContentPane().add(canvas); - validate(); - loader = getClass().getClassLoader(); - - ZVisualLeaf leaf; - PNode transform; - PSwing swing; - PSwing swing2; - - // JButton - JButton button = new JButton("Button"); - button.setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR)); - swing = new PSwing(button); - leaf = new ZVisualLeaf(swing); - transform = new PNode(); - transform.translate(-500, -500); - transform.addChild(leaf); - canvas.getLayer().addChild(transform); - - // JButton - JSpinner spinner = new JSpinner(new SpinnerNumberModel(0, 0, 10, 1)); - spinner.setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR)); - swing = new PSwing(spinner); - leaf = new ZVisualLeaf(swing); - transform = new PNode(); - transform.translate(-800, -500); - transform.addChild(leaf); - canvas.getLayer().addChild(transform); - - // 2nd Copy of JButton - leaf = new ZVisualLeaf(swing); - transform = new PNode(); - transform.translate(-450, -450); - transform.rotate(Math.PI / 2); - transform.scale(0.5); - transform.addChild(leaf); - canvas.getLayer().addChild(transform); - - // Growable JTextArea - JTextArea textArea = new JTextArea("This is a growable TextArea.\nTry it out!"); - textArea.setBorder(new LineBorder(Color.blue, 3)); - swing = new PSwing(textArea); - leaf = new ZVisualLeaf(swing); - transform = new PNode(); - transform.translate(-250, -500); - transform.addChild(leaf); - canvas.getLayer().addChild(transform); - - // Growable JTextField - JTextField textField = new JTextField("A growable text field"); - swing = new PSwing(textField); - leaf = new ZVisualLeaf(swing); - transform = new PNode(); - transform.translate(0, -500); - transform.addChild(leaf); - canvas.getLayer().addChild(transform); - - // A Slider - JSlider slider = new JSlider(); - swing = new PSwing(slider); - leaf = new ZVisualLeaf(swing); - transform = new PNode(); - transform.translate(250, -500); - transform.addChild(leaf); - canvas.getLayer().addChild(transform); - - // A Scrollable JTree - JTree tree = new JTree(); - tree.setEditable(true); - JScrollPane p = new JScrollPane(tree); - p.setPreferredSize(new Dimension(150, 150)); - swing = new PSwing(p); - leaf = new ZVisualLeaf(swing); - transform = new PNode(); - transform.translate(-500, -250); - transform.addChild(leaf); - canvas.getLayer().addChild(transform); - - // A Scrollable JTextArea - JScrollPane pane = new JScrollPane(new JTextArea("A Scrollable Text Area\nTry it out!")); - pane.setPreferredSize(new Dimension(150, 150)); - swing = new PSwing(pane); - leaf = new ZVisualLeaf(swing); - transform = new PNode(); - transform.translate(-250, -250); - transform.addChild(leaf); - canvas.getLayer().addChild(transform); - swing2 = swing; - - // A non-scrollable JTextField - // A panel MUST be created with double buffering off - JPanel panel = new JPanel(false); - textField = new JTextField("A fixed-size text field"); - panel.setLayout(new BorderLayout()); - panel.add(textField); - swing = new PSwing(panel); - leaf = new ZVisualLeaf(swing); - transform = new PNode(); - transform.translate(0, -250); - transform.addChild(leaf); - canvas.getLayer().addChild(transform); - - // // A JComboBox - // String[] listItems = {"Summer Teeth", "Mermaid Avenue", - // "Being There", "A.M."}; - // ZComboBox box = new ZComboBox( listItems ); - // swing = new PSwing( canvas, box ); - // leaf = new ZVisualLeaf( swing ); - // transform = new PNode(); - // transform.translate( 0, -150 ); - // transform.addChild( leaf ); - // canvas.getLayer().addChild( transform ); - - // A panel with TitledBorder and JList - panel = new JPanel(false); - panel.setBackground(Color.lightGray); - panel.setLayout(new BorderLayout()); - panel.setBorder(new TitledBorder(new EtchedBorder(EtchedBorder.RAISED), "A JList", TitledBorder.LEFT, - TitledBorder.TOP)); - panel.setPreferredSize(new Dimension(200, 200)); - Vector data = new Vector(); - data.addElement("Choice 1"); - data.addElement("Choice 2"); - data.addElement("Choice 3"); - data.addElement("Choice 4"); - data.addElement("Choice 5"); - JList list = new JList(data); - list.setBackground(Color.lightGray); - panel.add(list); - swing = new PSwing(panel); - leaf = new ZVisualLeaf(swing); - transform = new PNode(); - transform.translate(250, -250); - transform.addChild(leaf); - canvas.getLayer().addChild(transform); - - // A JLabel - JLabel label = new JLabel("A JLabel", SwingConstants.CENTER); - - swing = new PSwing(label); - leaf = new ZVisualLeaf(swing); - transform = new PNode(); - transform.translate(-500, 0); - transform.addChild(leaf); - canvas.getLayer().addChild(transform); - - // Rotated copy of the Scrollable JTextArea - leaf = new ZVisualLeaf(swing2); - transform = new PNode(); - transform.translate(-100, 0); - transform.rotate(Math.PI / 2); - transform.addChild(leaf); - canvas.getLayer().addChild(transform); - - // A panel with layout - // A panel MUST be created with double buffering off - panel = new JPanel(false); - panel.setLayout(new BorderLayout()); - JButton button1 = new JButton("Button 1"); - JButton button2 = new JButton("Button 2"); - label = new JLabel("A Panel with Layout"); - label.setHorizontalAlignment(SwingConstants.CENTER); - label.setForeground(Color.white); - panel.setBackground(Color.red); - panel.setPreferredSize(new Dimension(150, 150)); - panel.setBorder(new EmptyBorder(5, 5, 5, 5)); - panel.add(button1, "North"); - panel.add(button2, "South"); - panel.add(label, "Center"); - panel.revalidate(); - swing = new PSwing(panel); - leaf = new ZVisualLeaf(swing); - transform = new PNode(); - transform.translate(0, 0); - transform.addChild(leaf); - canvas.getLayer().addChild(transform); - - // JTable Example - Vector columns = new Vector(); - columns.addElement("Check Number"); - columns.addElement("Description"); - columns.addElement("Amount"); - Vector rows = new Vector(); - Vector row = new Vector(); - row.addElement("101"); - row.addElement("Sandwich"); - row.addElement("$20.00"); - rows.addElement(row); - row = new Vector(); - row.addElement("102"); - row.addElement("Monkey Wrench"); - row.addElement("$100.00"); - rows.addElement(row); - row = new Vector(); - row.addElement("214"); - row.addElement("Ant farm"); - row.addElement("$55.00"); - rows.addElement(row); - row = new Vector(); - row.addElement("215"); - row.addElement("Self-esteem tapes"); - row.addElement("$37.99"); - rows.addElement(row); - row = new Vector(); - row.addElement("216"); - row.addElement("Tube Socks"); - row.addElement("$7.45"); - rows.addElement(row); - row = new Vector(); - row.addElement("220"); - row.addElement("Ab Excerciser"); - row.addElement("$56.95"); - rows.addElement(row); - row = new Vector(); - row.addElement("319"); - row.addElement("Y2K Supplies"); - row.addElement("$4624.33"); - rows.addElement(row); - row = new Vector(); - row.addElement("332"); - row.addElement("Tie Rack"); - row.addElement("$15.20"); - rows.addElement(row); - row = new Vector(); - row.addElement("344"); - row.addElement("Swing Set"); - row.addElement("$146.59"); - rows.addElement(row); - JTable table = new JTable(rows, columns); - table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF); - table.setRowHeight(30); - TableColumn c = table.getColumn(table.getColumnName(0)); - c.setPreferredWidth(150); - c = table.getColumn(table.getColumnName(1)); - c.setPreferredWidth(150); - c = table.getColumn(table.getColumnName(2)); - c.setPreferredWidth(150); - pane = new JScrollPane(table); - pane.setPreferredSize(new Dimension(200, 200)); - table.setDoubleBuffered(false); - swing = new PSwing(pane); - leaf = new ZVisualLeaf(swing); - transform = new PNode(); - transform.translate(250, 0); - transform.addChild(leaf); - canvas.getLayer().addChild(transform); - - // JEditorPane - HTML example - try { - - final JEditorPane editorPane = new JEditorPane(loader.getResource("csdept.html")); - editorPane.setDoubleBuffered(false); - editorPane.setEditable(false); - pane = new JScrollPane(editorPane); - pane.setDoubleBuffered(false); - pane.setPreferredSize(new Dimension(400, 400)); - editorPane.addHyperlinkListener(new HyperlinkListener() { - public void hyperlinkUpdate(HyperlinkEvent e) { - if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) { - try { - editorPane.setPage(e.getURL()); - } - catch (IOException ioe) { - System.out.println("Couldn't Load Web Page"); - } - } - } - }); - swing = new PSwing(pane); - leaf = new ZVisualLeaf(swing); - transform = new PNode(); - transform.translate(-500, 250); - transform.addChild(leaf); - canvas.getLayer().addChild(transform); - - } - catch (IOException ioe) { - System.out.println("Couldn't Load Web Page"); - } - - // A JInternalFrame with a JSplitPane - a JOptionPane - and a - // JToolBar - JInternalFrame iframe = new JInternalFrame("JInternalFrame"); - iframe.getRootPane().setDoubleBuffered(false); - ((JComponent) iframe.getContentPane()).setDoubleBuffered(false); - iframe.setPreferredSize(new Dimension(500, 500)); - JTabbedPane tabby = new JTabbedPane(); - tabby.setDoubleBuffered(false); - iframe.getContentPane().setLayout(new BorderLayout()); - JOptionPane options = new JOptionPane("This is a JOptionPane!", JOptionPane.INFORMATION_MESSAGE, - JOptionPane.DEFAULT_OPTION); - options.setDoubleBuffered(false); - options.setMinimumSize(new Dimension(50, 50)); - options.setPreferredSize(new Dimension(225, 225)); - JPanel tools = new JPanel(false); - tools.setMinimumSize(new Dimension(150, 150)); - tools.setPreferredSize(new Dimension(225, 225)); - JToolBar bar = new JToolBar(); - Action letter = new AbstractAction("Big A!") { - - public void actionPerformed(ActionEvent e) { - } - }; - - Action hand = new AbstractAction("Hi!") { - public void actionPerformed(ActionEvent e) { - } - }; - Action select = new AbstractAction("There!") { - public void actionPerformed(ActionEvent e) { - } - }; - - label = new JLabel("A Panel with a JToolBar"); - label.setHorizontalAlignment(SwingConstants.CENTER); - bar.add(letter); - bar.add(hand); - bar.add(select); - bar.setFloatable(false); - bar.setBorder(new LineBorder(Color.black, 2)); - tools.setLayout(new BorderLayout()); - tools.add(bar, "North"); - tools.add(label, "Center"); - - JSplitPane split = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, options, tools); - split.setDoubleBuffered(false); - iframe.getContentPane().add(split); - swing = new PSwing(iframe); - leaf = new ZVisualLeaf(swing); - transform = new PNode(); - transform.translate(0, 250); - transform.addChild(leaf); - canvas.getLayer().addChild(transform); - - // JMenuBar menuBar = new JMenuBar(); - // ZMenu menu = new ZMenu( "File" ); - // ZMenu sub = new ZMenu( "Export" ); - // JMenuItem gif = new JMenuItem( "Funds" ); - // sub.add( gif ); - // menu.add( sub ); - // menuBar.add( menu ); - // iframe.setJMenuBar( menuBar ); - - iframe.setVisible(true); - - // A JColorChooser - also demonstrates JTabbedPane - // JColorChooser chooser = new JColorChooser(); - JCheckBox chooser = new JCheckBox("Check Box"); - swing = new PSwing(chooser); - leaf = new ZVisualLeaf(swing); - transform = new PNode(); - transform.translate(-250, 850); - transform.addChild(leaf); - canvas.getLayer().addChild(transform); - - // Revalidate and repaint - canvas.revalidate(); - canvas.repaint(); - - PSwing message = new PSwing(new JTextArea("Click-drag to zoom in and out.")); - message.translate(0, -50); - canvas.getLayer().addChild(message); - - canvas.getCamera().animateViewToCenterBounds(message.getFullBounds(), false, 1200); - } - - public static void main(String[] args) { - new PSwingExample2().setVisible(true); - } - - public static class ZVisualLeaf extends PNode { - public ZVisualLeaf(PNode node) { - addChild(node); - } - } - -} diff --git a/examples/src/main/java/edu/umd/cs/piccolo/swtexamples/SWTBasicExample.java b/examples/src/main/java/edu/umd/cs/piccolo/swtexamples/SWTBasicExample.java deleted file mode 100644 index 9aa8aee..0000000 --- a/examples/src/main/java/edu/umd/cs/piccolo/swtexamples/SWTBasicExample.java +++ /dev/null @@ -1,97 +0,0 @@ -/* - * Copyright (c) 2008-2009, Piccolo2D project, http://piccolo2d.org - * Copyright (c) 1998-2008, University of Maryland - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without modification, are permitted provided - * that the following conditions are met: - * - * Redistributions of source code must retain the above copyright notice, this list of conditions - * and the following disclaimer. - * - * Redistributions in binary form must reproduce the above copyright notice, this list of conditions - * and the following disclaimer in the documentation and/or other materials provided with the - * distribution. - * - * None of the name of the University of Maryland, the name of the Piccolo2D project, or the names of its - * contributors may be used to endorse or promote products derived from this software without specific - * prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED - * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A - * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR - * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR - * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF - * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ -package edu.umd.cs.piccolo.swtexamples; - -import java.awt.Color; - -import org.eclipse.swt.layout.FillLayout; -import org.eclipse.swt.widgets.Display; -import org.eclipse.swt.widgets.Shell; - -import edu.umd.cs.piccolox.swt.PSWTCanvas; -import edu.umd.cs.piccolox.swt.PSWTPath; -import edu.umd.cs.piccolox.swt.PSWTText; - -/** - * @author good - */ -public class SWTBasicExample { - - /** - * Constructor for SWTBasicExample. - */ - public SWTBasicExample() { - super(); - } - - public static void main(String[] args) { - Display display = new Display(); - Shell shell = open(display); - while (!shell.isDisposed()) { - if (!display.readAndDispatch()) - display.sleep(); - } - display.dispose(); - } - - public static Shell open(Display display) { - final Shell shell = new Shell(display); - shell.setLayout(new FillLayout()); - PSWTCanvas canvas = new PSWTCanvas(shell, 0); - - PSWTPath rect = PSWTPath.createRectangle(25, 25, 50, 50); - rect.setPaint(Color.red); - canvas.getLayer().addChild(rect); - - rect = PSWTPath.createRectangle(300, 25, 100, 50); - rect.setPaint(Color.blue); - canvas.getLayer().addChild(rect); - - PSWTPath circle = PSWTPath.createEllipse(100, 200, 50, 50); - circle.setPaint(Color.green); - canvas.getLayer().addChild(circle); - - circle = PSWTPath.createEllipse(400, 400, 75, 150); - circle.setPaint(Color.yellow); - canvas.getLayer().addChild(circle); - - PSWTText text = new PSWTText("Hello World"); - text.translate(350, 150); - text.setPenColor(Color.gray); - canvas.getLayer().addChild(text); - - text = new PSWTText("Goodbye World"); - text.translate(50, 400); - text.setPenColor(Color.magenta); - canvas.getLayer().addChild(text); - - shell.open(); - return shell; - } -} diff --git a/examples/src/main/java/edu/umd/cs/piccolo/swtexamples/SWTBenchTest.java b/examples/src/main/java/edu/umd/cs/piccolo/swtexamples/SWTBenchTest.java deleted file mode 100644 index cbd38a0..0000000 --- a/examples/src/main/java/edu/umd/cs/piccolo/swtexamples/SWTBenchTest.java +++ /dev/null @@ -1,453 +0,0 @@ -/* - * Copyright (c) 2008-2009, Piccolo2D project, http://piccolo2d.org - * Copyright (c) 1998-2008, University of Maryland - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without modification, are permitted provided - * that the following conditions are met: - * - * Redistributions of source code must retain the above copyright notice, this list of conditions - * and the following disclaimer. - * - * Redistributions in binary form must reproduce the above copyright notice, this list of conditions - * and the following disclaimer in the documentation and/or other materials provided with the - * distribution. - * - * None of the name of the University of Maryland, the name of the Piccolo2D project, or the names of its - * contributors may be used to endorse or promote products derived from this software without specific - * prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED - * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A - * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR - * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR - * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF - * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ -package edu.umd.cs.piccolo.swtexamples; - -import java.util.Random; -import java.io.*; -import java.awt.*; -import java.awt.geom.*; - -import org.eclipse.swt.SWT; -import org.eclipse.swt.events.PaintEvent; -import org.eclipse.swt.events.PaintListener; -import org.eclipse.swt.graphics.GC; -import org.eclipse.swt.graphics.Image; -import org.eclipse.swt.graphics.ImageData; -import org.eclipse.swt.graphics.Point; -import org.eclipse.swt.graphics.Rectangle; -import org.eclipse.swt.layout.FillLayout; -import org.eclipse.swt.widgets.Canvas; -import org.eclipse.swt.widgets.Composite; -import org.eclipse.swt.widgets.Display; -import org.eclipse.swt.widgets.Shell; - -import edu.umd.cs.piccolox.swt.SWTGraphics2D; - -/** - * Benchmarking test suite for SWT package - */ -public class SWTBenchTest extends Canvas { - - // Paths - GeneralPath testShape = new GeneralPath(); - - // Images - Image testImageOpaque, testImageBitmask, testImageTranslucent, testImageARGB; - - // Transforms - AffineTransform transform = new AffineTransform(); - static final AffineTransform IDENTITY = new AffineTransform(); - - // Geometry - double pts[] = new double[20]; - - // Colors - static final Color colors[] = { Color.red, Color.green, Color.blue, Color.white, Color.yellow, }; - - // Flags - boolean offscreen; - boolean antialiased; - - // Statistics - int results[][] = new int[NUM_CONTEXTS][NUM_TESTS]; - - // Constants - - static final int CTX_NORMAL = 0; - // static final int CTX_CLIPPED = 1; - static final int CTX_TRANSFORMED = 1; - // static final int CTX_BLENDED = 3; - static final int NUM_CONTEXTS = 2; - - // static String contextNames[] = { - // "normal", - // "clip", - // "transform", - // "alpha", - // }; - - static String contextNames[] = { "normal", "transform" }; - - // - // TEST METHODS - // - - static final int DRAW_LINE = 0; - static final int DRAW_RECT = 1; - static final int FILL_RECT = 2; - static final int DRAW_OVAL = 3; - static final int FILL_OVAL = 4; - static final int DRAW_POLY = 5; - static final int FILL_POLY = 6; - static final int DRAW_TEXT = 7; - static final int DRAW_IMG1 = 8; - static final int DRAW_IMG2 = 9; - static final int DRAW_IMG3 = 10; - static final int DRAW_IMG4 = 11; - static final int DRAW_IMG5 = 12; - static final int NUM_TESTS = 13; - - static String testNames[] = { "line", "rect", "fill rect", "oval", "fill oval", "poly", "fill poly", "text", - "image", "scaled image", "mask image", "alpha image", "argb image", }; - - void testDrawLine(SWTGraphics2D g, Random r) { - g.drawLine(rand(r), rand(r), rand(r), rand(r)); - } - - void testDrawRect(SWTGraphics2D g, Random r) { - g.drawRect(rand(r), rand(r), rand(r), rand(r)); - } - - void testFillRect(SWTGraphics2D g, Random r) { - g.fillRect(rand(r), rand(r), rand(r), rand(r)); - } - - void testDrawOval(SWTGraphics2D g, Random r) { - g.drawOval(rand(r), rand(r), rand(r), rand(r)); - } - - void testFillOval(SWTGraphics2D g, Random r) { - g.fillOval(rand(r), rand(r), rand(r), rand(r)); - } - - void genPoly(Random r) { - for (int i = 0; i < pts.length / 2; i++) { - pts[2 * i] = rand(r); - pts[2 * i + 1] = rand(r); - } - } - - void testDrawPoly(SWTGraphics2D g, Random r) { - genPoly(r); - g.drawPolyline(pts); - } - - void testFillPoly(SWTGraphics2D g, Random r) { - genPoly(r); - g.fillPolygon(pts); - } - - void testDrawText(SWTGraphics2D g, Random r) { - g.drawString("Abcdefghijklmnop", rand(r), rand(r)); - } - - // Basic image - void testDrawImg1(SWTGraphics2D g, Random r) { - g.drawImage(testImageOpaque, rand(r), rand(r)); - } - - // Scaled image - void testDrawImg2(SWTGraphics2D g, Random r) { - Rectangle rect = testImageOpaque.getBounds(); - g.drawImage(testImageOpaque, 0, 0, rect.width, rect.height, rand(r), rand(r), rand(r), rand(r)); - } - - // Bitmask image (unscaled) - void testDrawImg3(SWTGraphics2D g, Random r) { - g.drawImage(testImageBitmask, rand(r), rand(r)); - } - - // Translucent image (unscaled) - void testDrawImg4(SWTGraphics2D g, Random r) { - g.drawImage(testImageTranslucent, rand(r), rand(r)); - } - - // Buffered image (unscaled) - void testDrawImg5(SWTGraphics2D g, Random r) { - g.drawImage(testImageARGB, rand(r), rand(r)); - } - - Image loadImage(Display display, String name) { - try { - InputStream stream = SWTBenchTest.class.getResourceAsStream(name); - if (stream != null) { - ImageData imageData = new ImageData(stream); - return new Image(display, imageData); - // if (imageData != null) { - // ImageData mask = imageData.getTransparencyMask(); - // return new Image(display, imageData, mask); - // } - - } - } - catch (Exception e) { - } - return null; - } - - SWTBenchTest(Composite parent, int style) { - super(parent, style); - - testImageOpaque = loadImage(getDisplay(), "opaque.jpg"); - testImageBitmask = loadImage(getDisplay(), "bitmask.gif"); - testImageTranslucent = loadImage(getDisplay(), "translucent.png"); - testImageARGB = new Image(getDisplay(), 128, 128); - - GC tmpGC = new GC(testImageARGB); - tmpGC.drawImage(testImageTranslucent, 0, 0); - tmpGC.dispose(); - - addPaintListener(new PaintListener() { - public void paintControl(PaintEvent pe) { - runAll(new SWTGraphics2D(pe.gc, getDisplay())); - } - }); - } - - void setupTransform(Graphics2D g, Random r) { - transform.setToIdentity(); - - switch (abs(r.nextInt()) % 5) { - default: - // case 0: // UNIFORM SCALE - double s = r.nextDouble(); - transform.scale(5 * s + 0.1, 5 * s + 0.1); - break; - // case 1: // NON-UNIFORM SCALE - // transform.scale(5 * r.nextDouble() + 0.1, 5 * r.nextDouble() + - // 0.1); - // break; - // case 2: // ROTATION - // transform.rotate(r.nextDouble() * Math.PI * 2); - // break; - // case 3: // TRANSLATION - // transform.translate(r.nextDouble() * 500, r.nextDouble() * 500); - // break; - // case 4: // TRANSLATE + ROTATE + SCALE - // s = r.nextDouble(); - // transform.translate(r.nextDouble() * 500, r.nextDouble() * 500); - // transform.rotate(r.nextDouble() * Math.PI * 2); - // transform.scale(5*s + 0.1, 5*s + 0.1); - // break; - } - - g.setTransform(transform); - } - - void setupClip(Graphics2D g, Random r) { - // g.setClip(rand(r), rand(r), rand(r), rand(r)); - } - - void setupBlend(Graphics2D g, Random r) { - g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, r.nextFloat())); - } - - void setup(int ctx, Graphics2D g, Random r) { - switch (ctx) { - case CTX_NORMAL: - break; - - case CTX_TRANSFORMED: - setupTransform(g, r); - break; - - // case CTX_CLIPPED: - // setupClip(g, r); - // break; - // - // case CTX_BLENDED: - // setupBlend(g, r); - // break; - } - } - - void test(int testNum, SWTGraphics2D g, Random r) { - - g.setColor(colors[abs(r.nextInt()) % colors.length]); - g.setBackground(colors[abs(r.nextInt()) % colors.length]); - - switch (testNum) { - case DRAW_LINE: - testDrawLine(g, r); - break; - case DRAW_RECT: - testDrawRect(g, r); - break; - case FILL_RECT: - testFillRect(g, r); - break; - case DRAW_OVAL: - testDrawOval(g, r); - break; - case FILL_OVAL: - testFillOval(g, r); - break; - case DRAW_POLY: - testDrawPoly(g, r); - break; - case FILL_POLY: - testFillPoly(g, r); - break; - case DRAW_TEXT: - testDrawText(g, r); - break; - case DRAW_IMG1: - testDrawImg1(g, r); - break; - case DRAW_IMG2: - testDrawImg2(g, r); - break; - case DRAW_IMG3: - testDrawImg3(g, r); - break; - case DRAW_IMG4: - testDrawImg4(g, r); - break; - case DRAW_IMG5: - testDrawImg5(g, r); - break; - } - } - - void runTest(SWTGraphics2D g, int ctx, int testNum) { - Random r1 = new Random(1); - Random r2 = new Random(1); - - System.out.println("Test: " + testNames[testNum]); - long t1 = System.currentTimeMillis(); - int i = 0; - while (true) { - if (i % 10 == 0) - setup(ctx, g, r1); - test(testNum, g, r2); - i++; - long t2 = System.currentTimeMillis(); - if (t2 - t1 >= 5000) { - break; - } - } - results[ctx][testNum] += i / 5; - System.out.println("Shapes per second: " + (results[ctx][testNum])); - } - - void runAll(SWTGraphics2D g) { - System.out.println("BENCHMARKING: " + g); - - if (antialiased) { - System.out.println("ANTIALIASED"); - g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); - g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON); - } - - for (int ctx = 0; ctx < NUM_CONTEXTS; ctx++) { - System.out.println("Context: " + contextNames[ctx]); - for (int i = 0; i < NUM_TESTS; i++) { - g.setClip(null); - g.setTransform(IDENTITY); - runTest(g, ctx, i); - } - } - - if (offscreen) { - g.dispose(); - } - - String fileName = g.getClass().getName().replace('.', '_'); - if (offscreen) - fileName += "-offscreen"; - if (antialiased) - fileName += "-antialiased"; - dumpResults(fileName + ".txt"); - System.exit(0); - } - - void dumpResults(String fileName) { - try { - FileOutputStream fout = new FileOutputStream(fileName); - PrintWriter out = new PrintWriter(fout); - out.print('\t'); - for (int i = 0; i < NUM_TESTS; i++) { - out.print(testNames[i]); - out.print('\t'); - } - out.println(""); - for (int ctx = 0; ctx < NUM_CONTEXTS; ctx++) { - out.print(contextNames[ctx]); - for (int i = 0; i < NUM_TESTS; i++) { - out.print('\t'); - out.print(results[ctx][i]); - } - out.println(""); - } - out.close(); - results = new int[NUM_CONTEXTS][NUM_TESTS]; - } - catch (IOException e) { - e.printStackTrace(); - System.exit(1); - } - } - - public Point computeSize(int wHint, int hHint) { - return new Point(512, 512); - } - - public Point computeSize(int wHint, int hHint, boolean changed) { - return computeSize(wHint, hHint); - } - - final static int abs(int x) { - return (x < 0 ? -x : x); - } - - final static double rand(Random r) { - return abs(r.nextInt()) % 500; - } - - public static void main(String args[]) { - // Create frame - Display display = new Display(); - Shell shell = new Shell(display); - shell.setLayout(new FillLayout()); - - // Add bench test - SWTBenchTest m = new SWTBenchTest(shell, SWT.NO_REDRAW_RESIZE | SWT.NO_BACKGROUND); - m.setSize(512, 512); - for (int i = 0; i < args.length; i++) { - if (args[i].intern() == "-offscreen") - m.offscreen = true; - else if (args[i].intern() == "-anti") - m.antialiased = true; - else { - System.out.println("Usage: java BenchTest [-anti] [-offscreen]"); - System.exit(1); - } - } - - shell.pack(); - shell.open(); - - while (!shell.isDisposed()) { - if (!display.readAndDispatch()) - display.sleep(); - } - display.dispose(); - } - -} \ No newline at end of file diff --git a/examples/src/main/java/edu/umd/cs/piccolo/swtexamples/SWTHelloWorld.java b/examples/src/main/java/edu/umd/cs/piccolo/swtexamples/SWTHelloWorld.java deleted file mode 100644 index af5106a..0000000 --- a/examples/src/main/java/edu/umd/cs/piccolo/swtexamples/SWTHelloWorld.java +++ /dev/null @@ -1,71 +0,0 @@ -/* - * Copyright (c) 2008-2009, Piccolo2D project, http://piccolo2d.org - * Copyright (c) 1998-2008, University of Maryland - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without modification, are permitted provided - * that the following conditions are met: - * - * Redistributions of source code must retain the above copyright notice, this list of conditions - * and the following disclaimer. - * - * Redistributions in binary form must reproduce the above copyright notice, this list of conditions - * and the following disclaimer in the documentation and/or other materials provided with the - * distribution. - * - * None of the name of the University of Maryland, the name of the Piccolo2D project, or the names of its - * contributors may be used to endorse or promote products derived from this software without specific - * prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED - * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A - * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR - * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR - * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF - * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ -package edu.umd.cs.piccolo.swtexamples; - -import org.eclipse.swt.layout.FillLayout; -import org.eclipse.swt.widgets.Display; -import org.eclipse.swt.widgets.Shell; - -import edu.umd.cs.piccolox.swt.PSWTCanvas; -import edu.umd.cs.piccolox.swt.PSWTText; - -/** - * @author good - */ -public class SWTHelloWorld { - - /** - * Constructor for SWTBasicExample. - */ - public SWTHelloWorld() { - super(); - } - - public static void main(String[] args) { - Display display = new Display(); - Shell shell = open(display); - while (!shell.isDisposed()) { - if (!display.readAndDispatch()) - display.sleep(); - } - display.dispose(); - } - - public static Shell open(Display display) { - final Shell shell = new Shell(display); - shell.setLayout(new FillLayout()); - PSWTCanvas canvas = new PSWTCanvas(shell, 0); - - PSWTText text = new PSWTText("Hello World"); - canvas.getLayer().addChild(text); - - shell.open(); - return shell; - } -} diff --git a/examples/src/main/java/edu/umd/cs/piccolo/swtexamples/bitmask.gif b/examples/src/main/java/edu/umd/cs/piccolo/swtexamples/bitmask.gif deleted file mode 100644 index 053a5da..0000000 --- a/examples/src/main/java/edu/umd/cs/piccolo/swtexamples/bitmask.gif +++ /dev/null Binary files differ diff --git a/examples/src/main/java/edu/umd/cs/piccolo/swtexamples/opaque.jpg b/examples/src/main/java/edu/umd/cs/piccolo/swtexamples/opaque.jpg deleted file mode 100644 index 2ff5ba6..0000000 --- a/examples/src/main/java/edu/umd/cs/piccolo/swtexamples/opaque.jpg +++ /dev/null Binary files differ diff --git a/examples/src/main/java/edu/umd/cs/piccolo/swtexamples/translucent.png b/examples/src/main/java/edu/umd/cs/piccolo/swtexamples/translucent.png deleted file mode 100644 index 07601a7..0000000 --- a/examples/src/main/java/edu/umd/cs/piccolo/swtexamples/translucent.png +++ /dev/null Binary files differ