diff --git a/examples/src/main/java/edu/umd/cs/piccolo/examples/pswing/PSwingExample3.java b/examples/src/main/java/edu/umd/cs/piccolo/examples/pswing/PSwingExample3.java index 43644cb..74e7f0c 100644 --- a/examples/src/main/java/edu/umd/cs/piccolo/examples/pswing/PSwingExample3.java +++ b/examples/src/main/java/edu/umd/cs/piccolo/examples/pswing/PSwingExample3.java @@ -28,135 +28,208 @@ */ package edu.umd.cs.piccolo.examples.pswing; +import java.awt.Color; import java.awt.Dimension; -import java.awt.Graphics2D; +import java.awt.Graphics; +import javax.swing.ButtonGroup; import javax.swing.JButton; +import javax.swing.JCheckBox; +import javax.swing.JComponent; import javax.swing.JFrame; +import javax.swing.JLabel; import javax.swing.JPanel; +import javax.swing.JRadioButton; +import javax.swing.SwingUtilities; import edu.umd.cs.piccolo.PNode; import edu.umd.cs.piccolo.nodes.PText; -import edu.umd.cs.piccolo.util.PPaintContext; 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 PSwingExample3 extends JFrame { +public class PSwingExample3 extends JFrame { private static final long serialVersionUID = 1L; - private ExampleList exampleList; public PSwingExample3() { - setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); - PSwingCanvas canvas; + setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Set up basic frame setBounds(50, 50, 750, 750); setResizable(true); setBackground(null); setVisible(true); - canvas = new PSwingCanvas(); - canvas.setPanEventHandler(null); + final PSwingCanvas canvas = new PSwingCanvas(); getContentPane().add(canvas); - validate(); - - exampleList = new ExampleList("Button Examples"); - - addButtonExamples(); - - canvas.getLayer().addChild(exampleList); - - canvas.getCamera().animateViewToCenterBounds(canvas.getLayer().getFullBounds(), true, 1200); + validate(); + + ExampleGrid exampleGrid = new ExampleGrid(3); + exampleGrid.addChild(createButtonExamples()); + exampleGrid.addChild(createSimpleComponentExamples()); + canvas.getLayer().addChild(exampleGrid); + SwingUtilities.invokeLater(new Runnable() { + + public void run() { + canvas.getCamera().animateViewToCenterBounds(canvas.getLayer().getFullBounds(), true, 1200); + } + + }); + } - private void addButtonExamples() { - addButtonAloneNoSizing(); - addButtonAlone200x50(); - addButtonOnPanelNoSizing(); - addButtonOnPanel200x50(); - addButtonAlone10x10(); + private ExampleList createSimpleComponentExamples() { + ExampleList exampleList = new ExampleList("Simple Components"); + exampleList.addExample("JLabel", new PSwing(new JLabel("JLabel Example"))); + exampleList.addExample("JCheckBox ", new PSwing(new JCheckBox())); + + JRadioButton radio1 = new JRadioButton("Radio Option 1"); + JRadioButton radio2 = new JRadioButton("Radio Option 1"); + ButtonGroup buttonGroup = new ButtonGroup(); + buttonGroup.add(radio1); + buttonGroup.add(radio2); + exampleList.addExample("RadioButton 1", radio1); + exampleList.addExample("RadioButton 2", radio2); + + JPanel examplePanel = new JPanel() { + + protected void paintComponent(Graphics g) { + super.paintComponent(g); + g.setColor(Color.RED); + g.fillRect(0, 0, getWidth(), getHeight()); + + } + }; + examplePanel.setPreferredSize(new Dimension(50, 50)); + + exampleList.addExample("Custom JPanel ", examplePanel); + return exampleList; } - private void addButtonAloneNoSizing() { + private ExampleList createButtonExamples() { + ExampleList exampleList = new ExampleList("Button Examples"); + addButtonAloneNoSizing(exampleList); + addButtonAlone200x50(exampleList); + addButtonOnPanelNoSizing(exampleList); + addButtonOnPanel200x50(exampleList); + addButtonAlone10x10(exampleList); + return exampleList; + } + + private void addButtonAloneNoSizing(ExampleList exampleList) { JButton button = new JButton("Button"); PSwing pButton = new PSwing(button); exampleList.addExample("Alone - No Sizing", pButton); } - - private void addButtonAlone200x50() { + + private void addButtonAlone200x50(ExampleList exampleList) { JButton button = new JButton("Button"); button.setPreferredSize(new Dimension(200, 50)); PSwing pButton = new PSwing(button); exampleList.addExample("Alone - 200x50", pButton); } - - private void addButtonAlone10x10() { + + private void addButtonAlone10x10(ExampleList exampleList) { JButton button = new JButton("Button"); button.setPreferredSize(new Dimension(10, 10)); PSwing pButton = new PSwing(button); exampleList.addExample("Alone - 10x10", pButton); } - - private void addButtonOnPanelNoSizing() { + + private void addButtonOnPanelNoSizing(ExampleList exampleList) { JButton button = new JButton("Button"); JPanel panel = new JPanel(); - panel.add(button); - PSwing pPanel = new PSwing(panel); - - exampleList.addExample("On JPanel - No Sizing", pPanel); - } - - private void addButtonOnPanel200x50() { - JButton button = new JButton("Button"); - button.setPreferredSize(new Dimension(200, 50)); - - JPanel panel = new JPanel(); panel.add(button); PSwing pPanel = new PSwing(panel); - + + exampleList.addExample("On JPanel - No Sizing", pPanel); + } + + private void addButtonOnPanel200x50(ExampleList exampleList) { + JButton button = new JButton("Button"); + button.setPreferredSize(new Dimension(200, 50)); + + JPanel panel = new JPanel(); + panel.add(button); + PSwing pPanel = new PSwing(panel); + exampleList.addExample("On JPanel - 200x50", pPanel); - } + } public static void main(final String[] args) { new PSwingExample3().setVisible(true); - } - + } + + class ExampleGrid extends PNode { + private int columns; + + public ExampleGrid(int columns) { + this.columns = columns; + } + + public void layoutChildren() { + double[] colWidths = calculateColumnWidths(); + + double currentY = 0; + for (int i = 0; i < getChildrenCount(); i++) { + PNode child = getChild(i); + child.setOffset(colWidths[i % columns] * 1.25, currentY * 1.25); + if (i % columns == 0 && i > 0) { + currentY = getFullBounds().getHeight(); + } + } + } + + private double[] calculateColumnWidths() { + double[] colWidths = new double[columns]; + for (int i = 0; i < getChildrenCount(); i++) { + PNode child = getChild(i); + colWidths[i % columns] = Math.max(colWidths[i % columns], child.getFullBounds().getWidth() + * child.getScale()); + } + return colWidths; + } + } + class ExampleList extends PText { ExampleList(String name) { super(name); setScale(2); } - + public void layoutChildren() { PNode node; double currentY = getHeight(); - for (int i=0; i