📄 splinecontrolpanel.java
字号:
panel.add(display, BorderLayout.NORTH);
JPanel wrapper = new JPanel(new GridBagLayout());
wrapper.add(new JSeparator(),
new GridBagConstraints(0, 0,
2, 1,
1.0, 0.0,
GridBagConstraints.LINE_START,
GridBagConstraints.HORIZONTAL,
new Insets(0, 0, 0, 0),
0, 0));
wrapper.add(bounceSimulator,
new GridBagConstraints(0, 1,
1, 1,
1.0, 1.0,
GridBagConstraints.CENTER,
GridBagConstraints.BOTH,
new Insets(0, 0, 0, 0),
0, 0));
wrapper.add(dropSimulator,
new GridBagConstraints(1, 1,
1, 1,
1.0, 1.0,
GridBagConstraints.CENTER,
GridBagConstraints.BOTH,
new Insets(0, 0, 0, 0),
0, 0));
panel.add(wrapper, BorderLayout.CENTER);
return panel;
}
private JLabel addDebugLabel(JPanel panel, String label, String value) {
JLabel labelComponent = new JLabel(label);
panel.add(labelComponent,
new GridBagConstraints(0, linesCount,
1, 1,
0.5, 0.0,
GridBagConstraints.LINE_END,
GridBagConstraints.NONE,
new Insets(0, 6, 0, 0),
0, 0));
labelComponent = new JLabel(value);
panel.add(labelComponent,
new GridBagConstraints(1, linesCount++,
1, 1,
0.5, 0.0,
GridBagConstraints.LINE_START,
GridBagConstraints.NONE,
new Insets(0, 6, 0, 0),
0, 0));
return labelComponent;
}
private void addEmptySpace(JPanel panel, int size) {
panel.add(Box.createVerticalStrut(size),
new GridBagConstraints(0, linesCount++,
2, 1,
1.0, 0.0,
GridBagConstraints.CENTER,
GridBagConstraints.VERTICAL,
new Insets(6, 0, 0, 0),
0, 0));
}
private void addSeparator(JPanel panel, String label) {
JPanel innerPanel = new JPanel(new GridBagLayout());
innerPanel.add(new JLabel(label),
new GridBagConstraints(0, 0,
1, 1,
0.0, 0.0,
GridBagConstraints.LINE_START,
GridBagConstraints.NONE,
new Insets(0, 0, 0, 0),
0, 0));
innerPanel.add(new JSeparator(),
new GridBagConstraints(1, 0,
1, 1,
0.9, 0.0,
GridBagConstraints.LINE_START,
GridBagConstraints.HORIZONTAL,
new Insets(0, 6, 0, 6),
0, 0));
panel.add(innerPanel,
new GridBagConstraints(0, linesCount++,
2, 1,
1.0, 0.0,
GridBagConstraints.LINE_START,
GridBagConstraints.HORIZONTAL,
new Insets(6, 6, 6, 0),
0, 0));
}
private void startSampleAnimation() {
if (controller != null && controller.isRunning()) {
controller.stop();
}
Point2D control1 = display.getControl1();
Point2D control2 = display.getControl2();
Interpolator splines = new SplineInterpolator((float) control1.getX(),
(float) control1.getY(),
(float) control2.getX(), (float) control2.getY());
KeyTimes times = new KeyTimes(0.0f, 1.0f);
KeyValues values = KeyValues.create(0.0, 1.0);
KeyFrames frames = new KeyFrames(values,times, splines);
PropertySetter dropModifier = new PropertySetter(dropSimulator,
"time", frames);
PropertySetter bounceModifier = new PropertySetter(bounceSimulator,
"time", frames);
controller = new Animator(1000, 4, RepeatBehavior.REVERSE, dropModifier);
controller.setResolution(10);
controller.addTarget(bounceModifier);
controller.start();
}
private Evaluator point2dInterpolator = new Point2DNonLinearInterpolator();
private class Point2DNonLinearInterpolator extends Evaluator<Point2D> {
private Point2D value;
public Point2D evaluate(Point2D v0, Point2D v1,
float fraction) {
Point2D value = (Point2D)v0.clone();
if (v0 != v1) {
double x = value.getX();
x += (v1.getX() - v0.getX()) * fraction;
double y = value.getY();
y += (v1.getY() - v0.getY()) * fraction;
value.setLocation(x, y);
} else {
value.setLocation(v0.getX(), v0.getY());
}
return value;
}
}
private class TemplateSelectionHandler implements ListSelectionListener {
public void valueChanged(ListSelectionEvent e) {
if (e.getValueIsAdjusting()) {
return;
}
JList list = (JList) e.getSource();
Template template = (Template) list.getSelectedValue();
if (template != null) {
if (controller != null && controller.isRunning()) {
controller.stop();
}
controller = new Animator(300,
new PropertySetter(display, "control1",
point2dInterpolator, display.getControl1(),
template.getControl1()));
controller.setResolution(10);
controller.addTarget(new PropertySetter(display, "control2",
point2dInterpolator, display.getControl2(),
template.getControl2()));
controller.start();
}
}
}
private static NumberFormat getNumberFormatter() {
NumberFormat formatter = NumberFormat.getInstance(Locale.ENGLISH);
formatter.setMinimumFractionDigits(2);
formatter.setMaximumFractionDigits(2);
return formatter;
}
private static Template createTemplate(double x1, double y1, double x2, double y2) {
return new Template(new Point2D.Double(x1, y1),
new Point2D.Double(x2, y2));
}
private static class TemplateCellRenderer extends DefaultListCellRenderer {
private boolean isSelected;
@Override
public Component getListCellRendererComponent(JList list, Object value, int index,
boolean isSelected, boolean cellHasFocus) {
Template template = (Template) value;
this.setBackground(Color.WHITE);
this.setIcon(new ImageIcon(template.getImage()));
this.isSelected = isSelected;
return this;
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (isSelected) {
g.setColor(new Color(0.0f, 0.0f, 0.7f, 0.1f));
g.fillRect(0, 0, getWidth(), getHeight());
}
}
}
private static class Template {
private Point2D control1;
private Point2D control2;
private Image image;
public Template(Point2D control1, Point2D control2) {
this.control1 = control1;
this.control2 = control2;
}
public Point2D getControl1() {
return control1;
}
public Point2D getControl2() {
return control2;
}
public Image getImage() {
if (image == null) {
NumberFormat formatter = getNumberFormatter();
String name = "";
name += formatter.format(control1.getX()) + '-' + formatter.format(control1.getY());
name += '-';
name += formatter.format(control2.getX()) + '-' + formatter.format(control2.getY());
try {
image = ImageIO.read(getClass().getResourceAsStream("images/templates/" + name + ".png"));
} catch (IOException e) {
}
}
return image;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -