动画(Animation)顾名思义就是动态画面,就是利用人眼的滞留特性,通过逐帧变化的画面赋予静止的或无个性的物以运动或个性。使用Eclipse draw2d实现有动画效果的Figure可以让界面看上去更炫,其基本原理并不复杂,就是实现从前一帧向后一帧跳转。基本过程如下:
- 1、设置初始条件,
- 2、启动一个线程在线程中run()方法中刷新界面并改变条件。
- 3、run()方法结束前设置定时器,过一点时间启动另一个线程或再次调用本线程。
其中的关键就是Display.getDefault().timerExec(int milliseconds, Runnable runnable) 方法,它实现了向下一帧跳转。
这里笔者给出一个非常简单的draw2d Animation实例是一个计数器,如下图所示,画面上方是一个LED显示器,下方一个按钮。
是不是这个LED显示器有似曾相识的感觉,没错,这是从logic那个例子抄袭来的。程序代码如下:
import
org.eclipse.draw2d.ActionEvent;
import org.eclipse.draw2d.ActionListener;
import org.eclipse.draw2d.Button;
import org.eclipse.draw2d.ColorConstants;
import org.eclipse.draw2d.Figure;
import org.eclipse.draw2d.FigureCanvas;
import org.eclipse.draw2d.Graphics;
import org.eclipse.draw2d.ToolbarLayout;
import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
/**
* @author danlee, Modified by Farmworker2000
*/
public class LEDFigure extends Figure {
protected static final Dimension SIZE = new Dimension(61, 47);
/**
* Color of the LEDFigure's displayed value
*/
public static final Color DISPLAY_TEXT = new Color(null, 255, 199, 16);
private static final Font DISPLAY_FONT = new Font(null, "", 19, 0); //$NON-NLS-1$
private static Rectangle displayRectangle = new Rectangle(15, 11, 31, 25);
private static Point valuePoint = new Point(16, 10);
private final static Color logicGreen = new Color(null, 123, 174, 148);
private final static Color connectorGreen = new Color(null, 0, 69, 40);
private static final int Y1 = 2;
private static final int Y2 = 44;
private int value = 0;
private static int INTERVAL = 1000;//milliseconds
/**
* Creates a new LEDFigure
*/
public LEDFigure() {
}
/**
* @see org.eclipse.draw2d.Figure#getPreferredSize(int, int)
*/
public Dimension getPreferredSize(int wHint, int hHint) {
return SIZE;
}
/**
* @see org.eclipse.draw2d.Figure#paintFigure(Graphics)
*/
protected void paintFigure(Graphics g) {
Rectangle r = getBounds().getCopy();
g.translate(r.getLocation());
g.setBackgroundColor(logicGreen);
g.setForegroundColor(connectorGreen);
g.fillRectangle(0, 2, r.width, r.height - 4);
int right = r.width - 1;
g.drawLine(0, Y1, right, Y1);
g.drawLine(0, Y1, 0, Y2);
g.setForegroundColor(connectorGreen);
g.drawLine(0, Y2, right, Y2);
g.drawLine(right, Y1, right, Y2);
g.setBackgroundColor(ColorConstants.black);
g.fillRectangle(displayRectangle);
// Draw the value
g.setFont(DISPLAY_FONT);
g.setForegroundColor(DISPLAY_TEXT);
g.drawText(getStringValue(), valuePoint);
}
public void setValue(int val) {
value = val;
repaint();
}
public int getValue() {
return value;
}
private String getStringValue() {
String newValue = String.valueOf(value);
if (value < 10)
newValue = "0" + newValue; //$NON-NLS-1$
return newValue;
}
public void start() {
Display.getDefault().timerExec(INTERVAL, runnable);
}
Runnable runnable = new Runnable() {
public void run() {
int oldValue = getValue();
setValue(oldValue == 60 ? 0 : oldValue + 1);
Display.getDefault().timerExec(INTERVAL, this);
}
};
public static void main(String[] args) {
Display d = new Display();
Shell shell = new Shell(d);
shell.setLayout(new FillLayout());
FigureCanvas canvas = new FigureCanvas(shell);
canvas.setBackground(ColorConstants.white);
Figure diagram = new Figure();
ToolbarLayout layout = new ToolbarLayout();
layout.setVertical(true);
layout.setMinorAlignment(ToolbarLayout.ALIGN_CENTER);
layout.setStretchMinorAxis(false);
layout.setSpacing(10);
diagram.setLayoutManager(layout);
final LEDFigure clock = new LEDFigure();
diagram.add(clock);
Figure buttonContainer = new Figure();
layout = new ToolbarLayout();
layout.setVertical(false);
layout.setMinorAlignment(ToolbarLayout.ALIGN_CENTER);
layout.setStretchMinorAxis(false);
layout.setSpacing(10);
buttonContainer.setLayoutManager(layout);
final Button start = new Button("Start");
start.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
clock.start();
start.setEnabled(false);
}
});
start.setPreferredSize(48, 27);
buttonContainer.add(start);
diagram.add(buttonContainer);
canvas.setContents(diagram);
shell.setText("Test our animation Figure");
shell.setSize(320, 180);
shell.open();
while (!shell.isDisposed())
while (!d.readAndDispatch())
d.sleep();
}
}
import org.eclipse.draw2d.ActionListener;
import org.eclipse.draw2d.Button;
import org.eclipse.draw2d.ColorConstants;
import org.eclipse.draw2d.Figure;
import org.eclipse.draw2d.FigureCanvas;
import org.eclipse.draw2d.Graphics;
import org.eclipse.draw2d.ToolbarLayout;
import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
/**
* @author danlee, Modified by Farmworker2000
*/
public class LEDFigure extends Figure {
protected static final Dimension SIZE = new Dimension(61, 47);
/**
* Color of the LEDFigure's displayed value
*/
public static final Color DISPLAY_TEXT = new Color(null, 255, 199, 16);
private static final Font DISPLAY_FONT = new Font(null, "", 19, 0); //$NON-NLS-1$
private static Rectangle displayRectangle = new Rectangle(15, 11, 31, 25);
private static Point valuePoint = new Point(16, 10);
private final static Color logicGreen = new Color(null, 123, 174, 148);
private final static Color connectorGreen = new Color(null, 0, 69, 40);
private static final int Y1 = 2;
private static final int Y2 = 44;
private int value = 0;
private static int INTERVAL = 1000;//milliseconds
/**
* Creates a new LEDFigure
*/
public LEDFigure() {
}
/**
* @see org.eclipse.draw2d.Figure#getPreferredSize(int, int)
*/
public Dimension getPreferredSize(int wHint, int hHint) {
return SIZE;
}
/**
* @see org.eclipse.draw2d.Figure#paintFigure(Graphics)
*/
protected void paintFigure(Graphics g) {
Rectangle r = getBounds().getCopy();
g.translate(r.getLocation());
g.setBackgroundColor(logicGreen);
g.setForegroundColor(connectorGreen);
g.fillRectangle(0, 2, r.width, r.height - 4);
int right = r.width - 1;
g.drawLine(0, Y1, right, Y1);
g.drawLine(0, Y1, 0, Y2);
g.setForegroundColor(connectorGreen);
g.drawLine(0, Y2, right, Y2);
g.drawLine(right, Y1, right, Y2);
g.setBackgroundColor(ColorConstants.black);
g.fillRectangle(displayRectangle);
// Draw the value
g.setFont(DISPLAY_FONT);
g.setForegroundColor(DISPLAY_TEXT);
g.drawText(getStringValue(), valuePoint);
}
public void setValue(int val) {
value = val;
repaint();
}
public int getValue() {
return value;
}
private String getStringValue() {
String newValue = String.valueOf(value);
if (value < 10)
newValue = "0" + newValue; //$NON-NLS-1$
return newValue;
}
public void start() {
Display.getDefault().timerExec(INTERVAL, runnable);
}
Runnable runnable = new Runnable() {
public void run() {
int oldValue = getValue();
setValue(oldValue == 60 ? 0 : oldValue + 1);
Display.getDefault().timerExec(INTERVAL, this);
}
};
public static void main(String[] args) {
Display d = new Display();
Shell shell = new Shell(d);
shell.setLayout(new FillLayout());
FigureCanvas canvas = new FigureCanvas(shell);
canvas.setBackground(ColorConstants.white);
Figure diagram = new Figure();
ToolbarLayout layout = new ToolbarLayout();
layout.setVertical(true);
layout.setMinorAlignment(ToolbarLayout.ALIGN_CENTER);
layout.setStretchMinorAxis(false);
layout.setSpacing(10);
diagram.setLayoutManager(layout);
final LEDFigure clock = new LEDFigure();
diagram.add(clock);
Figure buttonContainer = new Figure();
layout = new ToolbarLayout();
layout.setVertical(false);
layout.setMinorAlignment(ToolbarLayout.ALIGN_CENTER);
layout.setStretchMinorAxis(false);
layout.setSpacing(10);
buttonContainer.setLayoutManager(layout);
final Button start = new Button("Start");
start.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
clock.start();
start.setEnabled(false);
}
});
start.setPreferredSize(48, 27);
buttonContainer.add(start);
diagram.add(buttonContainer);
canvas.setContents(diagram);
shell.setText("Test our animation Figure");
shell.setSize(320, 180);
shell.open();
while (!shell.isDisposed())
while (!d.readAndDispatch())
d.sleep();
}
}