/**
* 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.*;
import java.awt.datatransfer.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.beans.*;
import java.io.*;
import java.text.*;
import java.util.*;
import javax.imageio.*;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.image.*;
import org.jdesktop.animation.timing.interpolation.*;
import org.jdesktop.animation.timing.*;
import org.jdesktop.animation.timing.Animator.*;
import org.jdesktop.animation.timing.interpolation.*;
import java.awt.image.*;
import javax.swing.border.*;
import java.net.*;
public class SplineEditor extends JFrame {
public SplineEditor() throws HeadlessException {
super("Spline Editor");
add(buildHeader(), BorderLayout.NORTH);
add(buildControlPanel(), BorderLayout.CENTER);
pack();
setLocationRelativeTo(null);
setResizable(false);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
private Component buildHeader() {
ImageIcon icon = new ImageIcon(getClass().getResource("simulator.png"));
HeaderPanel header = new HeaderPanel(icon,
"Timing Framework Spline Editor",
"Drag control points in the display to change the shape of the spline.",
"Click the Copy Code button to generate the corresponding Java code.");
return header;
}
private Component buildControlPanel() {
return new SplineControlPanel();
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException e) {
} catch (InstantiationException e) {
} catch (IllegalAccessException e) {
} catch (UnsupportedLookAndFeelException e) {
}
new SplineEditor().setVisible(true);
}
});
}
}
/**
* 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.
*/
/**
* 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.
*/
class EquationDisplay extends JComponent implements PropertyChangeListener {
private static final Color COLOR_BACKGROUND = Color.WHITE;
private static final Color COLOR_MAJOR_GRID = Color.GRAY.brighter();
private static final Color COLOR_MINOR_GRID = new Color(220, 220, 220);
private static final Color COLOR_AXIS = Color.BLACK;
private static final float STROKE_AXIS = 1.2f;
private static final float STROKE_GRID = 1.0f;
private static final float COEFF_ZOOM = 1.1f;
private java.util.List equations;
protected double minX;
protected double maxX;
protected double minY;
protected double maxY;
private double originX;
private double originY;
private double majorX;
private int minorX;
private double majorY;
private int minorY;
private boolean drawText = true;
private Point dragStart;
private NumberFormat formatter;
private ZoomHandler zoomHandler;
private PanMotionHandler panMotionHandler;
private PanHandler panHandler;
public EquationDisplay(double originX, double originY,
double minX, double maxX,
double minY, double maxY,
double majorX, int minorX,
double majorY, int minorY) {
if (minX >= maxX) {
throw new IllegalArgumentException("minX must be < to maxX");
}
if (originX < minX || originX > maxX) {
throw new IllegalArgumentException("originX must be between minX and maxX");
}
if (minY >= maxY) {
throw new IllegalArgumentException("minY must be < to maxY");
}
if (originY < minY || originY > maxY) {
throw new IllegalArgumentException("originY must be between minY and maxY");
}
if (minorX <= 0) {
throw new IllegalArgumentException("minorX must be > 0");
}
if (minorY <= 0) {
throw new IllegalArgumentException("minorY must be > 0");
}
if (majorX <= 0.0) {
throw new IllegalArgumentException("majorX must be > 0.0");
}
if (majorY <= 0.0) {
throw new IllegalArgumentException("majorY must be > 0.0");
}
this.originX = originX;
this.originY = originY;
this.minX = minX;
this.maxX = maxX;
this.minY = minY;
this.maxY = maxY;
this.majorX = majorX;
this.minorX = minorX;
this.majorY = majorY;
this.minorY = minorY;
this.equations = new LinkedList();
this.formatter = NumberFormat.getInstance();
this.formatter.setMaximumFractionDigits(2);
panHandler = new PanHandler();
addMouseListener(panHandler);
panMotionHandler = new PanMotionHandler();
addMouseMotionListener(panMotionHandler);
zoomHandler = new ZoomHandler();
addMouseWheelListener(zoomHandler);
}
@Override
public void setEnabled(boolean enabled) {
if (isEnabled() != enabled) {
//super.setEnabled(enabled);
if (enabled) {
addMouseListener(panHandler);
addMouseMotionListener(panMotionHandler);
addMouseWheelListener(zoomHandler);
} else {
removeMouseListener(panHandler);
removeMouseMotionListener(panMotionHandler);
removeMouseWheelListener(zoomHandler);
}
}
}
public boolean isDrawText() {
return drawText;
}
public void setDrawText(boolean drawText) {
this.drawText = drawText;
}
public void addEquation(AbstractEquation equation, Color color) {
if (equation != null && !equations.contains(equation)) {
equation.addPropertyChangeListener(this);
equations.add(new DrawableEquation(equation, color));
repaint();
}
}
public void removeEquation(AbstractEquation equation) {
if (equation != null) {
DrawableEquation toRemove = null;
for (DrawableEquation drawable: equations) {
if (drawable.getEquation() == equation) {
toRemove = drawable;
break;
}
}
if (toRemove != null) {
equation.removePropertyChangeListener(this);
equations.remove(toRemove);
repaint();
}
}
}
@Override
public Dimension getPreferredSize() {
return new Dimension(400, 400);
}
public void propertyChange(PropertyChangeEvent evt) {
repaint();
}
protected double yPositionToPixel(double position) {
double height = (double) getHeight();
return height - ((position - minY) * height / (maxY - minY));
}
protected double xPositionToPixel(double position) {
return (position - minX) * (double) getWidth() / (maxX - minX);
}
protected double xPixelToPosition(double pixel) {
double axisV = xPositionToPixel(originX);
return (pixel - axisV) * (maxX - minX) / (double) getWidth();
}
protected double yPixelToPosition(double pixel) {
double axisH = yPositionToPixel(originY);
return (getHeight() - pixel - axisH) * (maxY - minY) / (double) getHeight();
}
@Override
protected void paintComponent(Graphics g) {
if (!isVisible()) {
return;
}
Graphics2D g2 = (Graphics2D) g;
setupGraphics(g2);
paintBackground(g2);
drawGrid(g2);
drawAxis(g2);
drawEquations(g2);
paintInformation(g2);
}
protected void paintInformation(Graphics2D g2) {
}
private void drawEquations(Graphics2D g2) {
for (DrawableEquation drawable: equations) {
g2.setColor(drawable.getColor());
drawEquation(g2, drawable.getEquation());
}
}
private void drawEquation(Graphics2D g2, AbstractEquation equation) {
float x = 0.0f;
float y = (float) yPositionToPixel(equation.compute(xPixelToPosition(0.0)));
GeneralPath path = new GeneralPath();
path.moveTo(x, y);
for (x = 0.0f; x < getWidth(); x += 1.0f) {
double position = xPixelToPosition(x);
y = (float) yPositionToPixel(equation.compute(position));
path.lineTo(x, y);
}
g2.draw(path);
}
private void drawGrid(Graphics2D g2) {
Stroke stroke = g2.getStroke();
drawVerticalGrid(g2);
drawHorizontalGrid(g2);
if (drawText) {
drawVerticalLabels(g2);
drawHorizontalLabels(g2);
}
g2.setStroke(stroke);
}
private void drawHorizontalLabels(Graphics2D g2) {
double axisV = xPositionToPixel(originX);
g2.setColor(COLOR_AXIS);
for (double y = originY + majorY; y < maxY + majorY; y += majorY) {
int position = (int) yPositionToPixel(y);
g2.drawString(formatter.format(y), (int) axisV + 5, position);
}
for (double y = originY - majorY; y > minY - majorY; y -= majorY) {
int position = (int) yPositionToPixel(y);
g2.drawString(formatter.format(y), (int) axisV + 5, position);
}
}
private void drawHorizontalGrid(Graphics2D g2) {
double minorSpacing = majorY / minorY;
double axisV = xPositionToPixel(originX);
Stroke gridStroke = new BasicStroke(STROKE_GRID);
Stroke axisStroke = new BasicStroke(STROKE_AXIS);
for (double y = originY + majorY; y < maxY + majorY; y += majorY) {
g2.setStroke(gridStroke);
g2.setColor(COLOR_MINOR_GRID);
for (int i = 0; i < minorY; i++) {
int position = (int) yPositionToPixel(y - i * minorSpacing);
g2.drawLine(0, position, getWidth(), position);
}
int position = (int) yPositionToPixel(y);
g2.setColor(COLOR_MAJOR_GRID);
g2.drawLine(0, position, getWidth(), position);
g2.setStroke(axisStroke);
g2.setColor(COLOR_AXIS);
g2.drawLine((int) axisV - 3, position, (int) axisV + 3, position);
}
for (double y = originY - majorY; y > minY - majorY; y -= majorY) {
g2.setStroke(gridStroke);
g2.setColor(COLOR_MINOR_GRID);
for (int i = 0; i < minorY; i++) {
int position = (int) yPositionToPixel(y + i * minorSpacing);
g2.drawLine(0, position, getWidth(), position);
}
int position = (int) yPositionToPixel(y);
g2.setColor(COLOR_MAJOR_GRID);
g2.drawLine(0, position, getWidth(), position);
g2.setStroke(axisStroke);
g2.setColor(COLOR_AXIS);
g2.drawLine((int) axisV - 3, position, (int) axisV + 3, position);
}
}
private void drawVerticalLabels(Graphics2D g2) {
double axisH = yPositionToPixel(originY);
FontMetrics metrics = g2.getFontMetrics();
g2.setColor(COLOR_AXIS);
for (double x = originX + majorX; x < maxX + majorX; x += majorX) {
int position = (int) xPositionToPixel(x);
g2.drawString(formatter.format(x), position, (int) axisH + metrics.getHeight());
}
for (double x = originX - majorX; x > minX - majorX; x -= majorX) {
int position = (int) xPositionToPixel(x);
g2.drawString(formatter.format(x), position, (int) axisH + metrics.getHeight());
}
}
private void drawVerticalGrid(Graphics2D g2) {
double minorSpacing = majorX / minorX;
double axisH = yPositionToPixel(originY);
Stroke gridStroke = new BasicStroke(STROKE_GRID);
Stroke axisStroke = new BasicStroke(STROKE_AXIS);
for (double x = originX + majorX; x < maxX + majorX; x += majorX) {
g2.setStroke(gridStroke);
g2.setColor(COLOR_MINOR_GRID);
for (int i = 0; i < minorX; i++) {
int position = (int) xPositionToPixel(x - i * minorSpacing);
g2.drawLine(position, 0, position, getHeight());
}
int position = (int) xPositionToPixel(x);
g2.setColor(COLOR_MAJOR_GRID);
g2.drawLine(position, 0, position, getHeight());
g2.setStroke(axisStroke);
g2.setColor(COLOR_AXIS);
g2.drawLine(position, (int) axisH - 3, position, (int) axisH + 3);
}
for (double x = originX - majorX; x > minX - majorX; x -= majorX) {
g2.setStroke(gridStroke);
g2.setColor(COLOR_MINOR_GRID);
for (int i = 0; i < minorX; i++) {
int position = (int) xPositionToPixel(x + i * minorSpacing);
g2.drawLine(position, 0, position, getHeight());
}
int position = (int) xPositionToPixel(x);
g2.setColor(COLOR_MAJOR_GRID);
g2.drawLine(position, 0, position, getHeight());
g2.setStroke(axisStroke);
g2.setColor(COLOR_AXIS);
g2.drawLine(position, (int) axisH - 3, position, (int) axisH + 3);
}
}
private void drawAxis(Graphics2D g2) {
double axisH = yPositionToPixel(originY);
double axisV = xPositionToPixel(originX);
g2.setColor(COLOR_AXIS);
Stroke stroke = g2.getStroke();
g2.setStroke(new BasicStroke(STROKE_AXIS));
g2.drawLine(0, (int) axisH, getWidth(), (int) axisH);
g2.drawLine((int) axisV, 0, (int) axisV, getHeight());
FontMetrics metrics = g2.getFontMetrics();
g2.drawString(formatter.format(0.0), (int) axisV + 5, (int) axisH + metrics.getHeight());
g2.setStroke(stroke);
}
protected void setupGraphics(Graphics2D g2) {
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
}
protected void paintBackground(Graphics2D g2) {
g2.setColor(COLOR_BACKGROUND);
g2.fill(g2.getClipBounds());
}
private class DrawableEquation {
private AbstractEquation equation;
private Color color;
DrawableEquation(AbstractEquation equation, Color color) {
this.equation = equation;
this.color = color;
}
AbstractEquation getEquation() {
return equation;
}
Color getColor() {
return color;
}
}
private class ZoomHandler implements MouseWheelListener {
public void mouseWheelMoved(MouseWheelEvent e) {
double distanceX = maxX - minX;
double distanceY = maxY - minY;
double cursorX = minX + distanceX / 2.0;
double cursorY = minY + distanceY / 2.0;
int rotation = e.getWheelRotation();
if (rotation < 0) {
distanceX /= COEFF_ZOOM;
distanceY /= COEFF_ZOOM;
} else {
distanceX *= COEFF_ZOOM;
distanceY *= COEFF_ZOOM;
}
minX = cursorX - distanceX / 2.0;
maxX = cursorX + distanceX / 2.0;
minY = cursorY - distanceY / 2.0;
maxY = cursorY + distanceY / 2.0;
repaint();
}
}
private class PanHandler extends MouseAdapter {
@Override
public void mousePressed(MouseEvent e) {
dragStart = e.getPoint();
}
}
private class PanMotionHandler extends MouseMotionAdapter {
@Override
public void mouseDragged(MouseEvent e) {
Point dragEnd = e.getPoint();
double distance = xPixelToPosition(dragEnd.getX()) -
xPixelToPosition(dragStart.getX());
minX -= distance;
maxX -= distance;
distance = yPixelToPosition(dragEnd.getY()) -
yPixelToPosition(dragStart.getY());
minY -= distance;
maxY -= distance;
repaint();
dragStart = dragEnd;
}
}
}
/**
* 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.
*/
interface Equation {
public double compute(double variable);
}
/**
* 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.
*/
abstract class AbstractEquation implements Equation {
protected java.util.List listeners;
protected AbstractEquation() {
this.listeners = new LinkedList();
}
public void addPropertyChangeListener(PropertyChangeListener listener) {
if (listener != null && !listeners.contains(listener)) {
listeners.add(listener);
}
}
public void removePropertyChangeListener(PropertyChangeListener listener) {
if (listener != null) {
listeners.remove(listener);
}
}
protected void firePropertyChange(String propertyName,
double oldValue,
double newValue) {
PropertyChangeEvent changeEvent = new PropertyChangeEvent(this,
propertyName,
oldValue,
newValue);
for (PropertyChangeListener listener: listeners) {
listener.propertyChange(changeEvent);
}
}
}
/**
* 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.
*/
class AbstractSimulator extends JComponent {
protected double time;
public AbstractSimulator() {
this.time = 0.0f;
}
public void setTime(double time) {
this.time = time;
repaint();
}
}
/**
* 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.
*/
class BouncerSimulator extends AbstractSimulator {
private static final Color COLOR_BACKGROUND = Color.WHITE;
private BufferedImage image;
public BouncerSimulator() {
try {
image = ImageIO.read(BouncerSimulator.class.getResource("item.png"));
} catch (Exception e) { }
}
@Override
protected void paintComponent(Graphics g) {
if (!isVisible()) {
return;
}
Graphics2D g2 = (Graphics2D) g;
setupGraphics(g2);
drawBackground(g2);
drawItem(g2);
}
private void drawItem(Graphics2D g2) {
double position = time;
double xPos = position * getWidth() / 2;
int width = getWidth() * 2 / 3;
int x = (getWidth() - width) / 2;
x += xPos;
int y = getHeight() / 2;
y -= image.getHeight() / 2;
g2.drawImage(image, null, x, y);
}
private void setupGraphics(Graphics2D g2) {
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
}
private void drawBackground(Graphics2D g2) {
g2.setColor(COLOR_BACKGROUND);
g2.fill(g2.getClipBounds());
}
@Override
public Dimension getPreferredSize() {
return new Dimension(150, 100);
}
}
/**
* 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.
*/
class DropSimulator extends AbstractSimulator {
private static final Color COLOR_BACKGROUND = Color.WHITE;
private BufferedImage image;
private BufferedImage shadow;
private float angle = 90;
private int distance = 20;
// cached values for fast painting
private int distance_x = 0;
private int distance_y = 0;
public DropSimulator() {
try {
image = ImageIO.read(BouncerSimulator.class.getResource("icon.png"));
ShadowFactory factory = new ShadowFactory(5, 0.5f, Color.BLACK);
shadow = factory.createShadow(image);
} catch (Exception e) { }
}
@Override
protected void paintComponent(Graphics g) {
if (!isVisible()) {
return;
}
Graphics2D g2 = (Graphics2D) g;
setupGraphics(g2);
drawBackground(g2);
drawItem(g2);
}
private void drawItem(Graphics2D g2) {
double position = time;
int width = (int) (shadow.getWidth() / 2 * (1.0 + position));
int height = (int) (shadow.getHeight() / 2 * (1.0 + position));
int x = (getWidth() - width) / 2;
int y = (getHeight() - height) / 2;
Composite composite = g2.getComposite();
g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,
1.0f - (0.5f * (float) position)));
computeShadowPosition((position * distance) + 1.0);
g2.drawImage(shadow, x + distance_x, y + distance_y, width, height, null);
g2.setComposite(composite);
width = (int) (image.getWidth() / 2 * (1.0 + position));
height = (int) (image.getHeight() / 2 * (1.0 + position));
x = (getWidth() - width) / 2;
y = (getHeight() - height) / 2;
g2.drawImage(image, x, y, width, height, null);
}
private void setupGraphics(Graphics2D g2) {
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BICUBIC);
}
private void drawBackground(Graphics2D g2) {
g2.setColor(COLOR_BACKGROUND);
g2.fill(g2.getClipBounds());
}
@Override
public Dimension getPreferredSize() {
return new Dimension(150, 100);
}
private void computeShadowPosition(double distance) {
double angleRadians = Math.toRadians(angle);
distance_x = (int) (Math.cos(angleRadians) * distance);
distance_y = (int) (Math.sin(angleRadians) * distance);
}
}
/**
* 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.
*/
class HeaderPanel extends JPanel {
private ImageIcon icon;
HeaderPanel(ImageIcon icon,
String title,
String help1,
String help2) {
super(new BorderLayout());
this.icon = icon;
JPanel titlesPanel = new JPanel(new GridLayout(3, 1));
titlesPanel.setOpaque(false);
titlesPanel.setBorder(new EmptyBorder(12, 0, 12, 0));
JLabel headerTitle = new JLabel(title);
Font police = headerTitle.getFont().deriveFont(Font.BOLD);
headerTitle.setFont(police);
headerTitle.setBorder(new EmptyBorder(0, 12, 0, 0));
titlesPanel.add(headerTitle);
JLabel message;
titlesPanel.add(message = new JLabel(help1));
police = headerTitle.getFont().deriveFont(Font.PLAIN);
message.setFont(police);
message.setBorder(new EmptyBorder(0, 24, 0, 0));
titlesPanel.add(message = new JLabel(help2));
police = headerTitle.getFont().deriveFont(Font.PLAIN);
message.setFont(police);
message.setBorder(new EmptyBorder(0, 24, 0, 0));
message = new JLabel(this.icon);
message.setBorder(new EmptyBorder(0, 0, 0, 12));
add(BorderLayout.WEST, titlesPanel);
add(BorderLayout.EAST, message);
add(BorderLayout.SOUTH, new JSeparator(JSeparator.HORIZONTAL));
setPreferredSize(new Dimension(500, this.icon.getIconHeight() + 24));
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
if (!isOpaque()) {
return;
}
Rectangle bounds = g.getClipBounds();
Color control = UIManager.getColor("control");
int width = getWidth();
Graphics2D g2 = (Graphics2D) g;
Paint storedPaint = g2.getPaint();
g2.setPaint(new GradientPaint(this.icon.getIconWidth(), 0, Color.white, width, 0, control));
g2.fillRect(bounds.x, bounds.y, bounds.width, bounds.height);
g2.setPaint(storedPaint);
}
}
/**
* 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.
*/
class Java2dHelper {
public static BufferedImage createCompatibleImage(int width, int height) {
GraphicsEnvironment environment = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice screenDevice = environment.getDefaultScreenDevice();
GraphicsConfiguration configuration = screenDevice.getDefaultConfiguration();
return configuration.createCompatibleImage(width, height);
}
public static BufferedImage loadCompatibleImage(URL resource) throws IOException {
BufferedImage image = ImageIO.read(resource);
BufferedImage compatibleImage = createCompatibleImage(image.getWidth(), image.getHeight());
Graphics g = compatibleImage.getGraphics();
g.drawImage(image, 0, 0, null);
g.dispose();
image = null;
return compatibleImage;
}
public static BufferedImage createThumbnail(BufferedImage image, int requestedThumbSize) {
float ratio = (float) image.getWidth() / (float) image.getHeight();
int width = image.getWidth();
BufferedImage thumb = image;
do {
width /= 2;
if (width < requestedThumbSize) {
width = requestedThumbSize;
}
BufferedImage temp = new BufferedImage(width, (int) (width / ratio), BufferedImage.TYPE_INT_ARGB);
Graphics2D g2 = temp.createGraphics();
g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g2.drawImage(thumb, 0, 0, temp.getWidth(), temp.getHeight(), null);
g2.dispose();
thumb = temp;
} while (width != requestedThumbSize);
return thumb;
}
}
/**
* 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.
*/
/**
*
A shadow factory generates a drop shadow for any given picture, respecting
* the transparency channel if present. The resulting picture contains the
* shadow only and to create a drop shadow effect you will need to stack the
* original picture and the shadow generated by the factory. If you are using
* Swing you can get this done very easily with the layout
* {@link org.jdesktop.swingx.StackLayout}.
*
Shadow Properties
*
A shadow is defined by three properties:
*
*
size: The size, in pixels, of the shadow. This property also
* defines the fuzzyness.
*
opacity: The opacity, between 0.0 and 1.0, of the shadow.
*
color: The color of the shadow. Shadows are not meant to be
* black only.
*
* You can set these properties using the provided mutaters or the appropriate
* constructor. Here are two ways of creating a green shadow of size 10 and
* with an opacity of 50%:
*
* The default constructor provides the following default values:
*
*
size: 5 pixels
*
opacity: 50%
*
color: Black
*
*
Shadow Quality
*
The factory provides two shadow generation algorithms: fast quality blur
* and high quality blur. You can select your preferred algorithm by
* setting the appropriate rendering hint:
*
* The default rendering algorihtm is VALUE_BLUR_QUALITY_FAST.
*
The current implementation should provide the same quality with both
* algorithms but performances are guaranteed to be better (about 30 times
* faster) with the fast quality blur.
*
Generating a Shadow
*
A shadow is generated as a BufferedImage from another
* BufferedImage. Once the factory is set up, you must call
* {@link #createShadow} to actually generate the shadow:
*
* The resulting image is of type BufferedImage.TYPE_INT_ARGB.
* Both dimensions of this image are larger than original image's:
*
*
new width = original width + 2 * shadow size
*
new height = original height + 2 * shadow size
*
* This must be taken into account when you need to create a drop shadow effect.
*
Properties Changes
*
This factory allows to register property change listeners with
* {@link #addPropertyChangeListener}. Listening to properties changes is very
* useful when you emebed the factory in a graphical component and give the API
* user the ability to access the factory. By listening to properties changes,
* you can easily repaint the component when needed.
*
Threading Issues
*
ShadowFactory is not guaranteed to be thread-safe.
*
* @author Romain Guy
* @author Sébastien Petrucci
*/
class ShadowFactory {
/**
*
Key for the blur quality rendering hint.
*/
public static final String KEY_BLUR_QUALITY = "blur_quality";
/**
*
Selects the fast rendering algorithm. This is the default rendering
* hint for KEY_BLUR_QUALITY.
*/
public static final String VALUE_BLUR_QUALITY_FAST = "fast";
/**
*
Selects the high quality rendering algorithm. With current implementation,
* This algorithm does not guarantee a better rendering quality and should
* not be used.
*/
public static final String VALUE_BLUR_QUALITY_HIGH = "high";
/**
*
Identifies a change to the size used to render the shadow.
*
When the property change event is fired, the old value and the new
* value are provided as Integer instances.
*/
public static final String SIZE_CHANGED_PROPERTY = "shadow_size";
/**
*
Identifies a change to the opacity used to render the shadow.
*
When the property change event is fired, the old value and the new
* value are provided as Float instances.
*/
public static final String OPACITY_CHANGED_PROPERTY = "shadow_opacity";
/**
*
Identifies a change to the color used to render the shadow.
*/
public static final String COLOR_CHANGED_PROPERTY = "shadow_color";
// size of the shadow in pixels (defines the fuzziness)
private int size = 5;
// opacity of the shadow
private float opacity = 0.5f;
// color of the shadow
private Color color = Color.BLACK;
// rendering hints map
private HashMap