📄 urlslider2.java
字号:
import java.applet.Applet;
import java.applet.AppletContext;
import java.awt.*;
import java.io.PrintStream;
import java.net.MalformedURLException;
import java.net.URL;
public class UrlSlider2 extends Applet
implements Runnable
{
int cursor = 0;
Thread runner = null;
MediaTracker tracker = null;
Object frame = null;
int showNr = 0;
int totalShowNr = 0;
URL url[] = null;
String statusText[] = null;
String text[] = null;
Color textColor = null;
Color linkColor = null;
Color BGColor = null;
Image BGImage = null;
int shadowXOffset = 0;
int shadowYOffset = 0;
Color shadowColor = null;
int x = 0;
int y = 0;
int xAdd = 0;
int yAdd = 0;
int textSize = 0;
int textType = 0;
String textFont = null;
boolean mouseIn = false;
int delay = 0;
Font f = null;
FontMetrics fm = null;
Image offscreenI = null;
Graphics offscreenG = null;
int breaker = 0;
int borderSize = 0;
Color borderColor = null;
int headHeight = 0;
int yHeadText = 0;
int xHeadText = 0;
String headText = null;
int headTextSize = 0;
int headTextType = 0;
Font headTextFont = null;
FontMetrics headTextFontMetrics = null;
Color headTextColor = null;
Color headerBackgroundColor = null;
boolean statusbarAlwaysOn = false;
String defaultTarget = null;
String target[] = null;
int getParameter(String s1, int s2)
{
String s = getParameter(s1) ;
return (s != null) ? Integer.parseInt(s) : s2 ;
}
Color getParameter(String s1, Color s2)
{
String s = getParameter(s1);
return (s != null) ? new Color(Integer.parseInt(s, 16)) : s2 ;
}
String getParameter(String s1, String s2)
{
String s = getParameter(s1) ;
return (s != null) ? s : s2 ;
}
public void init()
{
totalShowNr = 0;
String tempString;
for(tempString = getParameter("text" + (totalShowNr + 1)); tempString != null; tempString = getParameter("text" + (totalShowNr + 1)))
totalShowNr++;
statusText = new String[totalShowNr + 1];
text = new String[totalShowNr + 1];
url = new URL[totalShowNr + 1];
target = new String[totalShowNr + 1];
for(int tempInt = 1; tempInt <= totalShowNr; tempInt++)
text[tempInt] = getParameter("text" + tempInt);
for(int tempInt = 1; tempInt <= totalShowNr; tempInt++)
{
tempString = getParameter("url" + tempInt);
if(!tempString.startsWith("http://") && !tempString.startsWith("mailto:") && !tempString.startsWith("file:"))
tempString = getCodeBase().toString() + tempString;
statusText[tempInt] = tempString;
}
for(int tempInt = 1; tempInt <= totalShowNr; tempInt++)
{
try
{
url[tempInt] = new URL(statusText[tempInt]);
}
catch(MalformedURLException _ex)
{
System.out.println("Bad Url = " + url[tempInt]);
}
if(url[tempInt] != null)
{
tempString = getParameter("target" + tempInt,"_self");
if(tempString != null)
target[tempInt] = tempString;
}
}
for(int tempInt = 1; tempInt <= totalShowNr; tempInt++)
{
tempString = getParameter("statustext" + tempInt);
if(tempString != null)
statusText[tempInt] = tempString;
}
textColor = getParameter("textcolor",Color.black);
BGColor = getParameter("bgcolor",Color.white);
linkColor = getParameter("linkcolor",Color.red);
shadowColor = getParameter("shadowcolor",Color.gray);
offscreenI = createImage(getSize().width, getSize().height);
offscreenG = offscreenI.getGraphics();
shadowXOffset = getParameter("shadowxoffset",0);
shadowYOffset = getParameter("shadowyoffset",0);
tempString = getParameter("bgimage");
if(tempString != null)
{
tracker = new MediaTracker(this);
BGImage = getImage(getCodeBase(), tempString);
tracker.addImage(BGImage, 0);
getAppletContext().showStatus("loading the pictures");
try
{
tracker.waitForID(0);
}
catch(InterruptedException _ex)
{
return;
}
}
else
{
BGImage = null;
}
borderColor = getParameter("bordercolor",Color.yellow);
borderSize = getParameter("bordersize",0);
textFont = getParameter("textfont","TimesRoman");
delay = getParameter("delay",20);
for(frame = this; frame != null && !(frame instanceof Frame); frame = ((Component)frame).getParent());
headHeight = getParameter("headheight",0);
if(headHeight > 0)
{
headerBackgroundColor = getParameter("headbackgroundcolor",BGColor);
offscreenG.fillRect(0, 0, getSize().width, headHeight);
tempString = getParameter("headtextsize");
if(tempString == null)
headTextSize = ((headHeight - borderSize) * 2) / 3;
else
headTextSize = Integer.parseInt(tempString);
tempString = getParameter("headtext");
if(tempString != null)
{
headText = tempString;
tempString = getParameter("headtexttype");
if(tempString == null)
{
headTextType = 1;
}
else
{
headTextType = Integer.parseInt(tempString);
if(headTextType < 0 || headTextType > 3)
headTextType = 1;
}
headTextFont = new Font(textFont, headTextType, headTextSize);
headTextFontMetrics = getFontMetrics(headTextFont);
tempString = getParameter("xheadtext");
if(tempString == null)
xHeadText = (getSize().width - headTextFontMetrics.stringWidth(headText)) / 2;
else
xHeadText = Integer.parseInt(tempString);
tempString = getParameter("yheadtext");
if(tempString == null)
yHeadText = (((headHeight - borderSize) / 2 + (headTextFontMetrics.getAscent() - headTextFontMetrics.getDescent()) / 2) - headTextFontMetrics.getLeading()) + borderSize;
else
yHeadText = Integer.parseInt(tempString);
headTextColor = getParameter("headtextcolor",Color.white);
offscreenG.setColor(headTextColor);
offscreenG.setFont(headTextFont);
offscreenG.drawString(headText, xHeadText, yHeadText);
}
}
tempString = getParameter("textsize");
if(tempString == null)
textSize = ((getSize().height - borderSize - headHeight) * 2) / 3;
else
textSize = Integer.parseInt(tempString);
if(textSize == 0)
textSize = ((getSize().height - 2 * borderSize) * 2) / 3;
tempString = getParameter("texttype");
if(tempString == null)
textType = 1;
else
textType = Integer.parseInt(tempString);
if(textType < 0 || textType > 3)
textType = 1;
switch(textType)
{
case 0: // '\0'
f = new Font(textFont, 0, textSize);
break;
case 1: // '\001'
f = new Font(textFont, 1, textSize);
break;
case 2: // '\002'
f = new Font(textFont, 2, textSize);
break;
case 3: // '\003'
f = new Font(textFont, 3, textSize);
break;
}
fm = getFontMetrics(f);
tempString = getParameter("xadd");
if(tempString == null)
xAdd = 0;
else
xAdd = Integer.parseInt(tempString);
tempString = getParameter("yadd");
if(tempString == null)
yAdd = -1;
else
yAdd = Integer.parseInt(tempString);
showNr = 1;
calculateX(showNr);
calculateY(showNr);
if(BGImage == null)
{
offscreenG.setColor(BGColor);
offscreenG.fillRect(borderSize, headHeight, getSize().width - 2 * borderSize, getSize().height - borderSize - headHeight);
}
else
{
offscreenG.drawImage(BGImage, borderSize, headHeight, getSize().width - 2 * borderSize, getSize().height - headHeight, this);
}
if(borderSize != 0)
{
offscreenG.setColor(borderColor);
for(int temp = 0; temp < borderSize; temp++)
offscreenG.drawRect(temp, temp, getSize().width - 2 * temp - 1, getSize().height - 2 * temp - 1);
}
repaint();
y = (((getSize().height - borderSize - headHeight) / 2 + (fm.getAscent() - fm.getDescent()) / 2) - fm.getLeading()) + headHeight;
tempString = getParameter("y");
if(tempString != null)
y = Integer.parseInt(tempString);
offscreenG.setFont(f);
if(headHeight == 0)
offscreenG.clipRect(borderSize, borderSize, getSize().width - 2 * borderSize, getSize().height - 2 * borderSize);
else
offscreenG.clipRect(borderSize, headHeight, getSize().width - 2 * borderSize, getSize().height - borderSize - headHeight);
tempString = getParameter("statusbaralwayson");
if(tempString != null)
{
int tempInt = Integer.parseInt(tempString);
if(tempInt == 1)
statusbarAlwaysOn = true;
}
}
public void start()
{
if(runner == null)
{
runner = new Thread(this);
runner.start();
}
}
public void stop()
{
if(runner != null)
{
runner.stop();
runner = null;
}
}
public boolean handleEvent(Event evt)
{
switch(evt.id)
{
case 504: // Event.MOUSE_EVENT
mouseIn = true;
((Frame)frame).setCursor(14);
System.out.println(1);
System.out.println(0);
System.out.println(11);
System.out.println(12);
System.out.println(13);
System.out.println(7);
System.out.println(6);
System.out.println(8);
System.out.println(5);
System.out.println(10);
System.out.println(4);
System.out.println(9);
System.out.println(2);
System.out.println(3);
System.out.println(10);
cursor++;
getAppletContext().showStatus(statusText[showNr]);
return true;
case 505: // Event.MOUSE_EXIT
mouseIn = false;
((Frame)frame).setCursor(14);
if(!statusbarAlwaysOn)
getAppletContext().showStatus(" ");
return true;
case 501: // Event.MOUSE_DOWN
if(mouseIn = true)
if(target[showNr] == null)
getAppletContext().showDocument(url[showNr]);
else
getAppletContext().showDocument(url[showNr], target[showNr]);
return true;
}
return false;
}
public void run()
{
for(; Thread.currentThread() == runner; textMove())
{
if(BGImage == null)
{
offscreenG.setColor(BGColor);
offscreenG.fillRect(borderSize, headHeight, getSize().width - 2 * borderSize, getSize().height - borderSize - headHeight);
}
else
{
offscreenG.drawImage(BGImage, borderSize, headHeight, this);
}
offscreenG.setColor(shadowColor);
offscreenG.drawString(text[showNr], x + shadowXOffset, y + shadowYOffset);
if(mouseIn)
offscreenG.setColor(linkColor);
else
offscreenG.setColor(textColor);
offscreenG.drawString(text[showNr], x, y);
breaker = 0;
repaint();
pause(delay);
breaker = 1;
}
}
public void update(Graphics g)
{
if(mouseIn || statusbarAlwaysOn)
getAppletContext().showStatus(statusText[showNr]);
if(breaker == 0)
g.drawImage(offscreenI, 0, 0, this);
}
public void paint(Graphics g)
{
update(g);
}
public void calculateX(int nr)
{
if(xAdd > 0)
x = -fm.stringWidth(text[nr] + borderSize);
if(xAdd < 0)
x = getSize().width - borderSize;
if(xAdd == 0)
x = (getSize().width - fm.stringWidth(text[nr])) / 2;
}
public void calculateY(int nr)
{
if(yAdd > 0)
y = -fm.getDescent() + headHeight;
if(yAdd < 0)
y = (getSize().height + fm.getAscent()) - borderSize;
}
public void textMove()
{
boolean outOffApplet = false;
x += xAdd;
y += yAdd;
if(x > getSize().width && xAdd > 0)
outOffApplet = true;
if(x + fm.stringWidth(text[showNr]) < 0 && xAdd < 0)
outOffApplet = true;
if(y < -fm.getDescent() + headHeight && yAdd < 0)
outOffApplet = true;
if(y > getSize().height + fm.getAscent() && yAdd > 0)
outOffApplet = true;
if(outOffApplet)
nextLine();
}
public void nextLine()
{
showNr++;
if(showNr > totalShowNr)
showNr = 1;
calculateX(showNr);
calculateY(showNr);
}
public void pause(int sleeper)
{
try
{
Thread.sleep(sleeper);
return;
}
catch(InterruptedException _ex)
{
return;
}
}
public UrlSlider2()
{
cursor = -1;
statusbarAlwaysOn = false;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -