java图形打字机_Java实现打字机效果源代码

for(int j = 0; j < linecount; j++)

{

currentletter = 1;

String s = (String)lines.elementAt(j);

for(int k = 0; k < s.length(); k++)

{

if(soundOn && hit != null)

hit.play();

if(k == s.length() && soundOn && cr != null)

cr.play();

repaint();

int i1 = 75 + (int)(Math.random() * 100D);

try

{

Thread.sleep((i1 * speed) / 100);

}

catch(InterruptedException interruptedexception)

{

System.out.println("BB: "

+ interruptedexception.toString());

}

currentletter++;

}

currentletter = 0;

currentline++;

alreadyRun = false;

}

currentline = currentline % linecount;

try

{

Thread.sleep(pause);

}

catch(InterruptedException interruptedexception)

{

System.out.println("AA: " +

interruptedexception.toString());

}

}

}

public void paintBuffer(Graphics g)

{

if(background != null)

{

g.drawImage(background, 0, 0, this);

}

else

{

g.setColor(bgColor);

g.fillRect(0, 0, width, height);

}

g.setColor(textColor);

g.setFont(font);

FontMetrics fontmetrics = g.getFontMetrics();

for(int i = 0; i < currentline; i++)

g.drawString(

(String)lines.elementAt(currentline - i - 1),

margin, depth - (i + 1) * fontmetrics.getHeight()

);

String s = (String)lines.elementAt(currentline);

String s1 = currentletter >= s.length() ?

s : s.substring(0, currentletter);

if(fontmetrics.stringWidth(s1) > (8 * width) / 10

&& !alreadyRun && soundOn)

{

alreadyRun = true;

if(ding != null) ding.play();

}

g.drawString(s1, margin, depth);

}

public void paint(Graphics g)

{

paintBuffer(offScreen.getGraphics());

g.drawImage(offScreen, 0, 0, this);

}

public void update(Graphics g)

{

paint(g);

}

public void start()

{

if(woohoo == null)

{

woohoo = new Thread(this);

woohoo.start();

}

}

public void stop()

{

woohoo.stop();

woohoo = null;

}

public boolean mouseEnter(Event event, int i, int j)

{

if(hotlink != null) showStatus("Link to " + hotlink.toString());

if(soundactivation.equals("enter")) soundOn = true;

return true;

}

public boolean mouseExit(Event event, int i, int j)

{

if(soundactivation.equals("enter")) soundOn = false;

return true;

}

public boolean mouseDown(Event event, int i, int j)

{

try

{

getAppletContext().showDocument(hotlink, target);

}

catch(Exception e) { }

return true;

}

}0b1331709591d260c1c78e86d0c51c18.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值