📄 funscroll.java
字号:
/*
* Copyright (c) 1995 by Jan Andersson, Torpa Konsult AB.
*
* Permission to use, copy, and distribute this software for
* NON-COMMERCIAL purposes and without fee is hereby granted
* provided that this copyright notice appears in all copies.
*/
import java.applet.*;
import java.awt.*;
import java.awt.image.*;
import java.util.*;
/**
* FunScroll - A Funnier (?) scrolling text applet.
*
* @version 1.9 96/02/10
* @author Jan Andersson (janne@torpa.se)
*
*/
public class FunScroll extends Applet implements Runnable
{
Thread thread = null; // animation thread
boolean suspended = false; // animation thread suspended
int threadDelay = 100; // animation thread delay
Vector animatedTexts = null; // animated texts
int noOfTexts = 0; // number of texts
int currentTextIndex = 0; // current text index
FunScrollAnimatedText currentText; // current text instance
int lastWidth = 0; // last know width
int lastHeight = 0; // last know height
static boolean debug = false;// debug flag
// updates done "off-screen"...
private Image osImage; // ... image
private Graphics osGraphics; // ... graphics
private Dimension osSize; // ... size
/**
* Init applet
*/
public void init()
{
String lines[] = initLineParameters();
// get color parameters
Color fg = readColor(getParameter("fgColor"), Color.black);
Color bg = readColor(getParameter("bgColor"), getBackground());
setBackground(bg);
setForeground(fg);
// get font parameters
String fontName = getParameter("font");
if (fontName == null)
fontName = "TimesRoman";
String fontSize = getParameter("size");
if (fontSize == null)
fontSize = "22";
int size = Integer.valueOf(fontSize).intValue();
Font font = new Font(fontName, Font.BOLD, size);
// get animation thread delay time
String par = getParameter("delay");
if (par != null)
threadDelay = Integer.valueOf(par).intValue();
// get dx/dy movement
int dx = 3;
int dy = 2;
par = getParameter("dx");
if (par != null)
dx = Integer.valueOf(par).intValue();
par = getParameter("dy");
if (par != null)
dy = Integer.valueOf(par).intValue();
// get delimiters string
String delim = getParameter("delim");
// create animated texts
createAnimatedTexts(lines, font, fg, bg, dx, dy, delim);
}
/**
* Init unparsed line parameters
*/
protected String[] initLineParameters()
{
String par;
// get unparsed line parameters
dbg("get line parameters...");
String lines[] = new String[10];
for (int i=0; i<10; i++) {
String parName = "line" + i;
par = getParameter(parName);
lines[i] = par;
dbg(" " + parName + ":" + par);
}
if (lines[0] == null) {
// assume no line parameter provided; use default
lines[0] = "<20>" + getAppletInfo();
lines[1] = "<Text can be displayed as normal text";
lines[2] = "<nervous><20>or as this - nervous";
lines[3] = "<sine-wave><25>or as this - sine-wave";
lines[4] = "<up>We can scroll up, down, left and right<up>";
lines[5] = "We can start like this, and move down<down>";
lines[6] = "<up>We can also use more than one line\\nlike this.<up>";
lines[7] = "<down><20>Another feature -" +
"\"exploding\" chacters...<explode>";
lines[8] = "<left><10><sine-wave>Fun? I think so..." +
" at least a little bit.";
}
return lines;
}
/**
* Gets a parameter of the applet.
*
* Use this function to overload java.applet.Applet.getParameter
* to handle ISO Latin 1 characters correctly in Netscape 2.0.
* Following a suggestion from Peter Sylvester,
* Peter.Sylvester@edelweb.fr.
*
* Note: this is a work-a-round for a bug in Netscape and should
* be removed!
*/
public String getParameter(String s) {
String tmp = super.getParameter(s);
if (tmp == null)
return null;
char ec[] = tmp.toCharArray();
for (int i=0; i < ec.length; i++) {
if (ec[i] >= 0xff00)
ec[i] &= 0x00ff ;
}
return(new String(ec)) ;
}
/**
* Applet Info.
*/
public String getAppletInfo() {
return "FunScroll.java 1.9 96/02/10, by janne@torpa.se.";
}
/**
* Parameter Info.
*/
public String[][] getParameterInfo() {
// More should be added...
String[][] info = {
{"line0", "string", "Message line 0" },
{"linen", "string", "Message line <n>" },
{"line9", "string", "Message line 9" },
{"delim", "string", "Delimiter string (<>)" },
{"font", "string", "Message font (TimesRoman)" },
{"size", "int", "Message font size (22)" },
{"delay", "int", "Animation delay time in millisec. (100)" },
{"dx", "int",
"No of pixels to move horizontally for each animation (2)" },
{"dy", "int",
"No of pixels to move vertically for each animation (1)" },
{"fgColor", "hex", "Foreground Color" },
{"bgColor", "hex", "Background Color" },
};
return info;
}
/**
* Convert a Hexadecimal String with RGB-Values to Color
* Uses aDefault, if no or not enough RGB-Values
*/
public Color readColor(String aColor, Color aDefault) {
if ((aColor == null) ||
(aColor.charAt(0) != '#') ||
(aColor.length() != 7 )) {
return aDefault;
}
try {
Integer rgbValue = new Integer(0);
rgbValue = Integer.valueOf(aColor.substring(1, 7), 16);
return new Color(rgbValue.intValue());
}
catch (Exception e) {
return aDefault;
}
}
/**
* Create animated texts
*/
public void createAnimatedTexts(String lines[], Font font,
Color fg, Color bg,
int dx, int dy,
String delim)
{
noOfTexts = 0;
animatedTexts = new Vector(10);
dbg("Creating Animated Text...");
for (int i=0; i<10; i++) {
if (lines[i] == null)
break;
dbg(" " + lines[i]);
animatedTexts.addElement(
new FunScrollAnimatedText(
this, lines[i], font, fg, bg, dx, dy, delim));
noOfTexts++;
}
currentTextIndex = 0;
currentText = (FunScrollAnimatedText)
animatedTexts.elementAt(currentTextIndex);
}
/**
* Animate the texts
*/
public void animate(Graphics g) {
// update current text
if (currentText.update(g)) {
// done; get next text
currentTextIndex++;
if (currentTextIndex >= noOfTexts)
currentTextIndex = 0;
currentText = (FunScrollAnimatedText)
animatedTexts.elementAt(currentTextIndex);
currentText.reset(lastWidth, lastHeight);
}
}
/**
* Paint the graphics
*/
public void paint(Graphics g) {
if (lastWidth != size().width || lastHeight != size().height) {
lastWidth = size().width;
lastHeight = size().height;
// reset Animated Text item
currentText.reset(lastWidth, lastHeight);
}
animate(g);
}
/**
* Update off-screen graphics
*
* From NoFlickerApplet, by Matthew Gray
*/
public final synchronized void update (Graphics theG)
{
Dimension d = size();
if((osImage == null) || (d.width != osSize.width) ||
(d.height != osSize.height)) {
osImage = createImage(d.width, d.height);
osSize = d;
osGraphics = osImage.getGraphics();
osGraphics.setFont(getFont());
}
osGraphics.setColor(getBackground());
osGraphics.fillRect(0,0,d.width, d.height);
paint(osGraphics);
theG.drawImage(osImage, 0, 0, null);
}
/**
* Run the loop. This method is called by class Thread.
*/
public void run() {
Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
while (thread != null) {
repaint();
try {Thread.sleep(threadDelay);}
catch (InterruptedException e){}
}
}
/**
* Start the applet by forking an animation thread.
*/
public void start() {
if (thread == null) {
thread = new Thread(this);
thread.start();
}
}
/**
* Stop the applet. The thread will exit because run() exits.
*/
public void stop() {
thread = null;
}
/**
* Suspend/Resume
*/
public boolean mouseDown(Event evt, int x, int y) {
// handle Suspend/Resume
if (suspended) {
thread.resume();
}
else {
thread.suspend();
}
suspended = !suspended;
// show version
if (suspended)
showStatus(getAppletInfo());
return true;
}
/**
* Simple debug...
*/
static public void dbg(String str) {
if (debug) {
System.out.println("Debug: " + str);
System.out.flush();
}
}
}
/**
* Attributes of FunScroll Animated Text
*/
class FunScrollTextAttr
{
// scroll styles:
static final int NONE = 0; // no scrolling (default)
static final int LEFT = 1; // scroll left
static final int RIGHT = 2; // ... right
static final int UP = 3; // ... up
static final int DOWN = 4; // ... down
static final int EXPLODE = 5;// explode (only for endScroll)
// text styles:
static final int NORMAL = 0; // normal (default)
static final int NERVOUS = 1; // "nervous" text
static final int SINEWAVE = 2; // sine-wave text
String msg = ""; // message line
String delimiters = "<>"; // used delimiters (default is "<>")
int startScroll = NONE; // start scroll style
int endScroll = NONE; // end scroll style
int showDelay = 10; // start delay
int endDelay = -1; // end delay
int style = NORMAL; // text style
public FunScrollTextAttr(String line, String delim)
{
if (delim != null) {
// used specified delimiter
delimiters = delim;
}
parse(line);
}
public String msg()
{
return msg;
}
public int startScroll()
{
return startScroll;
}
public int endScroll()
{
return endScroll;
}
public int showDelay()
{
return showDelay;
}
public int endDelay()
{
return endDelay;
}
public int style()
{
return style;
}
void parse(String line)
{
StringTokenizer st = new StringTokenizer(line, delimiters);
boolean gotText = false;
while (st.hasMoreTokens()) {
int scroll = -1;
String token = st.nextToken();
// parse scroll style
if (token.equalsIgnoreCase("left"))
scroll = LEFT;
else if (token.equalsIgnoreCase("right"))
scroll = RIGHT;
else if (token.equalsIgnoreCase("up"))
scroll = UP;
else if (token.equalsIgnoreCase("down"))
scroll = DOWN;
else if (gotText && token.equalsIgnoreCase("explode"))
scroll = EXPLODE;
if (scroll >= 0) {
if (!gotText)
startScroll = scroll;
else
endScroll = scroll;
continue;
}
// parse text style
if (token.equalsIgnoreCase("nervous")) {
style = NERVOUS;
continue;
}
if (token.equalsIgnoreCase("sine-wave")) {
style = SINEWAVE;
continue;
}
// check if integer, if so assume delay value
boolean isInt = true;
for (int i=0; i<token.length(); i++) {
int digit = Character.digit(token.charAt(i), 10);
if (digit < 0) {
// not a digit
isInt = false;
break;
}
}
if (isInt) {
try {
if (!gotText)
showDelay = Integer.parseInt(token);
else
endDelay = Integer.parseInt(token);
} catch (NumberFormatException ne) {}
continue;
}
else {
// assume text string parsed
if (!gotText) {
msg = token;
gotText = true;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -