您的位置:首页 > 编程语言 > Java开发

Java实现动画逐字打印 推荐

2008-12-02 15:42 363 查看
在现代,摒弃传统的会话方式,很多的图形广告或游戏采用了[逐字打印]的效果来展示文字,当然,这样做的目的无非是为了突出内容或吸引眼球。

但是这种实现方式却是值得借鉴的,以下,我给出一个JAVA实现的简单用例,需要者可以自行扩充。

代码实现:

package org.test;

import java.awt.Color;
import java.awt.Font;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Panel;
import java.awt.image.BufferedImage;

import org.loon.framework.game.helper.ImageHelper;

/** *//**
* <p>
* Title: LoonFramework
* </p>
* <p>
* Description:JAVA实现对话逐字打印(由Loonframework提供)
* </p>
* <p>
* Copyright: Copyright (c) 2007
* </p>
* <p>
* Company: LoonFramework
* </p>
*
* @author chenpeng
* @email:[email]ceponline@yahoo.com.cn[/email]
* @version 0.1
*/

public final class WriterType extends Panel implements Runnable {
// 最大对话数
private int _MAX_COUNT = 10;

//间隔标记
private char _flag='|';

private Image _screen;

private Graphics _bg;

private Thread _L_WriterType;

private static Color _L_fClr;

private static Color _L_bClr;

private static String _M_Text[];

private int _S_COUNT;

private Font _L_TTF;

private int _L_Speed;

private int _L_Delay;

private String _C_Text[];

private static int _L_COUNT;

private static int _F_COUNT;

private static int _T_COUNT;

private static int _N_Text;

private String _FontName;

private final static int _WIDTH = 500;

private final static int _HEIGHT = 375;

private static int _FontStyle;

private static int _FontSize;

private static Image _back = null;

/** *//**
*
*/
private static final long serialVersionUID = 1L;

private boolean _isRun;

/** *//**
* 析构函数,注入背景图,循环显示的文字数组,速度,延迟
* @param img
* @param strContent
* @param speed
* @param delay
*/
public WriterType(Image img,String[] strContent, int speed, int delay) {
init(img,strContent, speed, delay);
}

private WriterType() {

}

public void init(Image img,String[] strContent, int speed, int delay) {
//初始背景色
setBackground(Color.BLACK);
// 原始对话信息
_M_Text = new String[_MAX_COUNT];
// 缓存对话信息
_C_Text = new String[_MAX_COUNT];

_back=img;
_FontName = "宋体";

_FontStyle = 0;

_FontSize = 15;

_L_TTF = new Font(_FontName, _FontStyle, _FontSize);
_L_bClr = Color.BLACK;
_L_fClr = new Color(250, 250, 250);

int length = strContent.length;

// 循环次数
_N_Text = strContent.length - 1;

// 直接copy String数组
System.arraycopy(strContent, 0, _M_Text, 0, length);

//速度及延迟设定
_L_Speed = speed;
_L_Delay = delay;

//缓存区域
_screen = new BufferedImage(_WIDTH, _HEIGHT, 1);
_bg = _screen.getGraphics();

//开启线程
_isRun = true;
_L_WriterType = new Thread(this);
_L_WriterType.start();

}

/** *//**
* 停止线程
*
*/
public void stop() {
_S_COUNT = 0;
_L_COUNT = 0;
_F_COUNT = 0;
if (_L_WriterType != null) {
_isRun = false;
_L_WriterType = null;
}
}

/** *//**
* 绘制图形
*/
public void paint(Graphics g) {
_bg.setColor(_L_bClr);
_bg.fillRect(0, 0, _WIDTH, _HEIGHT);
_bg.setColor(_L_fClr);
_bg.setFont(_L_TTF);
_bg.drawImage(_back, 0, 0, this);
for (int i = 0; i <= _L_COUNT; i++) {
//转换当前字体top位置
Double top = new Double((double) _FontSize
* (3.0D + 2.0D * (double) i));
if (_C_Text[i] != null) {
_bg.drawString(_C_Text[i], 50, top.intValue());
}
}

g.drawImage(_screen, 0, 0, this);
}

/** *//**
* 图形变更
*/
public void update(Graphics g) {
paint(g);
}

/** *//**
* 重新开始
*
*/
private void begin() {
//清除所有缓存数据。
for (int i = 0; i < _C_Text.length; i++) {
_C_Text[i] = null;
}
_F_COUNT = 0;
_S_COUNT = 0;
_L_COUNT = 0;
_T_COUNT++;
if (_T_COUNT > _N_Text) {
_T_COUNT = 0;
}
repaint();
}

public void run() {
while (_isRun) {
try {

//字符串累加
_S_COUNT++;
//当字符串完成整段对话时
if (_S_COUNT == _M_Text[_T_COUNT].length()) {
_C_Text[_L_COUNT] = _M_Text[_T_COUNT].substring(_F_COUNT,
_S_COUNT);
begin();
Thread.sleep(_L_Delay);
repaint();
}
//比对标识设定缓存对话
if (_M_Text[_T_COUNT].charAt(_S_COUNT) != _flag) {
_C_Text[_L_COUNT] = _M_Text[_T_COUNT].substring(_F_COUNT,
_S_COUNT)
+ "_";
} else {
_C_Text[_L_COUNT] = _M_Text[_T_COUNT].substring(_F_COUNT,
_S_COUNT);
}
//用以分辨是否转化对话组时的不同延迟
boolean shortSleep;
//当'|'标识存在时执行
if (_M_Text[_T_COUNT].charAt(_S_COUNT) == _flag) {
_L_COUNT++;
_S_COUNT++;
_F_COUNT = _S_COUNT;
shortSleep = true;
} else {
shortSleep = false;
}
//刷新
repaint();
//判断速度
if (!shortSleep) {
Thread.sleep(_L_Speed);
} else {
Thread.sleep(10 * _L_Speed);
}
} catch (InterruptedException e) {
stop();
}
}
}

public int getMaxCount() {
return _MAX_COUNT;
}

public void setMaxCount(int max_count) {
_MAX_COUNT = max_count;
}

public static void main(String[] args) {
String[] abracadabra = new String[] {
"身为剑体,|钢铁之血灼璃之心.|手创千剑,身经百战;|无一败绩,无一知己.|常一人自醉于剑丘之巅;|此生亦无任何意义;|故此身,定为无限剑成;|故此命,定为无限剑制.",
"I am the bone of my sword;|Steel is my body, and fire is my blood.|I have created over a thousand blades.|Have withstood pain to create many weapons.|Yet,those hands will never hold anything.|So as I pray, unlimited blade works." };
Frame frm = new Frame();
frm.setTitle("Java图形程序中的对话逐字显示(由Loonframework提供)");
frm.setSize(_WIDTH, _HEIGHT + 20);
frm.setResizable(false);
frm.add(new WriterType(ImageHelper
.loadImage("d:/back.jpg", true),abracadabra,50,5000));
frm.setVisible(true);
}

}

本例自动循环String数组中所有数据,每行显示对话由'|'标记分割,背景图如下:



效果图1:



效果图2:



PS:ImageHelper只是一个图片加载用类,完全可以用ImageIcon或者ImageIO替代,原来的博客来有人问,兄弟在这声明一下。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java source game