一个简单的Draw2d动画实例

来源:互联网 发布:mariadb与mysql 兼容 编辑:程序博客网 时间:2024/05/20 14:28

动画(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(6147);
    
/**
     * Color of the LEDFigure's displayed value
     
*/

    
public static final Color DISPLAY_TEXT = new Color(null25519916);

    
private static final Font DISPLAY_FONT = new Font(null""190); //$NON-NLS-1$
    private static Rectangle displayRectangle = new Rectangle(15113125);
    
private static Point valuePoint = new Point(1610);

    
private final static Color logicGreen = new Color(null123174148);
    
private final static Color connectorGreen = new Color(null06940);

    
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(
02, 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(
4827);
        buttonContainer.add(start);

        diagram.add(buttonContainer);
        canvas.setContents(diagram);
        shell.setText(
"Test our animation Figure");
        shell.setSize(
320180);
        shell.open();
        
while (!shell.isDisposed())
            
while (!d.readAndDispatch())
                d.sleep();

    }

}