进入旧版 | 服务项目 | 成功案例 | 联系方式 | 过客留言 | 友情链接
   
设为首页
加入收藏
联系我们
网站首页 | 新闻资讯 | 操作系统 | 办公软件 | 网络软件 | 工具软件 | 媒体动画 | 网页制作 | 网站开发 | 程序开发 | 平面设计
Photoshop视频教程 | Word入门 | Flash入门 | JScript | VBScript | ASP | PHP | ADO | 网页特效 | 3DS MAX6.0命令 | 系统进程
您当前的位置:GOODSGY电脑学习网 -> 网站开发 -> JSP -> 文章内容  
实现打字机效果的程序

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;
    
    }

}

在百度中搜索:实现打字机效果的程序
在Google中搜索:实现打字机效果的程序
在Yahoo中搜索:实现打字机效果的程序

收藏到网摘:新浪VIVI 365key 我摘 POCO网摘 博采中心 YouNote 和讯网摘 天天收藏
[] [返回上一页] [打 印] [收 藏]

 相关文章    最新文章
· ASP.Net实现将Word转换PDF格式
· [图文] 利用“替换”功能来实现Word表格的..
· ASP实现SQL语句日期格式的加减运算
· [组图] 用Word实现填空题下划线填充部分
· 实现PowerPoint演示文档窗口播放(非全屏..
· [图文] 让Word页面能够同时实现横排和纵排..
· [图文] PowerPoint怎样实现不停滚动的字幕..
· 实现Excel动态链接外部vfp数据库
· [图文] 灵活运用 在Word表格实现选择录入
· 在Excel中用VBA实现定时提醒功能
 
· 提升JSP页面响应速度的七大秘籍绝招
· 开发一个调试JSP的Eclipse插件
· JSP报表打印的一种简单解决方案
· JSP/Servlet的重定向技术综述
· java的md5加密类(zt)
· 一个用来访问http服务器的东西。功能类似..
· 菜鸟调试手记一(sql server 中文问题)
· Java性能优化技巧集锦(2)
· 用java压缩文件示例(没有中文问题)
· 使用XML/HTC/DHTML模拟标准Windows菜单

∷相关文章评论∷    (评论内容只代表网友观点,与本站立场无关!) [更多评论…]
站内搜索

精彩图文
  网站导航  
操作系统 办公软件 网络软件
Vista Windows2003 WindowsXP Windows2000/NT Windows9X/ME Linux 其他 Word Excel Powerpoint Outlook 金山系列 其他 网页浏览 上传下载 联络聊天 邮件工具 服务器软件 网络辅助
工具软件 媒体动画 网页制作
系统工具 媒体工具 压缩工具 图文处理 文件管理 其他 3DMAX Authorware Director Maya 视频处理 其他 Flash Dreamweaver FireWorks FrontPage LiveMotion Golive HTML/CSS 其它
网站开发 平面设计 程序设计
ASP JSP PHP CGI JavaScript VBScript XML/SOAP Web服务器 Photoshop PhotoImpact CorelDraw Illustrator Freehand 设计欣赏 其他 VB VC .NET C/C++ DELPHI JAVA

冀ICP备05019428号
Copyright © 2004-2008 电脑学习网 Inc.All rights reserved.
TEL:13832340607
QQ:39873155
E_Mail:goodsgy(#)hotmail.com   (把(#)替换成@)
MSN:goodsgy(#)hotmail.com   (把(#)替换成@)