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

📄 vcolorscroll.java

📁 Java 范例实战 光盘使用说明 ========================== 本光盘的文件结构如下所示: =====================================
💻 JAVA
字号:
import java.applet.Applet;
import java.awt.*;
import java.io.PrintStream;

public class VColorScroll extends Applet implements Runnable
{

    final static int MAX_TEXT_NUM = 100,
                     SPEED = 50 ;
    int realTextNum = 0;
    int width, height, offset, wait, ht, startDelay, finalDelay;
    int[] textDelay ;
    Image total, offScreen ;
    Font displayFont ;
    Thread myThread ;
    String[] textString ;
    Color[] textColor = null;

    public VColorScroll() { }

    int getParameter(String s1, int s2) 
    {
    
        String s = getParameter(s1) ;
        return (s != null) ? Integer.parseInt(s) : s2 ;
    
    }

    String getParameter(String s1, String s2) 
    {
    
        String s = getParameter(s1) ;
        return (s != null) ? s : s2 ;
    
    }

    int countTextNum() 
    {
    
        int j = 0 ;
        for (int i = 0 ; i < MAX_TEXT_NUM ; ++i)
            if (getParameter("Text" + i) != null) ++j ;
            else break ;
        return j ;
    
    }

    public void init()
    {
        
        width = getSize().width;
        height = getSize().height;
        offset = -height;
        startDelay = 1;
        finalDelay = 1;
        wait = startDelay;
        displayFont = new Font("TimesRoman", Font.PLAIN, 
        		getParameter("FontSize", 20));
        realTextNum = countTextNum() ;
        textString = new String[realTextNum];
        textDelay = new int[realTextNum];
        textColor = new Color[realTextNum];

        for(int i = 0; i < realTextNum; i++)
        {
            
            textString[i] = getParameter("Text" + i);
            textDelay[i] = getParameter("Delay", 10) ;
            textColor[i] = new Color(
            			Integer.parseInt(
            			getParameter("Color", "FF0000"), 16)
            		   ) ;
            
        }
        ht = height * realTextNum ;
        total = createImage(width, ht) ;
        offScreen = createImage(width, height) ;
    
    }

    public void start()
    {
    
        if(myThread == null)
        {
    
            myThread = new Thread(this);
            myThread.start();
    
        }
    
    }

    public void stop()
    {
    
        myThread = null ;
    
    }

    public void drawText(Graphics g)
    {
    
        g.setFont(displayFont);
        FontMetrics fontMet = g.getFontMetrics();
        g.setColor(Color.yellow);
        g.fillRect(0, 0, width, ht);
        int y = -2 - fontMet.getMaxDescent();
        for(int i = 0; i < realTextNum; i++)
            if(textString[i] != null)
            {
    
                int x = width / 2 - 
                	fontMet.stringWidth(textString[i]) / 2;
                g.setColor(textColor[i]);
                g.drawString(textString[i], x, (i+1)*height + y);
    
            }
    
    }

    public void run()
    {
    
        drawText(total.getGraphics());
        while(myThread == Thread.currentThread())
        {
    
            if(wait <= 0)
            {
    
                offset++;
                repaint();
    
            }
            try
            {
    
                Thread.sleep(SPEED);
    
            }
            catch(InterruptedException _ex) { }

	    if (wait <= 0) 
	    {
	    
	        if(offset == ht)
                {
    
                    offset = -height;
                    wait = finalDelay;
    
                }
                else if(offset % height == 0)
                    wait = textDelay[offset / height];

	    	
	    }
    
        }
    
    }

    public void paint(Graphics g)
    {
    
        offScreen.getGraphics().setColor(Color.blue);
        offScreen.getGraphics().fillRect(0, 0, width, height);
        offScreen.getGraphics().drawImage(total, 0, -offset, this);
        g.drawImage(offScreen, 0, 0, this);
    
    }

    public void update(Graphics g)
    {
    
        paint(g);
    
    }

}

⌨️ 快捷键说明

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