Java图形程序中的对话逐字显示实现(一)

2014-11-24 00:45:10 · 作者: · 浏览: 2

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

但是这种实现方式却是值得借鉴的,以下,我给出一个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;

/** *//**
*


* Title: LoonFramework
*


*


* Description:JAVA实现对话逐字打印(由Loonframework提供)
*


*


* Copyright: Copyright (c) 2007
*


*


* Company: LoonFramework
*


*
* @author chenpeng
* @email:ceponline@yahoo.com.cn
* @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位置