一起学习
import java.applet.Applet;
import java.applet.AppletContext;
import java.awt.*;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.*;
public class basictoc2 extends Applet
implements Runnable
{
public void init()
{
String s5 = "";
String s6 = "";
String s7 = "";
String s8 = "";
String s9 = "";
String s10 = "";
setBackground(Color.white);
String s;
int i = (s = getParameter("BGCOLOR")) == null ? Integer.parseInt("ffffff", 16) : Integer.parseInt(s, 16);
bgimgfile = (s = getParameter("BGIMAGE")) == null ? "" : s;
if(bgimgfile.length() == 0)
{
setBackground(new Color(i));
} else
{
bgimg = getImage(getCodeBase(), bgimgfile);
mt.addImage(bgimg, 1);
}
String s12 = (s = getParameter("ITEMHEIGHT")) == null ? "25" : s;
if(s12.indexOf('|') < 0)
gifh = Integer.parseInt(s12);
setBackground(new Color(i));
String s1 = (s = getParameter("AUTHORINFO")) == null ? "" : s;
isimgstrip = (s = getParameter("isIMAGESTRIP")) == null ? "yes" : s;
if(isimgstrip.equals("no"))
{
txtcolor = (s = getParameter("TEXTCOLOR")) == null ? Integer.parseInt("aa6666", 16) : Integer.parseInt(s, 16);
symbolcolor = (s = getParameter("SYMBOLCOLOR")) == null ? Integer.parseInt("aa6666", 16) : Integer.parseInt(s, 16);
highlightcolor = (s = getParameter("HIGHLIGHTCOLOR")) == null ? Integer.parseInt("000088", 16) : Integer.parseInt(s, 16);
s5 = (s = getParameter("FONT1")) == null ? "" : s;
s6 = (s = getParameter("FONT2")) == null ? "" : s;
spacing = (s = getParameter("SPACING1")) == null ? 4 : Integer.parseInt(s);
spacing2 = (s = getParameter("SPACING2")) == null ? 2 : Integer.parseInt(s);
}
if(s5.length() > 0)
{
sg = new StringTokenizer(s5, ";");
try
{
while(sg.hasMoreTokens())
{
s7 = sg.nextToken().trim();
s9 = sg.nextToken().trim();
font1psize = Integer.parseInt(sg.nextToken().trim());
}
}
catch(NoSuchElementException _ex) { }
if(s9.equals("bold"))
fm = new Font(s7, 1, font1psize);
else
if(s9.equals("italic"))
fm = new Font(s7, 2, font1psize);
else
if(s9.equals("plain"))
fm = new Font(s7, 0, font1psize);
else
fm = new Font(s7, 3, font1psize);
}
if(s6.length() > 0)
{
sg = new StringTokenizer(s6, ";");
try
{
while(sg.hasMoreTokens())
{
s8 = sg.nextToken().trim();
s10 = sg.nextToken().trim();
font2psize = Integer.parseInt(sg.nextToken().trim());
}
}
catch(NoSuchElementException _ex) { }
if(s10.equals("bold"))
fm2 = new Font(s8, 1, font2psize);
else
if(s10.equals("italic"))
fm2 = new Font(s8, 2, font2psize);
else
if(s10.equals("plain"))
fm2 = new Font(s8, 0, font2psize);
else
fm2 = new Font(s8, 3, font2psize);
}
String s4;
if(s1.indexOf("Nalla Senthilnathan") >= 0 && s1.indexOf("nrsenthil.tripod.com") >= 0)
s4 = (s = getParameter("TOCDATA")) == null ? "" : s;
else
s4 = "";
String s11 = (s = getParameter("CURRENTDOC")) == null ? "" : s;
byte byte0 = -1;
String s2 = "";
st = new StringTokenizer(s4, "|");
try
{
while(st.hasMoreTokens())
{
st2 = new StringTokenizer(st.nextToken(), ";");
try
{
while(st2.hasMoreTokens())
{
elems.addElement(new basictoc2Rec());
((basictoc2Rec)elems.elementAt(elemcount)).level = Integer.parseInt(st2.nextToken().trim());
String s3 = st2.nextToken().trim();
((basictoc2Rec)elems.elementAt(elemcount)).helpstr = s3;
if(isimgstrip.equals("yes"))
{
((basictoc2Rec)elems.elementAt(elemcount)).altstr = st2.nextToken().trim();
((basictoc2Rec)elems.elementAt(elemcount)).itemh = gifh;
} else
if(s3.indexOf(',') > 0)
{
st3 = new StringTokenizer(s3, ",");
try
{
while(st3.hasMoreTokens())
{
((basictoc2Rec)elems.elementAt(elemcount)).helpstr = st3.nextToken().trim();
((basictoc2Rec)elems.elementAt(elemcount)).altstr = st3.nextToken().trim();
}
}
catch(NoSuchElementException _ex) { }
if(((basictoc2Rec)elems.elementAt(elemcount)).level == 1)
((basictoc2Rec)elems.elementAt(elemcount)).itemh = 2 * spacing 2 * font1psize spacing2;
else
((basictoc2Rec)elems.elementAt(elemcount)).itemh = 2 * spacing 2 * font2psize spacing2;
} else
if(((basictoc2Rec)elems.elementAt(elemcount)).level == 1)
((basictoc2Rec)elems.elementAt(elemcount)).itemh = font1psize spacing * 2;
else
((basictoc2Rec)elems.elementAt(elemcount)).itemh = font2psize spacing * 2;
((basictoc2Rec)elems.elementAt(elemcount)).target = st2.nextToken().trim();
if(((basictoc2Rec)elems.elementAt(elemcount)).level == 1)
{
((basictoc2Rec)elems.elementAt(elemcount)).ishead = true;
((basictoc2Rec)elems.elementAt(elemcount)).isopen = false;
((basictoc2Rec)elems.elementAt(elemcount)).isvisible = true;
int j = elemcount;
} else
{
((basictoc2Rec)elems.elementAt(elemcount)).ishead = false;
((basictoc2Rec)elems.elementAt(elemcount)).isopen = false;
((basictoc2Rec)elems.elementAt(elemcount)).isvisible = false;
}
s3 = st2.nextToken().trim();
if(s3.length() == 1)
{
((basictoc2Rec)elems.elementAt(elemcount)).urlstr = "";
elemcount ;
} else
{
((basictoc2Rec)elems.elementAt(elemcount)).urlstr = s3;
elemcount ;
}
break;
}
}
catch(NoSuchElementException _ex) { }
}
}
catch(NoSuchElementException _ex) { }
try
{
for(int k = 0; k < elemcount; k )
if(((basictoc2Rec)elems.elementAt(k)).urlstr.length() > 0)
urls.addElement(new URL(getDocumentBase(), ((basictoc2Rec)elems.elementAt(k)).urlstr));
else
urls.addElement(null);
}
catch(MalformedURLException _ex) { }
int l1 = 0;
if(isimgstrip.equals("yes"))
{
for(int l = 0; l < elemcount; l )
{
((basictoc2Rec)elems.elementAt(l)).defimg = getImage(getCodeBase(), ((basictoc2Rec)elems.elementAt(l)).helpstr);
mt.addImage(((basictoc2Rec)elems.elementAt(l)).defimg, l1);
l1 ;
((basictoc2Rec)elems.elementAt(l)).altimg = getImage(getCodeBase(), ((basictoc2Rec)elems.elementAt(l)).altstr);
mt.addImage(((basictoc2Rec)elems.elementAt(l)).altimg, l1);
l1 ;
}
if(s12.indexOf('|') > 0)
{
st3 = new StringTokenizer(s12, "|");
for(int i1 = 0; i1 < elemcount; i1 )
try
{
if(st3.hasMoreTokens())
((basictoc2Rec)elems.elementAt(i1)).itemh = Integer.parseInt(st3.nextToken());
}
catch(NoSuchElementException _ex) { }
} else
{
for(int j1 = 0; j1 < elemcount; j1 )
((basictoc2Rec)elems.elementAt(j1)).itemh = gifh;
}
}
if(s11.length() > 0)
{
for(int k1 = 0; k1 < elemcount; k1 )
if(((basictoc2Rec)elems.elementAt(k1)).ishead)
{
if(((basictoc2Rec)elems.elementAt(k1)).urlstr.equals(s11))
{
docidx = k1;
return;
}
for(int i2 = k1 1; i2 < elemcount; i2 )
{
if(((basictoc2Rec)elems.elementAt(i2)).ishead)
break;
if(!((basictoc2Rec)elems.elementAt(i2)).urlstr.equals(s11))
continue;
docidx = i2;
((basictoc2Rec)elems.elementAt(k1)).isopen = true;
openAllTails(k1);
break;
}
}
}
}
public void update(Graphics g)
{
Dimension dimension = size();
if(offscreen == null || dimension.width != offscreensize.width || dimension.height != offscreensize.height)
{
offscreen = createImage(dimension.width, dimension.height);
offscreensize = dimension;
offgraphics = offscreen.getGraphics();
}
offgraphics.setColor(getBackground());
offgraphics.fillRect(0, 0, dimension.width, dimension.height);
paint(offgraphics);
g.drawImage(offscreen, 0, 0, null);
}
public synchronized boolean mouseDown(Event event, int i, int j)
{
if(event.id == 501 && event.clickCount == 1)
{
int k = 0;
int l = -1;
int i1 = 0;
boolean flag = false;
for(; i1 < elemcount; i1 )
{
if(!((basictoc2Rec)elems.elementAt(i1)).isvisible)
continue;
int j1 = ((basictoc2Rec)elems.elementAt(i1)).itemh;
if(event.y >= k && event.y <= k j1)
{
if(((basictoc2Rec)elems.elementAt(i1)).urlstr.length() > 0)
{
getAppletContext().showDocument((URL)urls.elementAt(i1), ((basictoc2Rec)elems.elementAt(i1)).target);
docidx = i1;
if(((basictoc2Rec)elems.elementAt(i1)).ishead)
closeAllTails2();
return true;
}
if(((basictoc2Rec)elems.elementAt(i1)).ishead)
{
l = i1;
break;
}
}
k = j1;
}
i1 = 0;
if(l >= 0)
for(; i1 < elemcount; i1 )
if(((basictoc2Rec)elems.elementAt(i1)).ishead)
if(i1 == l)
{
if(((basictoc2Rec)elems.elementAt(i1)).isopen)
{
((basictoc2Rec)elems.elementAt(i1)).isopen = false;
closeAllTails(i1);
} else
{
((basictoc2Rec)elems.elementAt(i1)).isopen = true;
openAllTails(i1);
}
} else
if(((basictoc2Rec)elems.elementAt(i1)).isopen)
{
((basictoc2Rec)elems.elementAt(i1)).isopen = false;
closeAllTails(i1);
}
return true;
} else
{
return false;
}
}
public void closeAllTails2()
{
for(int i = 0; i < elemcount; i )
if(((basictoc2Rec)elems.elementAt(i)).ishead && ((basictoc2Rec)elems.elementAt(i)).isopen)
{
((basictoc2Rec)elems.elementAt(i)).isopen = false;
closeAllTails(i);
}
}
public void closeAllTails(int i)
{
for(int j = i 1; j < elemcount; j )
{
if(((basictoc2Rec)elems.elementAt(j)).ishead)
break;
((basictoc2Rec)elems.elementAt(j)).isvisible = false;
((basictoc2Rec)elems.elementAt(j)).isopen = false;
}
}
public void openAllTails(int i)
{
for(int j = i 1; j < elemcount; j )
{
if(((basictoc2Rec)elems.elementAt(j)).ishead)
break;
((basictoc2Rec)elems.elementAt(j)).isvisible = true;
((basictoc2Rec)elems.elementAt(j)).isopen = true;
}
}
public boolean handleEvent(Event event)
{
if(event.id == 505)
mousemoved = 0;
return super.handleEvent(event);
}
public void paint(Graphics g)
{
int k = 0;
if(mousemoved == 1 && urlmsg >= 0)
showStatus(((basictoc2Rec)elems.elementAt(urlmsg)).urlstr);
openmsg = 0;
closemsg = 0;
urlmsg = -1;
if(isimgstrip.equals("yes"))
if(mt.checkAll(true))
{
for(int i = 0; i < elemcount; i )
if(((basictoc2Rec)elems.elementAt(i)).isvisible)
{
int i1 = ((basictoc2Rec)elems.elementAt(i)).itemh;
if(mousemoved == 1 && mousey >= k && mousey <= k i1)
{
g.drawImage(((basictoc2Rec)elems.elementAt(i)).altimg, 0, k, this);
urlmsg = i;
} else
if(docidx == i || ((basictoc2Rec)elems.elementAt(i)).ishead && ((basictoc2Rec)elems.elementAt(i)).isopen)
g.drawImage(((basictoc2Rec)elems.elementAt(i)).altimg, 0, k, this);
else
g.drawImage(((basictoc2Rec)elems.elementAt(i)).defimg, 0, k, this);
k = i1;
}
return;
} else
{
g.setColor(Color.black);
g.drawString("Loading TOC...,", bounds().width / 5, bounds().height / 2);
return;
}
if(bgimgfile.length() > 0 && mt.checkAll(true))
g.drawImage(bgimg, 0, 0, this);
for(int j = 0; j < elemcount; j )
if(((basictoc2Rec)elems.elementAt(j)).isvisible)
{
int j1 = ((basictoc2Rec)elems.elementAt(j)).itemh;
if(mousemoved == 1 && mousey >= k && mousey <= k j1)
urlmsg = j;
int l;
int k1;
if(((basictoc2Rec)elems.elementAt(j)).ishead)
{
g.setFont(fm);
l = k spacing font1psize;
k1 = font1psize;
} else
{
g.setFont(fm2);
l = k spacing font2psize;
k1 = font2psize;
}
g.setColor(new Color(symbolcolor));
if(((basictoc2Rec)elems.elementAt(j)).ishead)
if(((basictoc2Rec)elems.elementAt(j)).urlstr.length() > 0)
{
if(mousemoved == 1 && mousey >= k && mousey <= k j1 || docidx == j)
g.fillRect(10, l - (int)((double)font1psize * 0.80000000000000004D), (int)((double)font1psize * 0.5D), (int)((double)font1psize * 0.80000000000000004D));
g.drawRect(10, l - (int)((double)font1psize * 0.80000000000000004D), (int)((double)font1psize * 0.5D), (int)((double)font1psize * 0.80000000000000004D));
} else
if(((basictoc2Rec)elems.elementAt(j)).isopen)
{
drawFillOpentria(g, l);
drawOpentria(g, l);
} else
{
if(mousemoved == 1 && mousey >= k && mousey <= k j1)
drawFillClosetria(g, l);
drawClosetria(g, l);
}
if(mousemoved == 1 && mousey >= k && mousey <= k j1 || docidx == j)
g.setColor(new Color(highlightcolor));
else
g.setColor(new Color(txtcolor));
g.drawString(((basictoc2Rec)elems.elementAt(j)).helpstr, 25, l);
k = l spacing;
if(((basictoc2Rec)elems.elementAt(j)).altstr.length() > 0)
{
k = l spacing2;
g.drawString(((basictoc2Rec)elems.elementAt(j)).altstr, 25, k k1);
k = k k1 spacing;
}
}
}
public void drawClosetria(Graphics g, int i)
{
int ai[] = new int[4];
int ai1[] = new int[4];
gifh = (font1psize * 3) / 2;
ai[0] = 10;
ai[1] = 10;
ai[2] = 10 (int)((double)font1psize * 0.59999999999999998D);
ai[3] = 10;
ai1[0] = i - (int)((double)font1psize * 0.90000000000000002D);
ai1[1] = i;
ai1[2] = i - (int)(((double)font1psize * 0.90000000000000002D) / 2D);
ai1[3] = i - (int)((double)font1psize * 0.90000000000000002D);
g.drawPolygon(ai, ai1, 4);
}
public void drawOpentria(Graphics g, int i)
{
int ai[] = new int[4];
int ai1[] = new int[4];
ai[0] = 10;
ai[1] = 10 (int)((double)font1psize * 0.69999999999999996D);
ai[2] = 10 (int)(((double)font1psize * 0.69999999999999996D) / 2D);
ai[3] = 10;
ai1[0] = i - (int)((double)font1psize * 0.69999999999999996D);
ai1[1] = i - (int)((double)font1psize * 0.69999999999999996D);
ai1[2] = i;
ai1[3] = i - (int)((double)font1psize * 0.69999999999999996D);
g.drawPolygon(ai, ai1, 4);
}
public void drawFillClosetria(Graphics g, int i)
{
int ai[] = new int[4];
int ai1[] = new int[4];
gifh = (font1psize * 3) / 2;
ai[0] = 10;
ai[1] = 10;
ai[2] = 10 (int)((double)font1psize * 0.59999999999999998D);
ai[3] = 10;
ai1[0] = i - (int)((double)font1psize * 0.90000000000000002D);
ai1[1] = i;
ai1[2] = i - (int)(((double)font1psize * 0.90000000000000002D) / 2D);
ai1[3] = i - (int)((double)font1psize * 0.90000000000000002D);
g.fillPolygon(ai, ai1, 4);
}
public void drawFillOpentria(Graphics g, int i)
{
int ai[] = new int[4];
int ai1[] = new int[4];
ai[0] = 10;
ai[1] = 10 (int)((double)font1psize * 0.69999999999999996D);
ai[2] = 10 (int)(((double)font1psize * 0.69999999999999996D) / 2D);
ai[3] = 10;
ai1[0] = i - (int)((double)font1psize * 0.69999999999999996D);
ai1[1] = i - (int)((double)font1psize * 0.69999999999999996D);
ai1[2] = i;
ai1[3] = i - (int)((double)font1psize * 0.69999999999999996D);
g.fillPolygon(ai, ai1, 4);
}
public void start()
{
thread = new Thread(this);
thread.start();
}
public void run()
{
do
{
try
{
Thread.currentThread();
Thread.sleep(100L);
}
catch(InterruptedException _ex) { }
repaint();
} while(true);
}
public synchronized boolean mouseMove(Event event, int i, int j)
{
mousemoved = 1;
mousex = i;
mousey = j;
return true;
}
public void stop()
{
if(thread != null)
{
thread.stop();
thread = null;
}
}
public basictoc2()
{
buf = new char[1];
ch = new Character('\251');
fm3 = new Font("Helvectica", 0, 8);
fm = new Font("Helvectica", 0, 14);
fm2 = new Font("Helvectica", 1, 14);
basex = 35;
elems = new Vector(50, 10);
urls = new Vector(50, 10);
selhead = "";
curhead = "";
urlmsg = -1;
msg1 = "";
msg2 = "";
mt = new MediaTracker(this);
isopenfound = false;
docidx = -1;
font1psize = 12;
font2psize = 12;
spacing = 4;
spacing2 = 2;
}
static final int MAXLINES = 200;
char buf[];
Character ch;
int layoutcount;
int urlcount;
String tok0;
String tok1;
String tok2;
String tok3;
int elemcount;
int txtcolor;
int highlightcolor;
int symbolcolor;
int tailcount;
Thread thread;
Font fm3;
Font fm;
Font fm2;
int basex;
int basey;
int prev_x;
int prev_y;
int mousemoved;
int mousex;
int mousey;
Image offscreen;
Dimension offscreensize;
Graphics offgraphics;
Vector elems;
Vector urls;
String selhead;
String curhead;
int headisopen;
int gifw;
int gifh;
int openmsg;
int closemsg;
int urlmsg;
String msg1;
String msg2;
String txtfile;
String isimgstrip;
String bgimgfile;
MediaTracker mt;
boolean isopenfound;
int docidx;
StringTokenizer st;
StringTokenizer st2;
StringTokenizer st3;
StringTokenizer sg;
int fontpointsize;
Image bgimg;
int font1psize;
int font2psize;
int spacing;
int spacing2;
}
import java.awt.Image;
class basictoc2Rec
{
public basictoc2Rec()
{
id = "";
name = "";
helpstr = "";
urlstr = "";
head = "";
altstr = "";
target = "";
level = 0;
itemh = 0;
}
public basictoc2Rec(String s, String s1, String s2)
{
helpstr = s;
urlstr = s1;
altstr = s2;
}
void setxny(int i, int j)
{
elemx = i;
elemy = j;
}
public String id;
public String name;
public String helpstr;
public String urlstr;
public String head;
public String altstr;
public String target;
public Image defimg;
public Image altimg;
public Image img;
public int elemx;
public int elemy;
public int level;
public int itemh;
public boolean isopen;
public boolean ishead;
public boolean isvisible;
}
下载本文示例代码
如何用java实现下拉菜单如何用java实现下拉菜单如何用java实现下拉菜单如何用java实现下拉菜单如何用java实现下拉菜单如何用java实现下拉菜单如何用java实现下拉菜单如何用java实现下拉菜单如何用java实现下拉菜单如何用java实现下拉菜单如何用java实现下拉菜单如何用java实现下拉菜单