home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 27 / IOPROG_27.ISO / SOFT / MAC.ZIP / mac.java < prev    next >
Encoding:
Java Source  |  1998-09-25  |  11.4 KB  |  432 lines

  1. import java.net.*;
  2. import java.lang.*;
  3. import java.util.*;
  4. import java.awt.*;
  5. import java.applet.*;
  6. // menu1 = file
  7. // pop1  = 1:10
  8. public class mac extends Applet
  9. {
  10.  Image wallpaper;
  11.  String frame_target;
  12. //for Icons (size=32x32)
  13.  int iconWH=32;
  14.  int iconCount=0;
  15.  Vector iconText  =new Vector();
  16.  Vector icons     =new Vector();
  17.  Vector iconX     =new Vector();
  18.  Vector iconY     =new Vector();
  19.  Vector iconLink  =new Vector();
  20. //for MainMenu
  21.  String applebtn="Start";
  22.  int applebtnEX=0;
  23.  FontMetrics f;
  24.  Image applelogo,buffer=null;
  25.  Vector mpops=new Vector();
  26.  Vector mlinks=new Vector();
  27.  Vector macts=new Vector();
  28.  Vector mitems=new Vector();
  29.  boolean applepop=false;
  30.  boolean mclick=false;
  31.  
  32.  int pop=-1;
  33.  int mix=0,miy=0;
  34.  int popselect=-1;
  35.  int mitemCount=0;
  36.  Dimension d=size();
  37.  int fwidth=100;
  38.  int xmenu=0;
  39.  int mouseX=0,mouseY=0;
  40.  int fheight=0;
  41.  String message;
  42.  boolean message_show=false;
  43. //code for Icons
  44.   public void addIcon(String txt,Image img,String href,String x,String y)
  45.   { iconText.addElement(txt);
  46.     icons.addElement(img);
  47.     iconLink.addElement(href);
  48.     iconX.addElement(x);
  49.     iconY.addElement(y);
  50.     iconCount++;
  51.   }
  52. //code for MainMenu
  53.   public void addMItem(String caption)
  54.   {
  55.     mitems.addElement(caption);
  56.     mpops.addElement(new Vector());
  57.     mlinks.addElement(new Vector());
  58.     macts.addElement("none");
  59.     mitemCount++;
  60.   };
  61. //code for messageBox
  62.  public void message_box(String caption)
  63.  {
  64.   message=caption;
  65.   message_show=true;
  66.   repaint();
  67.  };
  68.   public boolean mouseUp(Event e,int x,int y)
  69.   { int tempY=0,i=0,maxlength=0,j=0,tx=0,ix=0,iy=0,iex=0;
  70.     mouseX=x;
  71.     mouseY=y;
  72.   if(mouseY<=fheight)mclick=true;
  73.     message_show=false;
  74.   if(pop>-1)
  75.   {
  76.   if(!((Vector)mpops.elementAt(pop)).isEmpty())
  77.   {
  78.   Vector temp=(Vector)mpops.elementAt(pop);
  79.   Vector temp2=(Vector)mlinks.elementAt(pop);
  80.  
  81.     for(j=0;j<temp.size();j++)
  82.         if(maxlength<f.stringWidth((String)temp.elementAt(j)))
  83.            maxlength=f.stringWidth((String)temp.elementAt(j));
  84.          maxlength=maxlength+32;
  85.     for(j=0;j<temp.size();j++)
  86.     if((mouseY>(j+1)*fheight)
  87.         &&(mouseY<fheight*(j+2))
  88.         &&(mouseX>=mix)
  89.         &&(mouseX<=maxlength+mix))
  90.     {
  91.     popselect=j;
  92.     do_link((String)temp2.elementAt(j));
  93.     }
  94.     else
  95.     {popselect=-1;pop=-1;}
  96.   };
  97.   };
  98.   i=0;
  99.   // logo button
  100.    if((mouseY<fheight)&&(mouseX<=16)&&(applepop)){i=1;pop=0;mix=0;mclick=false;}
  101.   // x button
  102.    if((mouseY<fheight)&&(mouseX>d.width-16))
  103.    {xmenu++;
  104.    switch(xmenu)
  105.    {
  106.    case 1:
  107.     message_box("RDESKTOP II (C) Raymond Anthony Samalo June 1998 ");
  108.    break;
  109.    case 2:
  110.     message_box("This is a FREE ware, Desktop like Site navigation tool.");
  111.    break;
  112.    case 3:
  113.     message_box("Any support moral, financial :), critics are welcome.");
  114.    break;
  115.    case 4:
  116.     message_box("Contact me at eng60728@leonis.nus.edu.sg");
  117.     break;
  118.    case 5:
  119.     message_box("Visit http://members.tripod.com/~yarsa");
  120.     xmenu=0;
  121.    break;
  122.    default:
  123.     message_box("Click on the logo, menu or icon, to navigate ");
  124.    break;
  125.    };
  126.    mclick=false;
  127.    }
  128.  
  129.  
  130.   if(iconCount>0)
  131.    for(i=iconCount-1;i>=0;i--)
  132.    {
  133.     if(icons.elementAt(i)!=null)
  134.     {
  135.     ix= Integer.parseInt((String)  iconX.elementAt(i))-iconWH/2;
  136.     iex=iconWH;
  137.     iy= Integer.parseInt((String)  iconY.elementAt(i))+iconWH;
  138.     if((ix<=mouseX)&&(iy-iconWH<=mouseY)
  139.         &&(ix+iex>=mouseX)&&(iy+fheight-8>=mouseY))
  140.         {do_icon(i);break;}
  141.     }else break;
  142.     }
  143.     return true;
  144.   }
  145.   public boolean mouseMove(Event e,int x,int y)
  146.   {
  147.     int tempY=0;
  148.     mouseX=x;
  149.     mouseY=y;
  150.     repaint();
  151.     return true;
  152.   }
  153.   public void update(Graphics g){paint(g);}
  154.   public boolean imageUpdate(Image img,int flags,int x,int y, int w,int h)
  155.   {
  156.   if((flags&SOMEBITS)!=0){repaint(x,y,w,h);
  157.   } else if((flags&ABORT)!=0)
  158.   {repaint();}
  159.   return(flags&(ALLBITS|ABORT))==0;
  160.   }
  161.  
  162. public void paint(Graphics g)
  163. {
  164.   Graphics screengc=null;
  165.   screengc=g;
  166.   g=buffer.getGraphics();
  167.   int i=0;
  168.   int j=0;
  169.   String str;
  170.   int y=0;
  171.   int maxy=0;
  172.   int mxlength=0;
  173.  
  174.   int ix=0,iy=0,iex=0;
  175.   int x=0;
  176.   d=size();
  177.     g.setColor(getColor("bgcolor",Color.lightGray));
  178.     g.fill3DRect(0,0,d.width,d.height,true);
  179.  
  180.   f=g.getFontMetrics();
  181.   fheight= f.getHeight()+3;
  182.   if(wallpaper!=null) g.drawImage(wallpaper,0,0,d.width,d.height-8,this);
  183.   g.setColor(getColor("mnu0color",Color.white));
  184.   g.fill3DRect(0,0,d.width,fheight,true);
  185.   g.setColor(getColor("mnu1color",Color.black));
  186.   g.drawRect(0,0,d.width,fheight);
  187. // draw icon
  188.   if(iconCount>0)
  189.    for(i=iconCount-1;i>=0;i--)
  190.    {
  191.     if(icons.elementAt(i)!=null)
  192.     {
  193.     g.setColor(Color.lightGray);
  194.     iex= g.getFontMetrics().stringWidth((String) iconText.elementAt(i));
  195.     ix= Integer.parseInt((String)  iconX.elementAt(i))-
  196.     iex/2;
  197.     iy= Integer.parseInt((String)  iconY.elementAt(i))+iconWH;
  198.     if((ix<=mouseX)&&(iy-iconWH<=mouseY)
  199.         &&(ix+iex>=mouseX)&&(iy+fheight-8>=mouseY))
  200.      g.draw3DRect(ix-4,iy-iconWH-4,iex+8,fheight+iconWH,true);
  201.     g.setColor(Color.black);
  202.     g.fillRect(
  203.      ix,
  204.      iy+2,
  205.      g.getFontMetrics().stringWidth((String) iconText.elementAt(i)),
  206.      fheight-8
  207.     );
  208.     g.setColor(Color.white);
  209.     g.drawString(
  210.     (String) iconText.elementAt(i),
  211.      ix,
  212.      iy+fheight-8
  213.     );
  214.     g.drawImage(
  215.      (Image)icons.elementAt(i),
  216.      ix+iconWH/2,
  217.      iy-iconWH,
  218.      iconWH,iconWH,this
  219.     );
  220.     };
  221.  
  222. };
  223. // draw message
  224.   if(message_show)
  225.   { int message_width=g.getFontMetrics().stringWidth(message)+64;
  226.     int message_x=(d.width-message_width)/2;
  227.     int message_y=d.height/2-fheight*2;
  228.     g.setColor(getColor("msg0color",Color.gray));
  229.     g.fill3DRect(message_x,message_y,message_width,fheight*4,true);
  230.     g.setColor(getColor("msg1color",Color.white));
  231.     g.drawString(message,message_x+32,message_y+fheight*2);
  232.    if(applelogo!=null)
  233.      g.drawImage(applelogo,message_x+2,message_y+fheight,28,28,this);
  234.   };
  235. // draw apple button
  236.   applebtnEX=16;
  237. i=0;
  238.    if(applelogo!=null)
  239.    {
  240.    g.drawImage(applelogo,0,0,16,fheight,this);
  241.    if((mouseY<fheight)&&(mouseX>=0)&&(mouseX<=16))
  242.     g.draw3DRect(0,0,16,fheight,true);
  243.    i=1;
  244.    };
  245.  
  246. // draw main menu
  247.    for(;i<mitemCount;i++)
  248.    { x+=applebtnEX;
  249.     x+=16;
  250.     if(i==mitemCount-1) x=d.width-16;
  251.     str=(String)mitems.elementAt(i);
  252.     applebtnEX=g.getFontMetrics().stringWidth(str);
  253.     if((mouseY<fheight)&&(mouseX>=x)&&(mouseX<=x+applebtnEX))
  254.     {
  255.     if(mclick){
  256.     pop=i;mix=x;
  257.     if(!(macts.elementAt(i).equals("none")))
  258.         do_link((String)macts.elementAt(i));
  259.     };
  260.     g.setColor(getColor("mnu1color",Color.black));
  261.     g.fillRect(x,1,applebtnEX,fheight-2);
  262.     g.setColor(getColor("mnu0color",Color.white));
  263.     g.drawString(str,x,fheight-3);
  264.     }
  265.     else
  266.     {
  267.     g.setColor(getColor("mnu0color",Color.white));
  268.     g.fillRect(x,1,applebtnEX,fheight-2);
  269.     g.setColor(getColor("mnu1color",Color.black));
  270.     g.drawString(str,x,fheight-3);
  271.     };
  272.    };
  273.     mclick=false;
  274.  
  275. // draw pop menu
  276.    x=mix;
  277.    if(pop>-1)
  278.       if(!((Vector)mpops.elementAt(pop)).isEmpty())
  279.       { Vector temp=(Vector)mpops.elementAt(pop);
  280.         for(j=0;j<temp.size();j++)
  281.         if(mxlength<g.getFontMetrics().stringWidth((String)temp.elementAt(j)))
  282.            mxlength=g.getFontMetrics().stringWidth((String)temp.elementAt(j));
  283.          mxlength=mxlength+32;
  284.          g.setColor(getColor("mnu0color",Color.white));
  285.          g.fillRect(x,fheight,mxlength,temp.size()*fheight);
  286.          g.setColor(getColor("mnu1color",Color.black));
  287.          g.drawRect(x,fheight,mxlength,temp.size()*fheight);
  288.          for(j=0;j<temp.size();j++)
  289.         {
  290.          g.setColor(getColor("mnu1color",Color.black));
  291.          if((mouseY>fheight*(j+1))&&(mouseY<fheight*(j+2))
  292.             &&(mouseX>x)&&(mouseX<mxlength+x))
  293.             {
  294.          g.fill3DRect(x,(j+1)*fheight,mxlength,fheight,true);
  295.          g.setColor(getColor("mnu0color",Color.white));
  296.             }
  297.             
  298.          g.drawString((String)temp.elementAt(j),x+16,(j+2)*fheight);
  299.         };
  300.       };
  301. screengc.drawImage(buffer,0,0,null);
  302. };
  303. // icon handler
  304.  public void do_icon(int i)
  305.  {
  306.   do_link((String)iconLink.elementAt(i));
  307.  }
  308. // link executor
  309.  public void do_link(String link)
  310.  {
  311.   if (!(link.equals("none")))
  312.   {
  313.   if(link.startsWith("say:"))
  314.    message_box(link.substring(4));
  315.   else
  316.   {
  317.    try{
  318.    if (frame_target==null){
  319.    getAppletContext().showDocument(new URL(link));
  320.    }
  321.    else
  322.    {
  323.    getAppletContext().showDocument(new URL(link),frame_target);
  324.    }
  325.     }
  326.    catch(MalformedURLException er)
  327.    { message_box("Malformed URL error at "+link);}
  328.   }
  329.   }
  330.  }
  331. // INIT() 
  332.  public void init()
  333.  {int i=0,k=0;
  334.   int j=0;
  335.   String str;
  336.   d=size();
  337.   buffer=createImage(d.width,d.height);
  338. // frame handler
  339.   frame_target=getParameter("target"); 
  340. //end_frame
  341.   if(getParameter("img")!=null) wallpaper=getImage(getDocumentBase(),getParameter("img"));
  342. k=0;
  343.   if(getParameter("logo")!=null)
  344.   {
  345.   addMItem("@");// logo pop up
  346.   applelogo=getImage(getDocumentBase(),getParameter("logo"));
  347.  if((str=getParameter("poplogo"))!=null)
  348.  {
  349.   applepop=true;
  350.   Vector temp=(Vector)(mpops.elementAt(0));
  351.   Vector temp2=(Vector)(mlinks.elementAt(0));
  352.   for(j=1;getParameter(str+j)!=null;j++)
  353.   {
  354.    temp.addElement(getParameter(str+j));
  355.    temp2.addElement(getParameter(str+"link"+j)==null?"none":getParameter(str+"link"+j));
  356.   };
  357.   k=1;
  358.  };
  359.   };
  360.  
  361. for(i=1;getParameter("menu"+i)!=null;i++)
  362. {
  363.  addMItem(getParameter("menu"+i));
  364.  if((str=getParameter("pop"+i))!=null)
  365.  {
  366.   Vector temp=(Vector)(mpops.elementAt(k+i-1));
  367.   Vector temp2=(Vector)(mlinks.elementAt(k+i-1));
  368.   for(j=1;getParameter(str+j)!=null;j++)
  369.   {
  370.    temp.addElement(getParameter(str+j));
  371.    temp2.addElement(getParameter(str+"link"+j)==null?"none":getParameter(str+"link"+j));
  372.   };
  373.  }else if((str=getParameter("link"+i))!=null)
  374.  {
  375.   macts.setElementAt(str,k+i-1);
  376.  };
  377. };
  378.     addMItem("?");
  379.  
  380.   for(i=1;getParameter("icon"+i)!=null;i++)
  381.   addIcon(
  382.    (getParameter("itext"+i)!=null)?getParameter("itext"+i):"",
  383.    getImage(getDocumentBase(),getParameter("icon"+i)),
  384.    (
  385.    getParameter("ilink"+i)!=null)?getParameter("ilink"+i):"none",
  386.    getParameter("ix"+i),
  387.    getParameter("iy"+i)
  388.   );
  389.  
  390.  message_box(
  391.    (
  392.    getParameter("msg")!=null)?getParameter("msg")
  393.    :
  394.    "Welcome To (C)RDesktop II"
  395.   );
  396.  
  397.  };
  398.  
  399.  
  400. // get color 
  401.  Color getColor(String sx, Color standard)
  402.  {
  403.    String s=getParameter(sx);
  404.    if(s==null){return standard;}
  405.    else
  406.    {
  407.     if(s.charAt(0)=='#')return(new Color(
  408.                                Integer.parseInt(s.substring(1),16)
  409.                                ));
  410.     else
  411.     {
  412.      if(s.equals("black"))return(Color.black);
  413.      if(s.equals("white"))return(Color.white);
  414.      if(s.equals("lightGray"))return(Color.lightGray);
  415.      if(s.equals("gray"))return(Color.gray);
  416.      if(s.equals("darkGray"))return(Color.darkGray);
  417.      if(s.equals("red"))return(Color.red);
  418.      if(s.equals("pink"))return(Color.pink);
  419.      if(s.equals("magenta"))return(Color.magenta);
  420.      if(s.equals("yellow"))return(Color.yellow);
  421.      if(s.equals("green"))return(Color.green);
  422.      if(s.equals("cyan"))return(Color.cyan);
  423.      if(s.equals("blue"))return(Color.blue);
  424.     };
  425.    };
  426.    return(standard);
  427.  };
  428. // end get color
  429. };
  430. // end mac applet
  431.                     
  432.