文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>java实现打字机效果的程序

java实现打字机效果的程序

时间:2010-05-24  来源:renwfza

 

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

}


 

相关阅读 更多 +
排行榜 更多 +
辰域智控app

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载