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

📄 spotlight.java

📁 java applet 非常全的实例。比较利于初学者进行研究。
💻 JAVA
字号:
import java.applet.*;
import java.awt.*;
import java.awt.image.*;
import java.net.*;

public class SpotLight extends Applet implements Runnable 
{
    private String Text;
    private Image offI;


    private int pX, pY, width, height, fadeWidth;
    private Thread thread=null;
    
    private int radius=25, shiftFrom=0, shiftTo=0x000000, 
                delay=0, step=8, hpadding=30, vpadding=0;
    private int FontSize=30;
    private boolean isVacuum=false;
    
    

    public void init() 
    {
      start2();
    }

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

    public void stop() 
    {
        if (thread!=null) 
        {
            thread.stop();
            doStop();
            thread = null;
        }
    }

    public void run() 
    {
        while (true) 
        {
            Efficinet();
            try 
            {
                thread.sleep(delay);
            }
            catch(InterruptedException e) {}
        }
    }
    
    public void doStop() 
    {
        System.gc();
    }

    public void start2() {
        String s = new String("");

        if ( (s=getParameter("Text")) != null )
            Text=s;
        else
            Text="Text=null";

        setFont(new Font("Helvetica",Font.BOLD,FontSize));

        if ( (s=getParameter("textcolor")) != null )
            setForeground(new Color(Integer.parseInt(s,16)));
        else
            setForeground(new Color(0xe99b00));

        if ( (s=getParameter("bgcolor")) != null )
            setBackground(new Color(Integer.parseInt(s,16)));
        else
            setBackground(Color.black);

        if ( (s=getParameter("shiftFrom")) != null )
            shiftFrom=Integer.parseInt(s,16);
        if ( (s=getParameter("shiftTo")) != null )
            shiftTo=Integer.parseInt(s,16);

        if ( (s=getParameter("radius")) != null )
            radius=Integer.parseInt(s);
        fadeWidth=radius*2+1;

        if ( (s=getParameter("hpadding")) != null )
            hpadding=Integer.parseInt(s);

        if ( (s=getParameter("vpadding")) != null )
            vpadding=Integer.parseInt(s);

        if ( (s=getParameter("isvacuum")) != null )
            if (Integer.parseInt(s)==1)
                isVacuum=true;

        if ( (s=getParameter("delay")) != null )
            delay=Integer.parseInt(s);

        if ( (s=getParameter("step")) != null )
            step=Integer.parseInt(s);

        offI = do3DText(Text);
    }

    public void paint(Graphics g) 
    {
        this.getGraphics().clearRect(0,0,this.size().width-1, this.size().height-1);
    }
    
    public void repaint() 
    {
    }

    public void update( Graphics g) 
    {
      //paint(g);
    }
    
    Image do3DText(String s) 
    {
        int i,j,n,m;

        int width = this.getGraphics().getFontMetrics().stringWidth(s);
        int height = this.getGraphics().getFontMetrics().getHeight();
        this.width = width+2*hpadding+1;
        this.height = height+2*vpadding+1;
        this.pX = 0;
        this.pY = this.height/2;
        Image tempI = createImage(this.width, this.height);
        Graphics tempG = tempI.getGraphics();

        tempG.setFont(this.getGraphics().getFont());
        tempG.setColor(getBackground());
        tempG.fillRect(0,0,this.width, this.height);
        tempG.setColor(new Color(~getBackground().getRGB()));
        tempG.drawString(s,hpadding,height-this.getGraphics().getFontMetrics().getDescent()+vpadding);
        tempG.setColor(getForeground());
        tempG.drawString(s,hpadding-1,height-this.getGraphics().getFontMetrics().getDescent()+vpadding-1);
	    return tempI;
    }
    
    public void Efficinet() 
    {
        int radius2=radius*radius;
        int rgb, rgb2, i, l, m, n, c, cc, x, y, bgColor;
        double f;


        pX += step;
        if (pX>width-radius)
            if (Math.random()<.5) 
            {
                step=-step;
                pX += step;
            }
            else 
            {
                pX = radius;
            }
        else
            if (pX<radius)
                if (Math.random()<.5) 
                {
                    step=-step;
                    pX += step;
                }
                else 
                {
                    pX=width-radius;
                }
        if (isVacuum)
          bgColor=getBackground().getRGB();
        else
          bgColor=0;
        n=0;

        int pixels0[], pixels[], lower[], upper[], range[];
        pixels0 = new int[width*height];
        pixels = new int[fadeWidth*fadeWidth];

	    PixelGrabber pg = new  PixelGrabber(offI, 0, 0, width, height, pixels0, 0, width);
        try  {
            pg.grabPixels();
        }
        catch  (InterruptedException e)  {
            System.err.println("interrupted  waiting  for  pixels!");
        }

        lower=new int[3];
        upper=new int[3];
        range=new int[3];
        for (i=0,l=0,m=0xff; i<3; i++, l+=8, m=m<<8) 
        {
            lower[i] = (shiftFrom & m) >> l;
            upper[i] = (shiftTo & m) >> l;
            range[i] = upper[i]-lower[i];
        }

        for (y=-fadeWidth+radius; y<radius; y++)
            for (x=-fadeWidth+radius; x<radius; x++) 
            {
                rgb = pX+x+(pY+y)*width;
                if (rgb>=0 && rgb<width*height) 
                {
                    rgb = pixels0[rgb];
                    c=x*x+y*y;
                    if (c<=radius2 && rgb!=bgColor) 
                    {
                        rgb2=0xff000000;
                      	f = (1-Math.sqrt(c)/radius)/1.000001;
        	            for (i=0, l=0, m=0xff; l<24; i++, l+=8, m=m<<8) 
        	            {
    	                    cc = ((rgb&m)>>l) +  lower[i]+(int) (f*range[i]);
            	            rgb2 |= (cc>255?255:cc)<<l;
                        }
                        pixels[n++]=rgb2;
                    }
                    else
                        pixels[n++]=rgb;
                }
                else {
                    pixels[n++]=0xff000000;
                }
            }
        Image imOffscreen, imTmp;
        imTmp = createImage(new  MemoryImageSource(fadeWidth, fadeWidth,  pixels,  0,  fadeWidth));
        imOffscreen = createImage(width,height);
        imOffscreen.getGraphics().drawImage(offI,0,0,this);
        imOffscreen.getGraphics().drawImage(
            imTmp, pX-fadeWidth+radius,pY-fadeWidth+radius,this);
        int w0=this.size().width, h0=this.size().height;
        this.getGraphics().clearRect(0,0,w0-1,(h0-height)/2-1);
        this.getGraphics().clearRect(0,(h0+height)/2,w0-1,h0-1);
        this.getGraphics().clearRect(0,0,(w0-width)/2-1,h0-1);
        this.getGraphics().clearRect((w0+width)/2,0,w0-1,h0-1);
        this.getGraphics().drawImage(imOffscreen,(this.size().width-width)/2,(this.size().height-height)/2,this);
    }


    
    
}


⌨️ 快捷键说明

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