desktop.java
资源名称:desktop.zip [点击查看]
上传用户:hsjcxs
上传日期:2007-01-07
资源大小:3k
文件大小:8k
源码类别:
Applet
开发平台:
Java
- import java.net.*;
- import java.lang.*;
- import java.util.*;
- import java.awt.*;
- import java.applet.*;
- public class desktop extends Applet
- {
- Frame f;
- Image wallpaper;
- //for Icons (size=32x32)
- int iconWH=32;
- int iconCount=0;
- Vector iconText =new Vector();
- Vector icons =new Vector();
- Vector iconX =new Vector();
- Vector iconY =new Vector();
- Vector iconLink =new Vector();
- //for StartBtn
- String startbtn="Start";
- int startbtnEX=0;
- Image startlogo,buffer=null;
- //for PopMenu
- Vector items=new Vector();
- Vector hrefs=new Vector();
- Dimension d=size();
- boolean pop=false;
- int maxLength=0;
- int fwidth=100;
- int itemCount=0;
- int mouseX=0,mouseY=0;
- int menuY=0,menuEY=0;
- int fheight=0;
- //code for Icons
- public void addIcon(String txt,Image img,String href,String x,String y)
- { iconText.addElement(txt);
- icons.addElement(img);
- iconLink.addElement(href);
- iconX.addElement(x);
- iconY.addElement(y);
- iconCount++;
- }
- //code for PopMenu
- public void addItem(String caption,String href)
- {
- items.addElement(caption);
- hrefs.addElement(href);
- if(caption.length()>maxLength)maxLength=caption.length();
- itemCount++;
- }
- public boolean mouseUp(Event e,int x,int y)
- { int tempY=0,i=0,ix=0,iy=0,iex=0;
- mouseX=x;
- mouseY=y;
- if(pop&&(0<=mouseX)&&(menuY<mouseY)&&(fwidth>=mouseX)&&(menuEY>mouseY))
- {
- for(tempY=menuY;tempY<menuEY;tempY+=fheight,i++)
- if((tempY+8<mouseY)&&(tempY+fheight+8>mouseY))
- {
- repaint(0,tempY+8,fwidth,tempY+fheight+8);
- showStatus("Select "+i+" is "+items.elementAt(i)+" Link To "+hrefs.elementAt(i));
- break;
- }
- do_menu(i);
- }
- else
- if((0<=mouseX)&&(d.height-fheight<mouseY)&&(startbtnEX>=mouseX)&&(d.height>mouseY))
- {pop=!pop;repaint();}
- else
- {
- repaint();
- pop=false;
- }
- if(iconCount>0)
- for(i=iconCount-1;i>=0;i--)
- {
- if(icons.elementAt(i)!=null)
- {
- ix= Integer.parseInt((String) iconX.elementAt(i))-iconWH/2;
- iex=iconWH;
- iy= Integer.parseInt((String) iconY.elementAt(i))+iconWH;
- if((ix<=mouseX)&&(iy-iconWH<=mouseY)
- &&(ix+iex>=mouseX)&&(iy+fheight-8>=mouseY))
- {do_icon(i);break;}
- }else break;
- }
- return true;
- }
- public boolean mouseMove(Event e,int x,int y)
- {
- int tempY=0;
- mouseX=x;
- mouseY=y;
- repaint();
- return true;
- }
- public void update(Graphics g){paint(g);}
- public boolean imageUpdate(Image img,int flags,int x,int y, int w,int h)
- {
- if((flags&SOMEBITS)!=0){repaint(x,y,w,h);
- } else if((flags&ABORT)!=0)
- {repaint();}
- return(flags&(ALLBITS|ABORT))==0;
- }
- public void paint(Graphics g)
- {
- Graphics screengc=null;
- screengc=g;
- g=buffer.getGraphics();
- int i=0;
- String str;
- int y=0;
- int ix=0,iy=0,iex=0;
- d=size();
- fheight= g.getFontMetrics().getHeight() + 8;
- if(wallpaper!=null) g.drawImage(wallpaper,0,0,d.width,d.height-8,this);
- g.setColor(Color.lightGray);
- g.fillRect(0,d.height-fheight,d.width,fheight+8);
- startbtnEX=g.getFontMetrics().stringWidth(startbtn)+20;
- g.fill3DRect(0,d.height-fheight,startbtnEX,fheight+8,!pop);
- if(startlogo!=null) g.drawImage(startlogo,0,d.height-fheight+2,16,fheight+8,this);
- if(!pop)g.setColor(Color.black);else g.setColor(Color.white);
- g.drawString(startbtn,17,d.height-3);
- if(iconCount>0)
- for(i=iconCount-1;i>=0;i--)
- {
- if(icons.elementAt(i)!=null)
- {
- g.setColor(Color.lightGray);
- iex= g.getFontMetrics().stringWidth((String) iconText.elementAt(i));
- ix= Integer.parseInt((String) iconX.elementAt(i))-
- iex/2;
- iy= Integer.parseInt((String) iconY.elementAt(i))+iconWH;
- if((ix<=mouseX)&&(iy-iconWH<=mouseY)
- &&(ix+iex>=mouseX)&&(iy+fheight-8>=mouseY))
- g.draw3DRect(ix-4,iy-iconWH-4,iex+8,iy+fheight,true);
- g.setColor(Color.black);
- g.fillRect(
- ix,
- iy+2,
- g.getFontMetrics().stringWidth((String) iconText.elementAt(i)),
- fheight-8
- );
- g.setColor(Color.white);
- g.drawString(
- (String) iconText.elementAt(i),
- ix,
- iy+fheight-8
- );
- g.drawImage(
- (Image)icons.elementAt(i),
- ix+iconWH/2,
- iy-iconWH,
- iconWH,iconWH,this
- );
- }
- }
- if(pop&&(itemCount>0))
- {
- y=d.height-fheight-8;
- menuEY=y;
- for(i=itemCount-1;i>=0;i--)
- {
- str=(String) items.elementAt(i);
- g.setColor(Color.lightGray);
- if((0<=mouseX)&&(y-fheight+8<=mouseY)&&(fwidth>=mouseX)&&(y+8>=mouseY))
- g.fill3DRect(0,y-fheight+8,fwidth,fheight,false);
- else
- g.fill3DRect(0,y-fheight+8,fwidth,fheight,true);
- g.setColor(Color.black);
- g.drawString(str,5,y);
- y-=fheight;
- }
- menuY=y;
- }
- screengc.drawImage(buffer,0,0,null);
- }
- //menu handler
- public void do_menu(int i)
- {
- switch(i)
- {
- case 0:
- f=new msgbox("About RDesktop",
- "Created by: Raymond Anthony Samalon"+
- "E-mail : eng60728@leonis.nus.edu.sgn"+
- "Made with : jdk 1.0,1998n"+
- "What is it: a Desktop like interface forn"+
- " website navigational purposen"+
- "Parameters:n"+
- " msg is the message of the dayn"+
- " img is the background imagen"+
- " cmd is the menu button textn"+
- " logo is the menu button imagenn"+
- " menu1 .. menuN are the menu itemsn"+
- " link1 .. linkN are the URLs nn"+
- " icon1 .. iconN are the icon image filesn"+
- " ilink1 .. ilinkN are the URLs n"+
- " ix1 .. ixN are the x-center of iconsn"+
- " iy1 .. iyN are the top-y of iconsn"+
- " itext1 .. itextN are the icons' textnn"+
- "Note :n link can be started with pop: for making messageboxn"+
- " I will be trying to allow dragging icon or a cool Mac Desktopn"+
- "Super Note:n this is a freeware but if you want to support me...n"+
- "BTW :n THANK YOU FOR TRYING RDesktop n"
- );
- f.show();
- break;
- default:
- do_link((String)hrefs.elementAt(i));
- }
- }
- // icon handler
- public void do_icon(int i)
- {
- showStatus("Select icon"+i+" is "+iconText.elementAt(i));
- do_link((String)iconLink.elementAt(i));
- }
- // link executor
- public void do_link(String link)
- {
- if(link.startsWith("pop:"))
- {
- f=new msgbox("RDesktop message",link.substring(4));
- f.show();
- }else
- {
- try{getAppletContext().showDocument(new URL(link));}
- catch(MalformedURLException er)
- {
- f=new msgbox("RDesktop ERROR","PAGE NOT FOUND:n"+link);
- f.show();
- }
- }
- }
- // INIT()
- public void init()
- {int i=0;
- setBackground(Color.cyan);
- d=size();
- buffer=createImage(d.width,d.height);
- if(getParameter("msg")!=null)
- {f=new msgbox("Message of the day..!",getParameter("msg"));f.show();}
- if(getParameter("img")!=null) wallpaper=getImage(getDocumentBase(),getParameter("img"));
- if(getParameter("logo")!=null) startlogo=getImage(getDocumentBase(),getParameter("logo"));
- if(getParameter("cmd")!=null) startbtn=getParameter("cmd");
- addItem("(C)RDesktop","about");
- for(i=1;getParameter("menu"+i)!=null;i++)
- addItem(
- getParameter("menu"+i),
- (getParameter("link"+i)!=null)?getParameter("link"+i):"none"
- );
- for(i=1;getParameter("icon"+i)!=null;i++)
- addIcon(
- (getParameter("itext"+i)!=null)?getParameter("itext"+i):"",
- getImage(getDocumentBase(),getParameter("icon"+i)),
- (getParameter("ilink"+i)!=null)?getParameter("ilink"+i):"none",
- getParameter("ix"+i),
- getParameter("iy"+i)
- );
- }
- }
- class msgbox extends Frame
- {
- msgbox(String title,String message)
- {
- super(title);
- setLayout(new BorderLayout());
- add("Center",new TextArea(message));
- add("South",new Button("OK"));
- }
- public boolean action(Event e,Object arg)
- {
- hide();
- return true;
- }
- }