Here's the "model" portion (text manipulation routines) of a ticker:
public class Ticker
{
//////////////////////
// Global constants //
//////////////////////
private static final int characterDelay = 130;
private static final int correctionDelay = 350;
private static final int blinkDelay = 400;
private static final int blinkMax = 6*3600*1000/blinkDelay; // 6
hours
/////////////////////////
// Class instance vars //
/////////////////////////
private int delay = characterDelay;
private int currentStory = 0;
private int blinkCount = 0;
private int letterCount = 0;
private int last = -1;
private boolean erase = false;
private int numErrs = 0;
private int beg = -1;
private int end = -1;
private String correction = null;
private int direction = 1;
private int offset = 0;
private String stories[] =
{
null
};
/*
private String stories[] =
{
null,
"What's the matter? Cat got your tongue?",
"Are you deaf or somethin'?",
"What are you waitin' for? Christmas?",
"Hey! Like I said..."
};
*/
private int storyCount = stories.length;
private String story = null;
private String ticker = null;
//////////////////
// Constructors //
//////////////////
public Ticker()
{
}
public Ticker(final String msg)
{
setFirstStory(msg);
}
////////////////////////////
// Clear ticker variables //
////////////////////////////
public void clearTheTicker()
{
blinkCount = 0;
letterCount = 0;
last = -1;
story = null;
}
///////////////////////////////
// Clear all erase variables //
///////////////////////////////
public void turnOffEraseMode()
{
erase = false;
numErrs = 0;
beg = -1;
end = -1;
correction = null;
direction = 1;
offset = 0;
}
///////////////////////////////
// Advance to the next story //
///////////////////////////////
public void nextStory()
{
currentStory++;
currentStory = currentStory % storyCount;
story = stories[currentStory];
blinkCount = blinkMax;
setDelay(characterDelay);
}
///////////////////////////////
// Advance ticker one letter //
///////////////////////////////
public String tick()
{
String text = null;
if (letterCount == 0)
{
// Clear the ticker
text = null;
// Turn off erase mode
turnOffEraseMode();
// Advance to next story
nextStory();
}
// Load the current story text into the ticker
last = story.length();
text = story.substring(0,letterCount) + getWidget(letterCount, last,
blinkCount);
if (letterCount != last)
{
if (direction > 0)
setDelay(characterDelay);
else
setDelay(correctionDelay);
// Check for mispelling
if (story.charAt(letterCount) == '/')
{
// Find # of previously typed incorrect letters (numErrs)
numErrs = Character.digit(story.charAt(letterCount + 1), 10);
// Find beginning and end of the correction
beg = letterCount + 2;
end = story.indexOf('/', beg);
// Extract the correction
correction = story.substring(beg, end);
// Go into erase mode
erase = true;
// Switch letter count direction into reverse
direction = -1;
offset = 0;
}
if (erase) offset++;
letterCount += direction;
if (erase && offset == numErrs)
{
// Done correcting, switch back to forward direction
direction = 1;
// Create new corrected string
story = story.substring(0,beg-2-numErrs) + correction +
story.substring(end+1);
if (blinkCount > 0)
blinkCount--;
else
letterCount = 0;
}
return text;
}
////////////////////
// Run the ticker //
////////////////////
public void runTheTicker()
{
// Video game loop
while (true)
{
ticker = tick();
System.out.println(letterCount + ": " + ticker);
}
}
/////////////////
// Odd number? //
/////////////////
public boolean isOdd(final int n)
{
if ((n & 1) == 1)
return true;
else
return false;
}
///////////////////////
// Set typing cursor //
///////////////////////
public String getWidget(final int letter, final int lastLetter, final
int blinks)
{
final String widgetNone = "";
final String widgetOne = "_";
final String widgetTwo = "-";
if (letter != lastLetter)
{
if (isOdd(letter))
return widgetOne;
else
return widgetTwo;
}
else
{
if (isOdd(blinks))
return widgetOne;
else
return widgetNone;
}
}
//////////////////////////////
// Set animation frame rate //
//////////////////////////////
public void setDelay(final int d)
{
delay = d;
}
//////////////////////////////
// Get animation frame rate //
//////////////////////////////
public int getDelay()
{
return delay;
}
////////////////////////////
// Load the primary story //
////////////////////////////
public void setFirstStory(final String firstStory)
{
stories[0] = firstStory;
}
//////////
// main //
//////////
public static void main(final String args[])
{
String tickerMsg = null;
if (args.length > 0)
tickerMsg = args[0];
else
tickerMsg = "I've thunk/3ought/ about it and the question /9answer/
is 42.";
final Ticker t = new Ticker(tickerMsg);
t.clearTheTicker();
t.runTheTicker();
}
}
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Java-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/java-dev/email@hidden