📄 splinecontrolpanel.java
字号:
/**
* Copyright (c) 2006, Sun Microsystems, Inc
* 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.
* * Neither the name of the TimingFramework project nor 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.
*/
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Image;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Point2D;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.IOException;
import java.text.NumberFormat;
import java.util.Locale;
import javax.imageio.ImageIO;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.DefaultListCellRenderer;
import javax.swing.DefaultListModel;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSeparator;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import org.jdesktop.animation.timing.interpolation.KeyFrames;
import org.jdesktop.animation.timing.interpolation.KeyTimes;
import org.jdesktop.animation.timing.interpolation.KeyValues;
import org.jdesktop.animation.timing.interpolation.PropertySetter;
import org.jdesktop.animation.timing.Animator;
import org.jdesktop.animation.timing.Animator.RepeatBehavior;
import org.jdesktop.animation.timing.interpolation.Interpolator;
import org.jdesktop.animation.timing.interpolation.Evaluator;
import org.jdesktop.animation.timing.interpolation.SplineInterpolator;
class SplineControlPanel extends JPanel {
private SplineDisplay display;
private DropSimulator dropSimulator = new DropSimulator();
private BouncerSimulator bounceSimulator = new BouncerSimulator();
private int linesCount = 0;
private JLabel labelControl1;
private JLabel labelControl2;
private Animator controller;
SplineControlPanel() {
super(new BorderLayout());
add(buildEquationDisplay(), BorderLayout.CENTER);
add(buildDebugControls(), BorderLayout.EAST);
}
private Component buildDebugControls() {
JButton button;
JPanel debugPanel = new JPanel(new GridBagLayout());
debugPanel.add(Box.createHorizontalStrut(150),
new GridBagConstraints(0, linesCount++,
2, 1,
1.0, 0.0,
GridBagConstraints.LINE_START,
GridBagConstraints.NONE,
new Insets(0, 0, 0, 0),
0, 0));
// button = addButton(debugPanel, "Create");
// button.addActionListener(new ActionListener() {
// public void actionPerformed(ActionEvent e) {
// JFileChooser chooser = new JFileChooser(".");
// int choice = chooser.showSaveDialog(SplineControlPanel.this);
// if (choice == JFileChooser.CANCEL_OPTION) {
// return;
// }
// File file = chooser.getSelectedFile();
// try {
// OutputStream out = new FileOutputStream(file);
// display.saveAsTemplate(out);
// out.close();
// } catch (FileNotFoundException e1) {
// } catch (IOException e1) {
// }
// }
// });
addSeparator(debugPanel, "Control Points");
labelControl1 = addDebugLabel(debugPanel, "Point 1:", formatPoint(display.getControl1()));
labelControl2 = addDebugLabel(debugPanel, "Point 2:", formatPoint(display.getControl2()));
button = addButton(debugPanel, "Copy Code");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
NumberFormat formatter = getNumberFormatter();
Point2D c1 = display.getControl1();
Point2D c2 = display.getControl2();
StringBuilder code = new StringBuilder();
code.append("Spline spline = new Spline(");
code.append(formatter.format(c1.getX())).append("f, ");
code.append(formatter.format(c1.getY())).append("f, ");
code.append(formatter.format(c2.getX())).append("f, ");
code.append(formatter.format(c2.getY())).append("f);");
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
clipboard.setContents(new StringSelection(code.toString()), null);
}
});
addEmptySpace(debugPanel, 6);
addSeparator(debugPanel, "Animation");
button = addButton(debugPanel, "Play Sample");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
startSampleAnimation();
}
});
addEmptySpace(debugPanel, 6);
addSeparator(debugPanel, "Templates");
debugPanel.add(createTemplates(),
new GridBagConstraints(0, linesCount++,
2, 1,
1.0, 0.0,
GridBagConstraints.CENTER,
GridBagConstraints.NONE,
new Insets(0, 0, 0, 0),
0, 0));
addEmptySpace(debugPanel, 6);
debugPanel.add(Box.createVerticalGlue(),
new GridBagConstraints(0, linesCount++,
2, 1,
1.0, 1.0,
GridBagConstraints.LINE_START,
GridBagConstraints.NONE,
new Insets(0, 0, 0, 0),
0, 0));
JPanel wrapper = new JPanel(new BorderLayout());
wrapper.add(new JSeparator(JSeparator.VERTICAL), BorderLayout.WEST);
wrapper.add(debugPanel);
wrapper.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 6));
return wrapper;
}
private Component createTemplates() {
DefaultListModel model = new DefaultListModel();
model.addElement(createTemplate(0.0, 0.0, 1.0, 1.0));
model.addElement(createTemplate(0.0, 1.0, 0.0, 1.0));
model.addElement(createTemplate(0.0, 1.0, 1.0, 1.0));
model.addElement(createTemplate(0.0, 1.0, 1.0, 0.0));
model.addElement(createTemplate(1.0, 0.0, 0.0, 1.0));
model.addElement(createTemplate(1.0, 0.0, 1.0, 1.0));
model.addElement(createTemplate(1.0, 0.0, 1.0, 0.0));
JList list = new JList(model);
list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
list.setCellRenderer(new TemplateCellRenderer());
list.addListSelectionListener(new TemplateSelectionHandler());
JScrollPane pane = new JScrollPane(list);
pane.getViewport().setPreferredSize(new Dimension(98, 97 * 3));
return pane;
}
private JButton addButton(JPanel debugPanel, String label) {
JButton button;
debugPanel.add(button = new JButton(label),
new GridBagConstraints(0, linesCount++,
2, 1,
1.0, 0.0,
GridBagConstraints.CENTER,
GridBagConstraints.NONE,
new Insets(3, 0, 0, 0),
0, 0));
return button;
}
private String formatPoint(Point2D p) {
NumberFormat formatter = getNumberFormatter();
return "" + formatter.format(p.getX()) + ", " + formatter.format(p.getY());
}
private Component buildEquationDisplay() {
JPanel panel = new JPanel(new BorderLayout());
display = new SplineDisplay();
display.addPropertyChangeListener("control1", new PropertyChangeListener() {
public void propertyChange(PropertyChangeEvent evt) {
labelControl1.setText(formatPoint(display.getControl1()));
}
});
display.addPropertyChangeListener("control2", new PropertyChangeListener() {
public void propertyChange(PropertyChangeEvent evt) {
labelControl2.setText(formatPoint(display.getControl2()));
}
});
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -