Simple Button Applet




Download
© Comweb.nl , RJHM van den Bergh , sales@comweb.nl


File 1 of 2: SimpleButton.java (Source code)
/*
	A basic extension of the java.applet.Applet class
 */

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.net.*;
import java.util.*;

public class SimpleButton extends Applet implements MouseListener
{
    private URL url;
    private String target;
    private AudioClip down,over;
    private int buttonStatus;
    private String buttonName;
    private Image upImage;
    private Image overImage;

    private MediaTracker tracker;

	public void init()
	{
	    System.out.println(this.getAppletInfo());

	    buttonName="";
	    String param=getParameter("button_name");
	    if (param!=null) buttonName=param;

	    param=getParameter("down");
	    if (param!=null) down=getAudioClip(getCodeBase(),param);
	    param=getParameter("over");
	    if (param!=null) over=getAudioClip(getCodeBase(),param);

	    try
	    {
	        param=getParameter("url");
	        if (param!=null)
	        {
	            if (param.toLowerCase().startsWith("http://") || param.toLowerCase().startsWith("ftp://") || param.toLowerCase().startsWith("mailto://") || param.toLowerCase().startsWith("gopher://"))
	            {
	                url=new URL(param);
	            } else
	            {
	                url=new URL(getCodeBase(),param);
	            }
	        }
	        target=getParameter("target");
	    } catch (MalformedURLException mu)
	    {
	        System.out.println(mu.toString());
	    }


  	    tracker=new MediaTracker(this);
  	    String upImageName=getParameter("up_picture");
  	    if (upImageName!=null)
  	    {
  	        upImage=getImage(getCodeBase(),upImageName);
            tracker.addImage(upImage,0);
        } else System.out.println("SimpleButton: applet param up_picture not found.");

  	    String overImageName=getParameter("down_picture");
  	    if (overImageName!=null)
  	    {
  	        overImage=getImage(getCodeBase(),overImageName);
            tracker.addImage(overImage,1);
        } else System.out.println("SimpleButton: applet param down_picture not found.");

	    this.setBackground(Color.gray);

	    this.addMouseListener(this);
	}

    public synchronized void mouseClicked(MouseEvent e)
    {
        if (e.getSource().equals(this))
        {
            if (url!=null)
            {
                if (target==null)
                {
                    this.getAppletContext().showDocument(url);
                } else
                {
                    this.getAppletContext().showDocument(url,target);
                }
            }
        }
    }

    public synchronized void mouseEntered(MouseEvent e)
    {
        try {InfoApplet.button=buttonName;}
        catch (Exception ex)
        {
            if ( !(ex instanceof ClassNotFoundException)) System.out.println(ex);
        }
        if (e.getSource().equals(this))
        {
            if (over!=null) over.play();
        }
        buttonStatus=1;
        repaint();
    }

    public synchronized void mouseExited(MouseEvent e)
    {
        try { InfoApplet.button=""; }
        catch (Exception ex)
        {
            if ( !(ex instanceof ClassNotFoundException)) System.out.println(ex);
        }
        buttonStatus=0;
        repaint();
    }

    public synchronized void mousePressed(MouseEvent e)
    {
        if (e.getSource().equals(this))
        {
            if (down!=null) down.play();
        }
    }

    public synchronized void mouseReleased(MouseEvent e){}

    public String getAppletInfo()
    {
        return "SimpleButton 1.x , sales@comweb.nl \nRevision Okt 06 2012 \n";
    }

    public void paint(Graphics g)
    {
        if (tracker.isErrorAny()) System.out.println("Error");
        if (tracker.statusAll(true) == MediaTracker.COMPLETE)
        {
            if (buttonStatus==0 && upImage!=null) g.drawImage(upImage, 0, 0, this);
            if (buttonStatus==1 && overImage!=null) g.drawImage(overImage, 0, 0, this);
        }
    }

}


File 2 of 2: InfoApplet.java (Source code)
/*
	A basic extension of the java.applet.Applet class
 */

import java.util.*;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.net.*;

public class InfoApplet extends Applet implements Runnable
{
    Thread runner;
    private Hashtable<String,Image> pictures;
    private Hashtable<String,Image> filesLoaded;
    private Image defaultImage;
    public static Object staticObject;
    public static String button;
    private String oldButton;
    boolean initialized;
    private boolean isNetscape;
    private boolean stopFlag=false;

	public void init()
	{
	    System.out.println(getAppletInfo());
	    button="";
	    oldButton="";
        isNetscape=System.getProperty("browser").startsWith("Netscape");
	    if (staticObject==null) staticObject=new Object();
	    initialized=false;
	}

	public void init2()
	{
	    repaint(); // workarround Ms Explorer 5.5 bug (Applet size not available before repaint )

	    filesLoaded= new Hashtable<String,Image>();

	    // get the default image
	    String param=getParameter("default");
	    if (param!=null)
	    {
	        defaultImage=getImage(param);
	    } else
	    {
	        System.out.println("No applet parameter default defined");
	    }

	    // add all pictures to the HashTable with String button as key
	    // pictures=new Hashtable(); obsolete need to determine Object class uses
	    pictures=new Hashtable<String,Image>();
	    int n=0;
	    while( (param=getParameter("button_"+(n+1)))!=null )
	    {
	        String key=null;
	        Image image=null;
	        StringTokenizer tk=new StringTokenizer(param,",|");
	        if (tk.countTokens()==2)
	        {
	            key=tk.nextToken().trim();
	            image=this.getImage(tk.nextToken());
	            pictures.put(key, image);
	        } else
	        {
	            System.out.println("Incorrect number of tokens at applet param button_"+(n+1));
	        }
	        n++;
	    }

	    Color bgColor=Color.gray;
	    param=getParameter("bgcolor");
	    if (param!=null)
	    {
	        if (param.startsWith("#"))
	        {
	            int num=Integer.parseInt(param.substring(1,param.length()),16);
	            bgColor=new Color(num);
	        } else
	        {
	            System.out.println("Syntax error\nParameter backgrond doesn't start with \"#\" only hex RGB values are supported." );
	        }
	    }
	    this.setBackground(bgColor);

	    initialized=true;

	    repaint();
	}

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

	public void run()
	{
	    if (!initialized) init2();
	    while (Thread.currentThread()==runner && !stopFlag)
	    {
	        if (oldButton!=button) { repaint(); oldButton=button;}
	        try{Thread.sleep(300);} catch (InterruptedException ie){}
	    }
	}

	public void stop()
	{
// methode Thread.stop() is deprecated
//	    if (runner!=null) { runner.stop();runner=null;}
	    if (runner!=null) { stopFlag=true; }
	}

    public String getAppletInfo()
    {
        return "InfoApplet 1.x , sales@comweb.nl \nRevision: Okt 06 2000 \n";
    }

    private Image getImage(String filename)
    {
        MediaTracker tracker=new MediaTracker(this);
        int trackerID=0;
        Image picture=null;
        if (filename!=null)
        {
            if (filesLoaded.containsKey(filename))
            {
                return (Image)(filesLoaded.get(filename));
            }
            synchronized (staticObject)
            {
                picture=getImage(getCodeBase(),filename);
                tracker.addImage(picture,trackerID);
                if (isNetscape)
                {
                    try {tracker.waitForAll();} catch (InterruptedException ie){}
                    if (tracker.isErrorAny())
                    {
                        System.out.println("SimpleButton:Error loading "+filename);
                        return null;
                    }
                } else
                {
                    while (tracker.statusID(trackerID, false) != MediaTracker.COMPLETE) // workarround for MS Explorer waitForAll thread death
                    {
                        try
                        {
                            tracker.waitForAll(200);
                            Thread.yield();
                        } catch (InterruptedException ie) {}
                        if (tracker.isErrorAny())
                        {
                            System.out.println("SimpleButton:Error loading "+filename);
                            return null;
                        }
                    }
                }
            }
            filesLoaded.put(filename,picture);
        }
        return picture;
    }

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

    public void paint(Graphics g)
    {
        if (!initialized)
        {
            String loadMessage="loading please wait.";
            String param=getParameter("load_message");
            if (param!=null) loadMessage=param;
            g.drawString(loadMessage,5,(int)(getSize().height/2));
            return;
        }

        Image image=null;
        if (button!=null)
        {
            if ( (button.trim()).equals(""))
            {
                image=defaultImage;
            }

            if (pictures.containsKey(button))
            {
                image=(Image)pictures.get(button);
            }
        }
        f (image!=null)
        {
            g.drawImage(image,0,0,this);
        } else System.out.println("InfoApplet.paint: Image to display == null");
    }

}