home *** CD-ROM | disk | FTP | other *** search
Java Source | 1996-10-10 | 5.4 KB | 246 lines | [TEXT/CWIE] |
- import java.applet.*;
- import java.awt.*;
- import java.awt.image.*;
- import java.io.*;
- import java.net.*;
-
- public class HighlightedImageMap extends Applet
- {
-
- Image normalimg;
- Image highlightedimg=null;
-
- public boolean nloaded=false;
- public boolean hloaded=false;
-
- boolean debug=false;
-
- public void start()
- {
- //Check if the images are already loaded. (Cache fix)
- if ((checkImage(normalimg,this) & ImageObserver.ALLBITS)==ImageObserver.ALLBITS)
- {
- nloaded=true;
- }
-
- if ((checkImage(highlightedimg,this) & ImageObserver.ALLBITS)==ImageObserver.ALLBITS)
- {
- hloaded=true;
- }
-
- //Paint it!
- repaint();
- }
-
- public void init()
- {
- //Applet name
- System.out.println("HighlightedImageMap v1.11\nRickard ÷berg(rickard@lysator.liu.se) 1996");
-
- //Show "Loading"
- repaint();
-
- //Show debug(X/Y) info?
- debug=(getParameter("debug")==null)?false:true;
-
- //Target frame name. If null then same frame is target
- //Either one target for every link, or one for each link
-
- String target=getParameter("target");
- if (target==null)
- target="";
-
- boolean individualtargets=false; //If true, each link has it's own target frame/window
- StringBufferInputStream tsbis=new StringBufferInputStream(target);
- StreamTokenizer tst=new StreamTokenizer(tsbis);
-
- if (target.indexOf(',')!=-1)
- individualtargets=true; //Individual link targets
-
- //Get images
- URL hosturl=getDocumentBase();
- URL normalurl;
- URL highlightedurl;
-
- try
- {
- normalurl=new URL(hosturl,getParameter("normal"));
- highlightedurl=new URL(hosturl,getParameter("highlight"));
- }
- catch(Exception e)
- {
- System.out.println("Error: 'normal' or 'highlight' not valid");
- return;
- }
-
- normalimg=getImage(normalurl);
- highlightedimg=getImage(highlightedurl);
- prepareImage(normalimg,this);
- prepareImage(highlightedimg,this);
-
- //Get rid of the layout manager: WE DON'T WANT IT!
- setLayout(null);
-
- /*
- *
- * Parse links
- *
- */
-
- String links=getParameter("links");
-
- //No links, no imagemap
- if (links==null)
- {
- System.out.println("Error: no links specified");
- return;
- }
-
- StringBufferInputStream sbis=new StringBufferInputStream(links);
- StreamTokenizer st=new StreamTokenizer(sbis);
- st.quoteChar(39);
-
- //Syntax {ulx,uly,lrx,lry,"URL"}
- int x,y,w,h;
- URL url; //Link URL
- String alt;
- boolean done=false; //Done parsing links?
-
- try
- do
- {
- //Default alternative text
- alt="";
-
- if (st.nextToken()!='{') throw new Exception("{ expected");
-
- //Upper left x
- if (st.nextToken()!=StreamTokenizer.TT_NUMBER) throw new Exception("number expected");
- x=(int)st.nval;
-
- // ','
- if (st.nextToken()!=',') throw new Exception("',' expected");
-
- //Upper left y
- if (st.nextToken()!=StreamTokenizer.TT_NUMBER) throw new Exception("number expected");
- y=(int)st.nval;
-
- // ','
- if (st.nextToken()!=',') throw new Exception("',' expected");
-
- //Lower right x
- if (st.nextToken()!=StreamTokenizer.TT_NUMBER) throw new Exception("number expected");
- w=(int)st.nval-x;
-
- // ','
- if (st.nextToken()!=',') throw new Exception("',' expected");
-
- //Lower right y
- if (st.nextToken()!=StreamTokenizer.TT_NUMBER) throw new Exception("number expected");
- h=(int)st.nval-y;
-
- // ','
- if (st.nextToken()!=',') throw new Exception("',' expected");
-
- //URL
- if (st.nextToken()!=39) throw new Exception("quoted url expected(use ' for quotation)");
- url=new URL(getDocumentBase(),st.sval);
-
- //Optional alternative text
- if (st.nextToken()==',')
- {
- if (st.nextToken()==39)
- alt=st.sval;
- else
- throw new Exception("alternative linktext expected");
- }
- else
- st.pushBack();
-
- if (st.nextToken()!='}') throw new Exception("} expected");
-
- //Get target name
- String linktarget; //Target for this particular link
- if (individualtargets)
- {
- tst.nextToken();
- linktarget=tst.sval;
- tst.nextToken();
- }
- else
- linktarget=target; //Use global target
-
- //Create images for link
- ImageFilter cropfilter = new CropImageFilter(x,y,w,h);
- Image nimg = createImage(new FilteredImageSource(normalimg.getSource(),
- cropfilter));
-
- Image hlimg = createImage(new FilteredImageSource(highlightedimg.getSource(),
- cropfilter));
-
- //Add link
- add(new HighlightableLink(x,y,w,h,nimg,hlimg,url,alt,linktarget,this));
-
- //Check if end of links
- if (st.nextToken()==StreamTokenizer.TT_EOF)
- done=true;
-
- st.pushBack();
-
- } while(!done);
- catch(Exception e)
- {
- System.out.println("Error in links parameter:");
- System.out.println(e);
- }
- }
-
- public void paint(Graphics g)
- {
- if (nloaded && hloaded)
- {
- g.drawImage(normalimg,0,0,this);
- }
- else
- g.drawString("Loading",0,10);
-
- paintComponents(g);
- }
-
- public void update(Graphics g)
- {
- paint(g);
- }
-
- public boolean imageUpdate(Image img, int infoflags,
- int x, int y, int width, int height)
- {
- if (img==normalimg && (infoflags & ImageObserver.ALLBITS)==ImageObserver.ALLBITS)
- {
- nloaded=true;
- }
-
- if (img==highlightedimg && (infoflags & ImageObserver.ALLBITS)==ImageObserver.ALLBITS)
- {
- hloaded=true;
- }
-
- if (nloaded && hloaded)
- repaint();
-
- return !(hloaded && nloaded);
- }
-
- public boolean mouseMove(Event evt, int x, int y)
- {
- //Show debug(X/Y) info
- if (debug)
- {
- showStatus("X: "+x+" Y: "+y);
- return true;
- }
-
- return false;
- }
-
- }