*) simplified banner creation code

pull/1/head
Marc Nause 12 years ago
parent cd0b5f31b4
commit 8fb1b1e290

@ -73,7 +73,8 @@ public class Banner {
env.getConfig( env.getConfig(
SwitchboardConstants.NETWORK_NAME, SwitchboardConstants.NETWORK_NAME,
"unspecified").toUpperCase(); "unspecified").toUpperCase();
// the '+ 1': the own peer is not included in sizeConnected()
// the '+ 1': the own peer is not included in sizeConnected()
final int peers = sb.peers.sizeConnected() + 1; final int peers = sb.peers.sizeConnected() + 1;
long nlinks = sb.peers.countActiveURL(); long nlinks = sb.peers.countActiveURL();
long nwords = sb.peers.countActiveRWI(); long nwords = sb.peers.countActiveRWI();
@ -117,7 +118,7 @@ public class Banner {
nqph, nppm); nqph, nppm);
if (!net.yacy.peers.graphics.Banner.logoIsLoaded()) { if (!net.yacy.peers.graphics.Banner.logoIsLoaded()) {
// do not write a cache to disc; keep in RAM // do not write a cache to disc; keep in RAM
ImageIO.setUseCache(false); ImageIO.setUseCache(false);
final BufferedImage logo = ImageIO.read(new File(pathToImage)); final BufferedImage logo = ImageIO.read(new File(pathToImage));
return net.yacy.peers.graphics.Banner.getBannerPicture( return net.yacy.peers.graphics.Banner.getBannerPicture(

@ -3,15 +3,29 @@ package net.yacy.peers.graphics;
import java.awt.image.BufferedImage; import java.awt.image.BufferedImage;
import java.text.DecimalFormat; import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols; import java.text.DecimalFormatSymbols;
import java.text.NumberFormat;
import net.yacy.visualization.PrintTool; import net.yacy.visualization.PrintTool;
import net.yacy.visualization.RasterPlotter; import net.yacy.visualization.RasterPlotter;
/** /**
* Creates banner which displays dtat about peer and network. * Creates banner which displays data about peer and network.
*/ */
public final class Banner { public final class Banner {
private static final String QUERIES_HOUR = " QUERIES/HOUR";
private static final String PAGES_MINUTE = " PAGES/MINUTE";
private static final String QUERIES = "QUERIES:";
private static final String WORDS = "WORDS:";
private static final String LINKS = "LINKS:";
private static final String NETWORK = "NETWORK:";
private static final String SPEED = "SPEED:";
private static final String TYPE = "TYPE:";
private static final String DHT_WORDS = "DHT WORDS:";
private static final String DOCUMENTS = "DOCUMENTS:";
private static final String PEER_NAME = "PEER:";
private static final int EXPR_LEN = 26;
/** Private constructor to avoid instantiation of utility class. */ /** Private constructor to avoid instantiation of utility class. */
private Banner() { } private Banner() { }
@ -19,149 +33,107 @@ public final class Banner {
* banner text is always in English. * banner text is always in English.
*/ */
private static final DecimalFormatSymbols DFS = private static final DecimalFormatSymbols DFS =
new DecimalFormatSymbols(); new DecimalFormatSymbols();
static { static {
DFS.setDecimalSeparator('.'); DFS.setDecimalSeparator('.');
DFS.setGroupingSeparator(',');
} }
private static final NumberFormat QPM_FORMAT = new DecimalFormat("0.00", DFS);
private static final NumberFormat LARGE_NUMBER_FORMAT = new DecimalFormat("#,###", DFS);
private static RasterPlotter bannerPicture = null; private static RasterPlotter bannerPicture = null;
private static BufferedImage logo = null; private static BufferedImage logo = null;
private static long bannerPictureDate = 0; private static long bannerPictureDate = 0;
/** /**
* Creates new banner image if max age has been reached, * Creates new banner image if max age has been reached, else returns cached version.
* else returns cached version.
* @param data data to display * @param data data to display
* @param maxAge age in ms since 01.01.1970 * @param maxAge age in ms since 01.01.1970
* @return banner image * @return banner image
*/ */
public static RasterPlotter getBannerPicture( public static RasterPlotter getBannerPicture( final BannerData data, final long maxAge) {
final BannerData data, final long maxAge) {
if ((bannerPicture == null) if (bannerPicture == null || bannerOutdated(maxAge)) {
|| ((System.currentTimeMillis() - bannerPictureDate) > maxAge)) {
drawBannerPicture(data, logo); drawBannerPicture(data, logo);
} }
return bannerPicture; return bannerPicture;
} }
/** /**
* Creates new banner image if max age has been reached, * Creates new banner image if max age has been reached, else returns cached version.
* else returns cached version.
* @param data data to display * @param data data to display
* @param maxAge age in ms since 01.01.1970 * @param maxAge age in ms since 01.01.1970
* @param newLogo logo to display * @param newLogo logo to display
* @return banner image * @return banner image
*/ */
public static RasterPlotter getBannerPicture( public static RasterPlotter getBannerPicture(
final BannerData data, final BannerData data,
final long maxAge, final long maxAge,
final BufferedImage newLogo) { final BufferedImage newLogo) {
if ((bannerPicture == null) if (bannerPicture == null || bannerOutdated(maxAge)) {
|| ((System.currentTimeMillis() - bannerPictureDate) > maxAge)) {
drawBannerPicture(data, newLogo); drawBannerPicture(data, newLogo);
} }
return bannerPicture; return bannerPicture;
} }
private static void drawBannerPicture( private static boolean bannerOutdated(final long maxAge) {
final BannerData data, final BufferedImage newLogo) { return (System.currentTimeMillis() - bannerPictureDate) > maxAge;
}
private static void drawBannerPicture(final BannerData data, final BufferedImage newLogo) {
final int exprlength = 16;
logo = newLogo; logo = newLogo;
bannerPicture = bannerPicture =
new RasterPlotter( new RasterPlotter(
data.getWidth(), data.getWidth(),
data.getHeight(), data.getHeight(),
RasterPlotter.DrawMode.MODE_REPLACE, RasterPlotter.DrawMode.MODE_REPLACE,
data.getBgcolor()); data.getBgcolor());
// draw description
bannerPicture.setColor(data.getTextcolor());
PrintTool.print(bannerPicture, 100, 12, 0, PEER_NAME + addBlanks(data.getName(), PEER_NAME.length()), -1);
PrintTool.print(bannerPicture, 100, 22, 0, DOCUMENTS + addBlanksAndDots(data.getLinks(), DOCUMENTS.length()), -1);
PrintTool.print(bannerPicture, 100, 32, 0, DHT_WORDS + addBlanksAndDots(data.getWords(), DHT_WORDS.length()), -1);
PrintTool.print(bannerPicture, 100, 42, 0, TYPE + addBlanks(data.getType(), TYPE.length()), -1);
PrintTool.print(bannerPicture, 100, 52, 0, SPEED + addBlanks(data.getPpm() + PAGES_MINUTE, SPEED.length()), -1);
PrintTool.print(bannerPicture, 290, 12, 0, NETWORK + addBlanks(data.getNetwork() + " [" + data.getPeers() + "]", NETWORK.length()), -1);
PrintTool.print(bannerPicture, 290, 22, 0, LINKS + addBlanksAndDots(data.getNlinks(), LINKS.length()), -1);
PrintTool.print(bannerPicture, 290, 32, 0, WORDS + addBlanksAndDots(data.getNwords(), WORDS.length()), -1);
PrintTool.print(bannerPicture, 290, 42, 0, QUERIES + addBlanks(formatQpm(data.getNqph()) + QUERIES_HOUR, QUERIES.length()), -1);
PrintTool.print(bannerPicture, 290, 52, 0, SPEED + addBlanks(data.getNppm() + PAGES_MINUTE, SPEED.length()), -1);
final int height = data.getHeight(); final int height = data.getHeight();
final int width = data.getWidth(); final int width = data.getWidth();
// draw description
bannerPicture.setColor(Long.parseLong(data.getTextcolor(), 16));
PrintTool.print(bannerPicture, 100, 12, 0, "PEER NAME:" + addTrailingBlanks(data.getName(), exprlength), -1);
PrintTool.print(bannerPicture, 100, 22, 0, "DOCUMENTS:" + addBlanksAndDots(data.getLinks(), exprlength), -1);
PrintTool.print(bannerPicture, 100, 32, 0, "DHT WORDS:" + addBlanksAndDots(data.getWords(), exprlength), -1);
PrintTool.print(bannerPicture, 100, 42, 0, "TYPE: " + addTrailingBlanks(data.getType(), exprlength), -1);
PrintTool.print(bannerPicture, 100, 52, 0, "SPEED: " + addTrailingBlanks(data.getPpm() + " PAGES/MINUTE", exprlength), -1);
PrintTool.print(bannerPicture, 290, 12, 0, "NETWORK: " + addTrailingBlanks(data.getNetwork() + " [" + data.getPeers() + "]", exprlength), -1);
PrintTool.print(bannerPicture, 290, 22, 0, "LINKS: " + addBlanksAndDots(data.getNlinks(), exprlength), -1);
PrintTool.print(bannerPicture, 290, 32, 0, "WORDS: " + addBlanksAndDots(data.getNwords(), exprlength), -1);
PrintTool.print(bannerPicture, 290, 42, 0, "QUERIES: " + addTrailingBlanks(formatQpm(data.getNqph()) + " QUERIES/HOUR", exprlength), -1);
PrintTool.print(bannerPicture, 290, 52, 0, "SPEED: " + addTrailingBlanks(data.getNppm() + " PAGES/MINUTE", exprlength), -1);
if (logo != null) { if (logo != null) {
final int x = (100 / 2 - logo.getWidth() / 2); final int x = (100 / 2 - logo.getWidth() / 2);
final int y = (height / 2 - logo.getHeight() / 2); final int y = (height / 2 - logo.getHeight() / 2);
bannerPicture.insertBitmap(logo, x, y, 0, 0, RasterPlotter.FilterMode.FILTER_ANTIALIASING); bannerPicture.insertBitmap(logo, x, y, 0, 0, RasterPlotter.FilterMode.FILTER_ANTIALIASING);
} }
final String bordercolor = data.getBordercolor(); bannerPicture.setColor(data.getBordercolor());
if (bordercolor != null && !bordercolor.isEmpty()) { bannerPicture.line(0, 0, 0, height - 1, 100);
bannerPicture.setColor(Long.parseLong(bordercolor, 16)); bannerPicture.line(0, 0, width - 1, 0, 100);
bannerPicture.line(0, 0, 0, height - 1, 100); bannerPicture.line(width - 1, 0, width - 1, height - 1, 100);
bannerPicture.line(0, 0, width - 1, 0, 100); bannerPicture.line(0, height - 1, width - 1, height - 1, 100);
bannerPicture.line(width - 1, 0, width - 1, height - 1, 100);
bannerPicture.line(0, height - 1, width - 1, height - 1, 100);
}
// set timestamp // set timestamp
bannerPictureDate = System.currentTimeMillis(); bannerPictureDate = System.currentTimeMillis();
} }
private static String addBlanksAndDots(final long input, final int length) { private static String addBlanksAndDots(final long input, final int length) {
return addBlanksAndDots(Long.toString(input), length); return addBlanks(LARGE_NUMBER_FORMAT.format(input), length);
} }
private static String addBlanksAndDots(String input, final int length) { private static String addBlanks(final String word, final int offset) {
input = addDots(input); return String.format(String.format("%%%ds", (EXPR_LEN - offset)), word);
input = addTrailingBlanks(input,length);
return input;
}
private static String addDots(String word) {
String tmp = "";
int len = word.length();
if (len > 3) {
while (len > 3) {
if (tmp.equals("")) {
tmp = word.substring(len - 3,len);
} else {
tmp = word.substring(len - 3,len) + "." + tmp;
}
word = word.substring(0,len - 3);
len = word.length();
}
word = word + "." + tmp;
}
return word;
}
private static String addTrailingBlanks(String word, int length) {
if (length > word.length()) {
String blanks = "";
length = length - word.length();
int i = 0;
while (i++ < length) {
blanks += " ";
}
word = blanks + word;
}
return word;
} }
private static String formatQpm(final double qpm) { private static String formatQpm(final double qpm) {
final String ret; return (qpm < 10) ? QPM_FORMAT.format(qpm) : Long.toString(Math.round(qpm));
if (qpm < 10) {
ret = new DecimalFormat("0.00", DFS).format(qpm);
} else {
ret = Long.toString(Math.round(qpm));
}
return ret;
} }
/** /**
@ -169,10 +141,7 @@ public final class Banner {
* @return true if logo has been set, else false * @return true if logo has been set, else false
*/ */
public static boolean logoIsLoaded() { public static boolean logoIsLoaded() {
if (logo == null) { return logo != null;
return false;
}
return true;
} }
} }

@ -4,9 +4,9 @@ public class BannerData {
private final int width; private final int width;
private final int height; private final int height;
private final String bgcolor; private final long bgcolor;
private final String textcolor; private final long textcolor;
private final String bordercolor; private final long bordercolor;
private final String name; private final String name;
private final long links; private final long links;
private final long words; private final long words;
@ -39,9 +39,13 @@ public class BannerData {
) { ) {
this.width = width; this.width = width;
this.height = height; this.height = height;
this.bgcolor = bgcolor; try {
this.textcolor = textcolor; this.bgcolor = Long.parseLong(bgcolor, 16);
this.bordercolor = bordercolor; this.textcolor = Long.parseLong(textcolor, 16);
this.bordercolor = Long.parseLong(bordercolor, 16);
} catch (final NumberFormatException ex) {
throw new IllegalArgumentException("Invalid color definition.", ex);
}
this.name = name; this.name = name;
this.links = links; this.links = links;
this.words = words; this.words = words;
@ -63,15 +67,15 @@ public class BannerData {
return height; return height;
} }
public final String getBgcolor() { public final long getBgcolor() {
return bgcolor; return bgcolor;
} }
public final String getTextcolor() { public final long getTextcolor() {
return textcolor; return textcolor;
} }
public final String getBordercolor() { public final long getBordercolor() {
return bordercolor; return bordercolor;
} }

Loading…
Cancel
Save