织梦CMS - 轻松建站从此开始!

罗索

当前位置: 主页>杂项技术>JAVA>

Java Applet实现一个字效,一个光照效果

罗索客 发布于 2004-07-15 10:53 点击:次 
高手们请帮我看下这两个JAVA APPLET程序,简单解释一下急用 import java.applet.Applet; import java.awt.*; import java.awt.image.MemoryImageSource; import java.awt.image.PixelGrabber; import java.io.PrintStream; public class SpotLight extends Applet imple
TAG:

高手们请帮我看下这两个JAVA APPLET程序,简单解释一下急用
import java.applet.Applet;
import java.awt.*;
import java.awt.image.MemoryImageSource;
import java.awt.image.PixelGrabber;
import java.io.PrintStream;

public class SpotLight extends Applet
    implements Runnable
{

    private String Text;
    private Image offI;
    private int pX;
    private int pY;
    private int width;
    private int height;
    private int fadeWidth;
    private Thread thread;
    private int radius;
    private int shiftFrom;
    private int shiftTo;
    private int delay;
    private int step;
    private int hpadding;
    private int vpadding;
    private int FontSize;
    private boolean isVacuum;

    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()
    {
        do
        {
            Efficinet();
            try
            {
                Thread.sleep(delay);
            }
            catch(InterruptedException _ex) { }
        } while(true);
    }

    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", 1, 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 && 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)
    {
        getGraphics().clearRect(0, 0, size().width - 1, size().height - 1);
    }

    public void repaint()
    {
    }

    public void update(Graphics g)
    {
    }

    Image do3DText(String s)
    {
        int i = getGraphics().getFontMetrics().stringWidth(s);
        int j = getGraphics().getFontMetrics().getHeight();
        width = i + 2 * hpadding + 1;
        height = j + 2 * vpadding + 1;
        pX = 0;
        pY = height / 2;
        Image image = createImage(width, height);
        Graphics g = image.getGraphics();
        g.setFont(getGraphics().getFont());
        g.setColor(getBackground());
        g.fillRect(0, 0, width, height);
        g.setColor(new Color(~getBackground().getRGB()));
        g.drawString(s, hpadding, (j - getGraphics().getFontMetrics().getDescent()) + vpadding);
        g.setColor(getForeground());
        g.drawString(s, hpadding - 1, ((j - getGraphics().getFontMetrics().getDescent()) + vpadding) - 1);
        return image;
    }

    public void Efficinet()
    {
        int i = radius * radius;
        pX += step;
        if(pX > width - radius)
        {
            if(Math.random() < 0.5D)
            {
                step = -step;
                pX += step;
            } else
            {
                pX = radius;
            }
        } else
        if(pX < radius)
            if(Math.random() < 0.5D)
            {
                step = -step;
                pX += step;
            } else
            {
                pX = width - radius;
            }
        int k3;
        if(isVacuum)
            k3 = getBackground().getRGB();
        else
            k3 = 0;
        int j2 = 0;
        int ai[] = new int[width * height];
        int ai1[] = new int[fadeWidth * fadeWidth];
        PixelGrabber pixelgrabber = new PixelGrabber(offI, 0, 0, width, height, ai, 0, width);
        try
        {
            pixelgrabber.grabPixels();
        }
        catch(InterruptedException _ex)
        {
            System.err.println("interrupted  waiting  for  pixels!");
        }
        int ai2[] = new int[3];
        int ai3[] = new int[3];
        int ai4[] = new int[3];
        int l = 0;
        int j1 = 0;
        for(int l1 = 255; l < 3; l1 <<= 8)
        {
            ai2[l] = (shiftFrom & l1) >> j1;
            ai3[l] = (shiftTo & l1) >> j1;
            ai4[l] = ai3[l] - ai2[l];
            l++;
            j1 += 8;
        }

        for(int j3 = -fadeWidth + radius; j3 < radius; j3++)
        {
            for(int i3 = -fadeWidth + radius; i3 < radius; i3++)
            {
                int j = pX + i3 + (pY + j3) * width;
                if(j >= 0 && j < width * height)
                {
                    j = ai[j];
                    int k2 = i3 * i3 + j3 * j3;
                    if(k2 <= i && j != k3)
                    {
                        int k = 0xff000000;
        double d = (1.0D - Math.sqrt(k2) / (double)radius) / 1.0000009999999999D;
                        int i1 = 0;
                        int k1 = 0;
                        for(int i2 = 255; k1 < 24; i2 <<= 8)
                        {
                   nt l2 = ((j & i2) >> k1) + ai2[i1] + (int)(d * (double)ai4[i1]);
                            k |= (l2 <= 255 ? l2 : 255) << k1;
                            i1++;
                            k1 += 8;
                        }

                        ai1[j2++] = k;
                    } else
                    {
                        ai1[j2++] = j;
                    }
                } else
                {
                    ai1[j2++] = 0xff000000;
                }
            }

        }

        Image image1 = createImage(new MemoryImageSource(fadeWidth, fadeWidth, ai1, 0, fadeWidth));
        Image image = createImage(width, height);
        image.getGraphics().drawImage(offI, 0, 0, this);
        image.getGraphics().drawImage(image1, (pX - fadeWidth) + radius, (pY - fadeWidth) + radius, this);
        int l3 = size().width;
        int i4 = size().height;
        getGraphics().clearRect(0, 0, l3 - 1, (i4 - height) / 2 - 1);
        getGraphics().clearRect(0, (i4 + height) / 2, l3 - 1, i4 - 1);
        getGraphics().clearRect(0, 0, (l3 - width) / 2 - 1, i4 - 1);
        getGraphics().clearRect((l3 + width) / 2, 0, l3 - 1, i4 - 1);
        getGraphics().drawImage(image, (size().width - width) / 2, (size().height - height) / 2, this);
    }

    public SpotLight()
    {
        radius = 25;
        step = 8;
        hpadding = 30;
        FontSize = 30;
        isVacuum = false;
    }
}
(iwgh)
本站文章除注明转载外,均为本站原创或编译欢迎任何形式的转载,但请务必注明出处,尊重他人劳动,同学习共成长。转载请注明:文章转载自:罗索实验室 [http://www.rosoo.net/a/200407/1383.html]
本文出处: 作者:iwgh
顶一下
(0)
0%
踩一下
(0)
0%
------分隔线----------------------------
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
评价:
表情:
用户名: 验证码:点击我更换图片
栏目列表
将本文分享到微信
织梦二维码生成器
推荐内容