概述
在现代,摒弃传统的会话方式,很多的图形广告或游戏采用了[逐字打印]的效果来展示文字,当然,这样做的目的无非是为了突出内容或吸引眼球。
但是这种实现方式却是值得借鉴的,以下,我给出一个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>
*
*@authorchenpeng
*@email:ceponline@yahoo.com.cn
*@version0.1
*/
public final class WriterType extends Panel implements Runnable ... {
//最大对话数
privateint_MAX_COUNT=10;
//间隔标记
privatechar_flag='|';
privateImage_screen;
privateGraphics_bg;
privateThread_L_WriterType;
privatestaticColor_L_fClr;
privatestaticColor_L_bClr;
privatestaticString_M_Text[];
privateint_S_COUNT;
privateFont_L_TTF;
privateint_L_Speed;
privateint_L_Delay;
privateString_C_Text[];
privatestaticint_L_COUNT;
privatestaticint_F_COUNT;
privatestaticint_T_COUNT;
privatestaticint_N_Text;
privateString_FontName;
privatefinalstaticint_WIDTH=500;
privatefinalstaticint_HEIGHT=375;
privatestaticint_FontStyle;
privatestaticint_FontSize;
privatestaticImage_back=null;
/***//**
*
*/
privatestaticfinallongserialVersionUID=1L;
privateboolean_isRun;
/***//**
*析构函数,注入背景图,循环显示的文字数组,速度,延迟
*@paramimg
*@paramstrContent
*@paramspeed
*@paramdelay
*/
publicWriterType(Imageimg,String[]strContent,intspeed,intdelay)...{
init(img,strContent,speed,delay);
}
privateWriterType()...{
}
publicvoidinit(Imageimg,String[]strContent,intspeed,intdelay)...{
//初始背景色
setBackground(Color.BLACK);
//原始对话信息
_M_Text=newString[_MAX_COUNT];
//缓存对话信息
_C_Text=newString[_MAX_COUNT];
_back=img;
_FontName="宋体";
_FontStyle=0;
_FontSize=15;
_L_TTF=newFont(_FontName,_FontStyle,_FontSize);
_L_bClr=Color.BLACK;
_L_fClr=newColor(250,250,250);
intlength=strContent.length;
//循环次数
_N_Text=strContent.length-1;
//直接copyString数组
System.arraycopy(strContent,0,_M_Text,0,length);
//速度及延迟设定
_L_Speed=speed;
_L_Delay=delay;
//缓存区域
_screen=newBufferedImage(_WIDTH,_HEIGHT,1);
_bg=_screen.getGraphics();
//开启线程
_isRun=true;
_L_WriterType=newThread(this);
_L_WriterType.start();
}
/***//**
*停止线程
*
*/
publicvoidstop()...{
_S_COUNT=0;
_L_COUNT=0;
_F_COUNT=0;
if(_L_WriterType!=null)...{
_isRun=false;
_L_WriterType=null;
}
}
/***//**
*绘制图形
*/
publicvoidpaint(Graphicsg)...{
_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(inti=0;i<=_L_COUNT;i++)...{
//转换当前字体top位置
Doubletop=newDouble((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);
}
/***//**
*图形变更
*/
publicvoidupdate(Graphicsg)...{
paint(g);
}
/***//**
*重新开始
*
*/
privatevoidbegin()...{
//清除所有缓存数据。
for(inti=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();
}
publicvoidrun()...{
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);
}
//用以分辨是否转化对话组时的不同延迟
booleanshortSleep;
//当'|'标识存在时执行
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(InterruptedExceptione)...{
stop();
}
}
}
publicintgetMaxCount()...{
return_MAX_COUNT;
}
publicvoidsetMaxCount(intmax_count)...{
_MAX_COUNT=max_count;
}
publicstaticvoidmain(String[]args)...{
String[]abracadabra=newString[]...{
"身为剑体,|钢铁之血灼璃之心.|手创千剑,身经百战;|无一败绩,无一知己.|常一人自醉于剑丘之巅;|此生亦无任何意义;|故此身,定为无限剑成;|故此命,定为无限剑制.",
"Iamtheboneofmysword;|Steelismybody,andfireismyblood.|Ihavecreatedoverathousandblades.|Havewithstoodpaintocreatemanyweapons.|Yet,thosehandswillneverholdanything.|SoasIpray,unlimitedbladeworks."};
Framefrm=newFrame();
frm.setTitle("Java图形程序中的对话逐字显示(由Loonframework提供)");
frm.setSize(_WIDTH,_HEIGHT+20);
frm.setResizable(false);
frm.add(newWriterType(ImageHelper
.loadImage("d:/back.jpg",true),abracadabra,50,5000));
frm.setVisible(true);
}
}
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>
*
*@authorchenpeng
*@email:ceponline@yahoo.com.cn
*@version0.1
*/
public final class WriterType extends Panel implements Runnable ... {
//最大对话数
privateint_MAX_COUNT=10;
//间隔标记
privatechar_flag='|';
privateImage_screen;
privateGraphics_bg;
privateThread_L_WriterType;
privatestaticColor_L_fClr;
privatestaticColor_L_bClr;
privatestaticString_M_Text[];
privateint_S_COUNT;
privateFont_L_TTF;
privateint_L_Speed;
privateint_L_Delay;
privateString_C_Text[];
privatestaticint_L_COUNT;
privatestaticint_F_COUNT;
privatestaticint_T_COUNT;
privatestaticint_N_Text;
privateString_FontName;
privatefinalstaticint_WIDTH=500;
privatefinalstaticint_HEIGHT=375;
privatestaticint_FontStyle;
privatestaticint_FontSize;
privatestaticImage_back=null;
/***//**
*
*/
privatestaticfinallongserialVersionUID=1L;
privateboolean_isRun;
/***//**
*析构函数,注入背景图,循环显示的文字数组,速度,延迟
*@paramimg
*@paramstrContent
*@paramspeed
*@paramdelay
*/
publicWriterType(Imageimg,String[]strContent,intspeed,intdelay)...{
init(img,strContent,speed,delay);
}
privateWriterType()...{
}
publicvoidinit(Imageimg,String[]strContent,intspeed,intdelay)...{
//初始背景色
setBackground(Color.BLACK);
//原始对话信息
_M_Text=newString[_MAX_COUNT];
//缓存对话信息
_C_Text=newString[_MAX_COUNT];
_back=img;
_FontName="宋体";
_FontStyle=0;
_FontSize=15;
_L_TTF=newFont(_FontName,_FontStyle,_FontSize);
_L_bClr=Color.BLACK;
_L_fClr=newColor(250,250,250);
intlength=strContent.length;
//循环次数
_N_Text=strContent.length-1;
//直接copyString数组
System.arraycopy(strContent,0,_M_Text,0,length);
//速度及延迟设定
_L_Speed=speed;
_L_Delay=delay;
//缓存区域
_screen=newBufferedImage(_WIDTH,_HEIGHT,1);
_bg=_screen.getGraphics();
//开启线程
_isRun=true;
_L_WriterType=newThread(this);
_L_WriterType.start();
}
/***//**
*停止线程
*
*/
publicvoidstop()...{
_S_COUNT=0;
_L_COUNT=0;
_F_COUNT=0;
if(_L_WriterType!=null)...{
_isRun=false;
_L_WriterType=null;
}
}
/***//**
*绘制图形
*/
publicvoidpaint(Graphicsg)...{
_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(inti=0;i<=_L_COUNT;i++)...{
//转换当前字体top位置
Doubletop=newDouble((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);
}
/***//**
*图形变更
*/
publicvoidupdate(Graphicsg)...{
paint(g);
}
/***//**
*重新开始
*
*/
privatevoidbegin()...{
//清除所有缓存数据。
for(inti=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();
}
publicvoidrun()...{
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);
}
//用以分辨是否转化对话组时的不同延迟
booleanshortSleep;
//当'|'标识存在时执行
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(InterruptedExceptione)...{
stop();
}
}
}
publicintgetMaxCount()...{
return_MAX_COUNT;
}
publicvoidsetMaxCount(intmax_count)...{
_MAX_COUNT=max_count;
}
publicstaticvoidmain(String[]args)...{
String[]abracadabra=newString[]...{
"身为剑体,|钢铁之血灼璃之心.|手创千剑,身经百战;|无一败绩,无一知己.|常一人自醉于剑丘之巅;|此生亦无任何意义;|故此身,定为无限剑成;|故此命,定为无限剑制.",
"Iamtheboneofmysword;|Steelismybody,andfireismyblood.|Ihavecreatedoverathousandblades.|Havewithstoodpaintocreatemanyweapons.|Yet,thosehandswillneverholdanything.|SoasIpray,unlimitedbladeworks."};
Framefrm=newFrame();
frm.setTitle("Java图形程序中的对话逐字显示(由Loonframework提供)");
frm.setSize(_WIDTH,_HEIGHT+20);
frm.setResizable(false);
frm.add(newWriterType(ImageHelper
.loadImage("d:/back.jpg",true),abracadabra,50,5000));
frm.setVisible(true);
}
}
本例自动循环String数组中所有数据,每行显示对话由'|'标记分割,背景图如下:
效果图1如下:
效果图2如下:
最后
以上就是英勇小白菜为你收集整理的Java图形程序中的对话逐字显示实现的全部内容,希望文章能够帮你解决Java图形程序中的对话逐字显示实现所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复