📄 nervousscrolltext.java
字号:
import java.applet.Applet;
import java.applet.AudioClip;
import java.awt.*;
public class NervousScrollText extends Applet implements Runnable
{
Image offScreen ;
Graphics offScreenG ;
Thread scroller ;
final static String DEFAULT_TEXT =
"ABCDEFGHIJKLMNOPQRSTUVWXYZ+-*/" ;
String text ;
int[] letterWidth, pseudoRandom ;
int maxCharWidth, width, height,
scrollIncrement, letterPos, pos, baseY,
mouseDistortionX0, mouseDistortionX1,
randomCounter, direction, firstCharIndex,
firstCharPos, sleepingTime ;
boolean nervous, isPlaying, hasShadow ;
AudioClip nervousSound ;
Color backColor, textColor ;
Point shadowDis ;
public NervousScrollText()
{
isPlaying = false;
hasShadow = true;
shadowDis = new Point(3, 3);
}
int getParameter(String s1, int s2)
{
String s = getParameter(s1) ;
return (s != null) ? Integer.parseInt(s) : s2 ;
}
String getParameter(String s1, String s2)
{
String s = getParameter(s1) ;
return (s != null) ? s : s2 ;
}
public void init()
{
text = getParameter("message", DEFAULT_TEXT) + " " ;
backColor = new Color(Integer.parseInt(
getParameter("back_color", "000000"), 16));
textColor = new Color(Integer.parseInt(
getParameter("text_color", "00ff00"), 16));
direction = getParameter("direction", 1);
scrollIncrement = getParameter("scroll_increment", 4);
if(scrollIncrement == 0) scrollIncrement = 1 ;
if(scrollIncrement < 0)
{
direction = -direction;
scrollIncrement = -scrollIncrement;
}
sleepingTime = getParameter("sleeping_time", 100);
if(sleepingTime <= 0) sleepingTime = 100;
String s = getParameter("nervous");
if(s == null) nervous = false;
else nervous = true ;
if(nervous)
if(getParameter("audio") != null)
nervousSound = getAudioClip(getCodeBase(),
getParameter("audio"));
else nervousSound = null;
Font font = new Font(getParameter("font", "Helvetica"), 0,
getParameter("font_size", 14));
if(font != null) setFont(font);
letterWidth = getGraphics().getFontMetrics().getWidths();
maxCharWidth = 0;
for(char c = '\0'; c < 128; c++)
if(letterWidth[c] > maxCharWidth)
maxCharWidth = letterWidth[c];
pseudoRandom = new int[128];
for(int j = 0; j < 128; j++)
pseudoRandom[j] = (int)(Math.random() * 10D) - 5;
offScreen = createImage(getSize().width + maxCharWidth * 2,
getSize().height);
offScreenG = offScreen.getGraphics();
if(font != null) offScreenG.setFont(font);
int k = offScreenG.getFontMetrics().getHeight();
int l = offScreenG.getFontMetrics().getDescent();
baseY = ((getSize().height + k) - l) / 2;
width = getSize().width ;
height = getSize().height ;
if(direction == 1)
{
firstCharIndex = 0;
firstCharPos = width + maxCharWidth;
}
else
{
firstCharIndex = text.length() - 1;
firstCharPos = 0;
}
}
public void update(Graphics g)
{
int i = firstCharIndex;
int j = firstCharPos;
offScreenG.setColor(backColor);
offScreenG.fillRect(0, 0, width + 2 * maxCharWidth, height);
offScreenG.setColor(textColor);
do
{
if(direction != 1 ? j < 0 : j > width + maxCharWidth)
break;
int k = baseY, l = j;
if(j > mouseDistortionX0 && j < mouseDistortionX1)
{
l += pseudoRandom[randomCounter++];
k += pseudoRandom[randomCounter++];
randomCounter &= 0x7f;
}
char c = text.charAt(i);
offScreenG.drawString(text.substring(i, i + 1), l, k);
if(direction == 1)
{
j += letterWidth[c];
if(++i == text.length()) i = 0;
}
else
{
if(--i == -1) i = text.length() - 1;
j -= letterWidth[text.charAt(i)];
}
} while(true);
if(direction == 1)
{
firstCharPos -= scrollIncrement;
if(firstCharPos < 0)
{
firstCharPos +=
letterWidth[text.charAt(firstCharIndex)];
firstCharIndex++;
if(firstCharIndex == text.length())
firstCharIndex = 0;
}
}
else
{
firstCharPos += scrollIncrement;
if(firstCharPos > width + maxCharWidth)
{
firstCharIndex--;
if(firstCharIndex == -1)
firstCharIndex = text.length() - 1;
firstCharPos -=
letterWidth[text.charAt(firstCharIndex)];
}
}
paint(g);
}
public void paint(Graphics g)
{
g.drawImage(offScreen, -maxCharWidth, 0, null);
}
public boolean mouseMove(Event event, int i, int j)
{
if(!nervous) return false;
mouseDistortionX0 = i - maxCharWidth;
mouseDistortionX1 = i + maxCharWidth;
if(nervousSound != null && !isPlaying)
{
isPlaying = true;
nervousSound.loop();
}
return true;
}
public boolean mouseExit(Event event, int i, int j)
{
if(!nervous) return false;
mouseDistortionX0 = mouseDistortionX1 = 0;
if(nervousSound != null)
{
nervousSound.stop();
isPlaying = false;
}
return true;
}
public void stop()
{
scroller = null;
}
public void start()
{
if(scroller == null)
{
scroller = new Thread(this);
scroller.start();
}
}
public void run()
{
do
{
repaint();
try
{
Thread.sleep(sleepingTime, 0);
}
catch(InterruptedException e) { }
} while(true);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -