lz 你好
如果你是要用java实现的话 我写了一个比较简单的例子
具体代码如下(有注释):
import java.awt.*;
import java.awt.event.*;
public class Butterfly extends Frame{
private int[] xPoints,yPoints;
private int x1,x2,y1,y2;
private Dimension sSize;
private Image bufferImg;
//构造函数
public Butterfly(){
setTitle("Butterfly");
sSize = Toolkit.getDefaultToolkit().getScreenSize();
setLocation(sSize.width / 4, sSize.height / 4);
setSize(sSize.width / 2, sSize.height / 2);
sSize = getSize();
initialPoints();
setVisible(true);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
}
//初始化polyline的各个点
public void initialPoints(){
xPoints = new int[5];
yPoints = new int[5];
xPoints[0] = sSize.width / 2 - 20;
yPoints[0] = sSize.height / 4;
xPoints[1] = sSize.width / 4;
yPoints[1] = sSize.height * 3 / 4;
xPoints[2] = sSize.width / 2;
yPoints[2] = sSize.height * 3 / 4 - 50;
xPoints[3] = sSize.width * 3 / 4;
yPoints[3] = sSize.height * 3 / 4;
xPoints[4] = sSize.width / 2 + 20;
yPoints[4] = sSize.height / 4;
}
//移动蝴蝶的翅膀(在50范围内移动)
public void fly(){
try {
Thread.sleep(10);
if(xPoints[1] == (sSize.width / 4 - 50)){
x1 = 1;
y1 = 1;
}
else if(xPoints[1] == sSize.width / 4){
x1 = -1;
y1 = -1;
}
if(xPoints[3] == (sSize.width * 3 / 4 + 50)){
x2 = -1;
y2 = 1;
}
else if(xPoints[3] == sSize.width * 3 / 4){
x2 = 1;
y2 = -1;
}
xPoints[1] += x1;
yPoints[1] += y1;
xPoints[3] += x2;
yPoints[3] += y2;
repaint();
}
catch (Exception ex) {
}
}
//实现双缓冲 避免闪烁
public void update(Graphics g){
bufferImg = createImage(sSize.width , sSize.height);
if(bufferImg != null){
Graphics g2 = bufferImg.getGraphics();
paint(g2);
g2.dispose();
g.drawImage(bufferImg, 0, 0, this);
}
else{
paint(g);
}
}
//重写paint函数 绘出蝴蝶的状态
public void paint(Graphics g){
g.setColor(Color.YELLOW);
//绘制翅膀
g.drawPolyline(xPoints, yPoints, 5);
g.setColor(Color.BLACK);
//绘制触角
g.fillArc(sSize.width / 2 - 40, 60, 10, 10, 0, 360);
g.fillArc(sSize.width / 2+ 33, 60, 10, 10, 0, 360);
g.setColor(Color.RED);
//绘制身体和长须
g.drawArc(2, -30, sSize.width / 2 , sSize.height * 5 / 4, -35, 70);
g.drawArc(sSize.width / 2 + 2, -30, sSize.width / 2 , sSize.height * 5 / 4, 145, 70);
}
//主方法
public static void main (String[] args) {
Butterfly bf = new Butterfly();
//使蝴蝶一直翩翩起飞
while(true){
bf.fly();
}
}
}
整个程序的运行效果 就是一只蝴蝶翩翩起舞
希望能帮助你哈