动画(Animation)顾名思义就是动态画面,就是利用人眼的滞留特性,通过逐帧变化的画面赋予静止的或无个性的物以运动或个性。使用Eclipse draw2d实现有动画效果的Figure可以让界面看上去更炫,其基本原理并不复杂,就是实现从前一帧向后一帧跳转。基本过程如下:
- 1、设置初始条件,
- 2、启动一个线程在线程中run()方法中刷新界面并改变条件。
- 3、run()方法结束前设置定时器,过一点时间启动另一个线程或再次调用本线程。
其中的关键就是Display.getDefault().timerExec(int milliseconds, Runnable runnable) 方法,它实现了向下一帧跳转。
这里笔者给出一个非常简单的draw2d Animation实例是一个计数器,如下图所示,画面上方是一个LED显示器,下方一个按钮。
![](https://p-blog.csdn.net/images/p_blog_csdn_net/farmworker2000/screen.JPG)
是不是这个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;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
/** */
/**
* @author danlee, Modified by Farmworker2000
*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
public
class
LEDFigure
extends
Figure
...
{
protected static final Dimension SIZE = new Dimension(61, 47);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
/** *//**
* Color of the LEDFigure's displayed value
*/
public static final Color DISPLAY_TEXT = new Color(null, 255, 199, 16);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
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);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private final static Color logicGreen = new Color(null, 123, 174, 148);
private final static Color connectorGreen = new Color(null, 0, 69, 40);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private static final int Y1 = 2;
private static final int Y2 = 44;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private int value = 0;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private static int INTERVAL = 1000;//milliseconds
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
/** *//**
* Creates a new LEDFigure
*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
public LEDFigure() ...{
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
/** *//**
* @see org.eclipse.draw2d.Figure#getPreferredSize(int, int)
*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
public Dimension getPreferredSize(int wHint, int hHint) ...{
return SIZE;
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
/** *//**
* @see org.eclipse.draw2d.Figure#paintFigure(Graphics)
*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
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);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
g.setForegroundColor(connectorGreen);
g.drawLine(0, Y2, right, Y2);
g.drawLine(right, Y1, right, Y2);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
g.setBackgroundColor(ColorConstants.black);
g.fillRectangle(displayRectangle);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// Draw the value
g.setFont(DISPLAY_FONT);
g.setForegroundColor(DISPLAY_TEXT);
g.drawText(getStringValue(), valuePoint);
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
public void setValue(int val) ...{
value = val;
repaint();
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
public int getValue() ...{
return value;
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
private String getStringValue() ...{
String newValue = String.valueOf(value);
if (value < 10)
newValue = "0" + newValue; //$NON-NLS-1$
return newValue;
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
public void start() ...{
Display.getDefault().timerExec(INTERVAL, runnable);
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
Runnable runnable = new Runnable() ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
public void run() ...{
int oldValue = getValue();
setValue(oldValue == 60 ? 0 : oldValue + 1);
Display.getDefault().timerExec(INTERVAL, this);
}
};
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
public static void main(String[] args) ...{
Display d = new Display();
Shell shell = new Shell(d);
shell.setLayout(new FillLayout());
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
FigureCanvas canvas = new FigureCanvas(shell);
canvas.setBackground(ColorConstants.white);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Figure diagram = new Figure();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
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();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
layout = new ToolbarLayout();
layout.setVertical(false);
layout.setMinorAlignment(ToolbarLayout.ALIGN_CENTER);
layout.setStretchMinorAxis(false);
layout.setSpacing(10);
buttonContainer.setLayoutManager(layout);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
final Button start = new Button("Start");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
start.addActionListener(new ActionListener() ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
public void actionPerformed(ActionEvent arg0) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
clock.start();
start.setEnabled(false);
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
});
start.setPreferredSize(48, 27);
buttonContainer.add(start);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
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();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
}
}