Документ взят из кэша поисковой машины. Адрес оригинального документа : http://hea-www.harvard.edu/~dburke/aas218/tweetanim/Tweet.pde
Дата изменения: Sat Jun 4 15:01:16 2011
Дата индексирования: Tue Oct 2 07:42:40 2012
Кодировка:

Поисковые слова: http astrokuban.info astrokuban
/*
* Simple class for displaying a Tweet.
*/

static HashMap colorMap = new HashMap();

static int nearly = 0;
static int nlate = 0;
static int ntweets = 0;
static int maxNameWidth = 0;

static final int twXpos = 10;
static final int rtXpos = 25;

static final int spacing = 20;

int get_spacing() {
return spacing;
}

int get_nearly() {
return nearly;
}

int get_nlate() {
return nlate;
}

/* return a random color */
int get_random_color() {
colorMode(HSB, 255, 255, 255, 1);
return color(random(20,255), random(20,255), random(50,255));
}

/*
* Gets the display color for the user name.
*/
int get_user_color(String userName, boolean reTweet) {

// do we need a new color?
colorMode(HSB, 255, 255, 255, 1);
int uc;

if (colorMap.containsKey(userName)) {
uc = (Integer) colorMap.get(userName);
} else {
uc = get_random_color();
colorMap.put(userName, uc);
}

// Change the color of re-tweets (dim them a bit)
if (reTweet) {
float h = hue(uc);
float s = saturation(uc);
float b = brightness(uc);
return color(h, s, b-40);
} else {
return uc;
}
}

/*
* Create a new set of randomly-selected user colors.
*/
void recreate_user_colors() {
Iterator iter = colorMap.keySet().iterator();

while (iter.hasNext()) {
colorMap.put(iter.next(), get_random_color());
}

}

class Tweet {

int type; // -1 for early, 1 for late and 0 for during
String contents;
String userName;
int time;
boolean isReTweet;

// int contentsColor;
// int userNameColor;

int posY;
int contentsX;
int userNameX;

Tweet(String input) {

String[] tokens = split(input, "\t");
if (tokens.length != 3) {
println("Error: unable to parse <" + input + ">");
return;
}

contents = tokens[2];
userName = tokens[1];
time = Integer.parseInt(tokens[0]) - time0;

if (time < timeStart) {
time = nearly;
nearly ++;
type = -1;
} else if (time >= timeEnd) {
time = nlate;
nlate ++;
type = 1;
} else {
posY = timeToPixel(time);
type = 0;
}

userNameX = nameBorder / 2;
contentsX = nameBorder;

int tmin = (int) textWidth(contents);
if (tmin > maxNameWidth) {
maxNameWidth = tmin;
}

isReTweet = contents.startsWith("RT");
if (isReTweet) {
contentsX += rtXpos;
} else {
contentsX += twXpos;
}

// contentsColor = uc;
// userNameColor = get_user_color(userName, isRT);

}

void adjust() {
// for early/late tweets time is just a numeric counter used to
// indicate an offset from the boundary.
if (type < 0) {
posY = timeToPixel(timeStart) + spacing * (nearly - time) + spacing; // originally included a 50 in the offset
} else if (type > 0) {
posY = timeToPixel(timeEnd) - spacing * time - 2 * spacing;
} else {
posY = timeToPixel(time);
}
}

/* Draw the tweet using the given color (the username uses the assigned value) */
void display(int col) {
if (posY < 0 || posY > maxHeight) {
return;
}

textFont(twFont, fontHeight);
fill(col);
textAlign(LEFT);
text(contents, contentsX, posY);

textAlign(CENTER);
fill(get_user_color(userName, isReTweet));
text(userName, userNameX, posY);
}

}