实现打字机效果的程序

程序

import java.applet.*;
import java.awt.*;
import java.io.*;
import java.net.URL;
import java.util.*;

public class Typewriter4 extends Applet implements Runnable
{

    AudioClip hit, cr, ding ;
    boolean alreadyRun = false,
            soundOn = false,
            loop = false;
    Color bgColor = Color.lightGray,
          textColor = Color.black ;
    Font font ;
    Image offScreen, background ;
    int width, height,
        currentline = 0,
        currentword = 0,
        currentletter = 0,
        depth = 0,
        margin = 0,
        cycles = 0,
        step = 0,
        pause = 0,
        speed = 0,
        update = 0,
        linecount = 0 ;
    long lastread = 0;
    MediaTracker mt ;
    String soundactivation, text, textfile, target ;
    Thread woohoo = null;
    URL hotlink = null;
    Vector lines = null;

    public Typewriter4()
    {

        alreadyRun = false;
        soundOn = false;
        loop = true;
        soundactivation = "enter";

    }

    public void buildLines()
    {

        lines = new Vector();
        FontMetrics fontmetrics =
                offScreen.getGraphics().getFontMetrics();
        StringTokenizer strTok = new StringTokenizer(text, "\n") ;
        while (strTok.hasMoreTokens())
        {

            StringTokenizer strTok1 =
                       new StringTokenizer(strTok.nextToken());
            int wordcount = strTok1.countTokens();
            String[] words = new String[wordcount];
            for(int i = 0; i < wordcount; i++)
                words[i] = strTok1.nextToken();

            String s = "" ;
            for(int j = 0; j < wordcount; j++)
            {
                
                s = s != null ? s + words[j] + " " : words[0];
                if(fontmetrics.stringWidth(s) > width - margin * 2)
                {
                
                    lines.addElement(s.substring(0,
                    s.lastIndexOf(" ", s.lastIndexOf(" ") - 1)));
                    s = words[j] + " ";
                
                }
            
            }

            lines.addElement(s);
            linecount = lines.size();
        
        }

        depth = height - fontmetrics.getHeight() / 2;
    
    }

    public void checkTextfile()
    {
    
        loop = true;
        text = "";
        try
        {
    
            DataInputStream datainputstream = new
                DataInputStream((new URL(getDocumentBase(),
                textfile)).openStream());
            boolean flag = true;
            while(flag)
            {
    
                String s = datainputstream.readLine();
                if(s == null)
                    flag = false;
                else
                    text = text + s + "\n";
    
            }

            datainputstream.close();
            lastread =
            (new Long((new Date()).getTime())).longValue();
            return;
    
        }
        catch(Exception exception)
        {
    
            System.out.println("OOOHH--" + exception.toString());
    
        }
    
    }

    public void init()
    {
        
        mt = new MediaTracker(this);
        lastread = 0L;
        width = getSize().width;
        height = getSize().height;
        offScreen = createImage(width, height);

        String param ;
        if ((param = getParameter("BACKGROUND")) != null)
        {
        
            try
            {
        
                background = getImage(new URL(getDocumentBase(),
                            param));
        
            }
            catch(Exception e) { }
            if(background != null) mt.addImage(background, 0);
        
        }

        if ((param = getParameter("BGCOLOR")) != null)
          bgColor = new Color(Integer.parseInt(param, 16)) ;

        if ((param = getParameter("TEXTCOLOR")) != null)
          textColor = new Color(Integer.parseInt(param, 16)) ;

        String fontName = "Helvetica" ;
        if ((param = getParameter("FONTNAME")) != null)
            fontName = param ;
        int fontSize = 12 ;
        if ((param = getParameter("FONTSIZE")) != null)
            fontSize = Integer.parseInt(param) ;
        int fontStyle = Font.PLAIN ;
        if ((param = getParameter("FONTSTYLE")) != null)
        {
            
            param = param.toUpperCase() ;
            if (param.indexOf("BOLD") != -1)
                fontStyle |= Font.BOLD ;
            if (param.indexOf("ITALIC") != -1)
                fontStyle |= Font.ITALIC ;
        
        }
        font = new Font(fontName, fontStyle, fontSize);

        param = getParameter("CYCLES");
        if(param == null || param.equalsIgnoreCase("infinite"))
        {
            
            cycles = 1;
            step = 0;
        
        }
        else
        {
        
            cycles = Integer.parseInt(param);
            step = 1;
        
        }

        param = getParameter("MARGIN");
        margin = param == null ?
            width / 10 : Integer.parseInt(param);

        param = getParameter("PAUSE");
        pause = param == null ? 2000 : Integer.parseInt(param);

        param = getParameter("SOUNDACTIVATION");
        soundactivation = param == null ?
            "enter" : param.toLowerCase();

        soundOn = soundactivation.equals("auto");

        if((param = getParameter("SOUND.KEYSTROKE")) != null)
            try
            {
                
                hit = getAudioClip(new URL(getDocumentBase(),
                         param));
            
            }
            catch(Exception e) { }

        if((param = getParameter("SOUND.RETURN")) != null)
            try
            {
            
                cr = getAudioClip(new URL(getDocumentBase(),
                         param));
            
            }
            catch(Exception e) { }

        if((param = getParameter("SOUND.BELL")) != null)
            try
            {
            
                ding = getAudioClip(new URL(getDocumentBase(),
                            param));
            
            }
            catch(Exception _ex) { }

        param = getParameter("SPEED");
        speed = param == null ?
            100 : Math.max(10, Integer.parseInt(param));

        param = getParameter("TARGET");
        target = param == null ? "_self" : param;

        if((param = getParameter("URL")) != null)
            try
            {
                
                hotlink = new URL(getDocumentBase(), param);
            
            }
            catch(Exception e) { }

        param = getParameter("TEXT");
        text = param == null ?
            "This is a test... \nthis is a test..." : param;
        text = text.replace('\\', '\n');

        textfile =  getParameter("TEXTFILE") ;

        param = getParameter("UPDATE");
        update = param == null ? 15 : Integer.parseInt(param);

        buildLines();
        try
        {
            
            mt.waitForID(0);
            return;
        
        }
        catch(InterruptedException _ex) { return; }
    
    }

    public void run()
    {
    
        currentline = 0;
        for(int i = 0; i < cycles; i += step)
        {
    
            long l = (new Long((new Date()).getTime())).longValue();
            if(l - lastread > (long)(update * 60000)
                && textfile != null)
            {
    
                checkTextfile();
                buildLines();
    
            }
            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;
    
    }

}

时间: 2024-10-06 11:11:15

实现打字机效果的程序的相关文章

vc++-VC怎样在txt里实现打字机效果

问题描述 VC怎样在txt里实现打字机效果 VC怎样创建一个txt文件,然后自动往里面输入字符,实现类似于人手动输入的效果? 有没有类似的代码?谢谢! 解决方案 是不是想实现文本中有输入字符一个个显示的方式 可以模拟键盘事件等,keybd_event(),往文本中输入键盘 解决方案二: 同问 解决方案三: 就是向文本文件写数据吧,那么怎么叫"自动"呢?就是说什么情况下才向文件中写数据,什么时候结束 程序是不会有思维的,需要定义一定规则,考虑到种种可能的情况,才会有所谓的自动,

JS实现的打字机效果完整实例_javascript技巧

本文实例讲述了JS实现的打字机效果.分享给大家供大家参考,具体如下: <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <!-- Always force latest IE rendering engine (even in intranet) & Chrome Frame Remove this if you use the

Android自定义View实现打字机效果_Android

一.先来看看效果演示 二.实现原理: 这个其实不难实现,通过一个定时器不断调用TextView的setText就行了,在setText的时候播放打字的音效. 具体代码如下: import java.util.Timer; import java.util.TimerTask; import android.content.Context; import android.media.MediaPlayer; import android.text.TextUtils; import android

Android自定义View实现打字机效果

一.先来看看效果演示 二.实现原理: 这个其实不难实现,通过一个定时器不断调用TextView的setText就行了,在setText的时候播放打字的音效. 具体代码如下: import java.util.Timer; import java.util.TimerTask; import android.content.Context; import android.media.MediaPlayer; import android.text.TextUtils; import android

用JavaScript实现网页“打字机”效果

 在网页中经常有人使用swish来做打字效果,其实JavaScript也能做出那样的效果,按如下方法在HTML文件中加入JavaScript代码便可.要使用的文字你可以根据需要修改. 第一步:把如下代码加入<head>区域中   第二步:把如下代码加入<body>区域中   第三步:把<body>改为 <body bgcolor="#fef4d9" OnLoad="textticker()">

javascript中的缓动效果实现程序_javascript技巧

常见的动画有四种类型,介绍一下: linear:线性动画,即匀速 easeIn:速度从小到大,即淡入 easeOut :速度从大到小,即淡出 easeInOut:开始时速度从小到大,结束时速度从大到小,即淡入淡出 其实说到缓动,就不得不提Robert Penner,他发明了N多缓动公式,举个例子 我还是解释一下吧: 设当前变化量为X,则 t / d = X / c,所以X = c * t / d,然后X + b就可以获得当前属性值 再看一个稍复杂的: 这个有淡入效果,也就是说动画开始时,值的变化

jquery导航滑动切换效果实例程序

兼容性不错噢! 菜单切换效果,当然,子栏目还可以再炫一点. 子栏目原来是 像下雨那样消失效果的,好吧,有机会再更新下,因为估计这样炫的导航效果,被使用的网站不会很多吧!? 这个可以直接调用参数说明:  代码如下 复制代码 $(window).p9({     a:$('.P9CCNav'),     b:$('.P9CCMNav'),     c:$('.P9CCMBg') }); 实例代码    代码如下 复制代码 <!DOCTYPE html PUBLIC "-//W3C//DTD X

javascript中的缓动效果实现程序

常见的动画有四种类型,介绍一下:         linear:线性动画,即匀速       easeIn:速度从小到大,即淡入   easeOut :速度从大到小,即淡出 easeInOut:开始时速度从小到大,结束时速度从大到小,即淡入淡出 其实说到缓动,就不得不提Robert Penner,他发明了N多缓动公式,举个例子   我还是解释一下吧: 设当前变化量为X,则 t / d = X / c,所以X = c * t / d,然后X + b就可以获得当前属性值   再看一个稍复杂的:  

javascript实现textarea打字机效果提示

非常不错的提示输入内容,动态的提示,给人亲切感 <textarea id="content" name="content" cols="70" rows="5" style="width:450px;height:50px" class="text" onchange="CHECKCONTENT()" onClick="javascript:flush