⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 movingtext.java

📁 java applet编程,实现对相关图片声音的调用
💻 JAVA
字号:
import java.awt.*;
import java.applet.*;
import java.util.*;


public class MovingText extends Applet implements Runnable
{
	String msg;
	Font fnt;
	Color clr;
	int lastX, lastY, directX, directY; 
	int delay;
    	Thread animatorThread;

    public void init()
    {
        String str;
        int fps = 10;

        //The text to be displayed
        msg = getParameter("text");

        System.out.println(msg);

        //How many milliseconds between frames?
        str = getParameter("fps");
        System.out.println(str);
        try {
            if (str != null)	{     fps = Integer.parseInt(str);            }
        } catch (Exception e) {}
        delay = (fps > 0) ? (1000 / fps) : 100;

        //Create Font and Color
	fnt= new Font("TimeRoman",Font.PLAIN,30);
	clr=new Color(255,0,0);

	//original loacation
	lastX = (int)( Math.random()*(getSize().width-1) );
	lastY = (int)( Math.random()*(getSize().height-1) );
	directX = 3;
	directY = 3;
    }

    public void start()
    {
        //Start animating!
        if (animatorThread == null) {
            animatorThread = new Thread(this);
        }
        animatorThread.start();
    }

    public void stop()
    {
        //Stop the animating thread.
        animatorThread = null;
    }

    public void run()
    {
        //Just to be nice, lower this thread's priority
        //so it can't interfere with other processing going on.
        Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
 
        //Remember the starting time.
        long startTime = System.currentTimeMillis();

        //This is the animation loop.
        while (Thread.currentThread() == animatorThread)
		{
            //Display it.
            repaint();

            //Delay depending on how far we are behind.
            try {
                startTime += delay;
                Thread.sleep(Math.max(0, 
                                      startTime-System.currentTimeMillis()));
            } catch (InterruptedException e) {
                break;
            }
        }
    }

    public void paint(Graphics g)
	{
		int x, y, space;
		StringTokenizer st;
		FontMetrics fntM;

		g.setColor(clr);
		g.setFont(fnt);

		fntM = g.getFontMetrics();
		space = fntM.stringWidth(" ");

		x = lastX;
		y = lastY;

		//分别显示每一个文字串
		try{
			for( st = new StringTokenizer(msg); st.hasMoreTokens();  )
			{
				String word = st.nextToken();
				int w = fntM.stringWidth(word) + space;
				if( x > getSize().width )
				{
					x = x - getSize().width;
				}
				clr = new Color( (int)( Math.random()*256 ), (int)( Math.random()*256 ), (int)( Math.random()*256 ) );
				g.setColor(clr);

				g.drawString(word, x, y);

				x += w;
			}
		}catch(Exception e)
		{
			System.out.println(e);
		}

		if( Math.random() > 0.99 )  directX = 0 - directX;
		lastX += directX;
		if( lastX >= getSize().width )
		{
			lastX = 0;
		}
		else if( lastX < 0 )
		{
			lastX = getSize().width -1;
		} 

		lastY += directY;
		if( lastY >= getSize().height )
		{
			directY = -3;
		}
		else if( lastY < fnt.getSize() )
		{
			directY = 3;
		} 
    }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -