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

📄 anbutton.java

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

public class ANButton extends Applet
    implements Runnable
{

    private MediaTracker tracker = null;
    private Image buf = null;
    private Image bg = null;
    private Image img[] = null;
    private int X = 0;
    private int Y = 0;
    private Graphics offscreen = null;
    private Dimension d = null;
    private boolean onButt = false;
    private boolean pressedButt = false;
    private boolean three_state = false;
    private boolean userPause = false;
    private int onIs = 0;
    private int animImg = 0;
    private int maxImg = 0;
    private int pause = 0;
    private URL clickDest = null;
    private String dest = null;
    private Thread engine = null;

    public void init()
    {
    	
        d = getSize();
        buf = createImage(d.width, d.height);
        offscreen = buf.getGraphics();
        int i = 0;
        int count = 0;
        String s;
        while (getParameter("image" + count) != null)
          count++;
        img = new Image[count];  
        boolean flag = false;
        tracker = new MediaTracker(this);
        for(; !flag && i < count; i++)
        {
        
            s = getParameter("image" + i);
            if(s == null)
            {
        
                if(i > 0)
                {
        
                    flag = true;
        
                }
                else
                {
        
                    three_state = false;
                    onIs = 3;
        
                }
        
            }
            else
            {
        
                if(i == 0)
                    three_state = true;
                showStatus("Loading image " + s + ".");
                img[i] = getImage(getCodeBase(), s);
                tracker.addImage(img[i], 0);
                try
                {
        
                    tracker.waitForAll();
        
                }
                catch(InterruptedException _ex)
                {
        
                    System.out.println(
                    "Error waiting for image" + i + " to load");
        
                }
        
            }
        
        }

        maxImg = i - 2;
        if(maxImg < 3)
        {
        
            System.out.println(
            "Need at least images 1 to 3: Check Applet Tag.");
            for(int j = maxImg + 1; j < 4; j++)
                img[j] = img[1];

        
            maxImg = 3;
        }
        String s1 = getParameter("x");
        X = s1 == null ? 0 : Integer.parseInt(s1);
        s1 = getParameter("y");
        Y = s1 == null ? 0 : Integer.parseInt(s1);
        s1 = getParameter("pause");
        pause = s1 == null ? 200 : Integer.parseInt(s1);
        s1 = getParameter("dest");
        try
        {
        
            clickDest = new URL(dest);
            return;
        
        }
        catch(MalformedURLException _ex)
        {
        
            System.out.println(
            "Malformed URL: Check Applet Tag.");
        
        }
    }

    public void start()
    {
        
        if(engine == null && !userPause)
        {
        
            engine = new Thread(this);
            engine.start();
        
        }
    
    }

    public void stop()
    {
    
        if(engine != null && engine.isAlive())
            engine.stop();
        engine = null;
    
    }

    public void destroy(){}

    public boolean mouseDown(Event event, int i, int j)
    {
        
        if(engine != null && engine.isAlive())
        {
        
            userPause = true;
            engine.suspend();
            stopPlaying();
        
        }
        pressedButt = true;
        repaint();
        return true;
    
    }

    public boolean mouseUp(Event event, int i, int j)
    {
    
        if(pressedButt && onButt)
        {
    
            pressedButt = false;
            repaint();
            getAppletContext().showDocument(clickDest);
    
        }
        else
        {
    
            pressedButt = false;
            repaint();
    
        }
        return true;
    
    }

    public boolean mouseEnter(Event event, int i, int j)
    {
    
        onButt = true;
        userPause = false;
        if(engine != null && engine.isAlive())
        {
    
            engine.resume();
            startPlaying();
    
        }
        else
        {
    
            engine = new Thread(this);
            engine.start();
            startPlaying();
    
        }
        repaint();
        showStatus(dest);
        return true;
    }

    public boolean mouseExit(Event event, int i, int j)
    {
    
        onButt = false;
        userPause = true;
        if(engine != null && engine.isAlive())
        {
    
            engine.suspend();
            stopPlaying();
    
        }
        repaint();
        showStatus("");
        return true;
    
    }

    void startPlaying(){}

    void stopPlaying(){}

    public void run()
    {
    
        Thread thread = Thread.currentThread();
        thread.setPriority(1);
        while(engine == thread) 
        {
    
            try
            {
    
                Thread.sleep(pause);
    
            }
            catch(InterruptedException _ex) { }
            animImg++;
            if(animImg > maxImg)
                animImg = 3;
            repaint();
    
        }

    }

    public void update(Graphics g)
    {
    
        if(!onButt)
        {
    
            if(three_state)
                onIs = 0;
            else
                onIs = 3;
     
        }
        else
        if(onButt && !pressedButt)
            onIs = 3;
        else
            onIs = 2;
        paint(g);
    
    }

    public void paint(Graphics g)
    {
    
        if(offscreen != null)
        {
    
            paintApplet(offscreen);
            g.drawImage(buf, 0, 0, this);
            return;
    
        }
        else
        {
    
            paintApplet(g);
            return;
    
        }
    
    }

    public void paintApplet(Graphics g)
    {
    
        int i = onIs;
        if(onIs == 3)
            i = animImg;
        g.drawImage(img[1], 0, 0, null);
        g.drawImage(img[i], X, Y, null);
    
    }

}

⌨️ 快捷键说明

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