Commit d029ce1e authored by Drahomír Karchňák's avatar Drahomír Karchňák

Added date and time values to the exported image.

parent a41c54a7
......@@ -9,7 +9,6 @@ import java.io.IOException;
import java.io.InputStream;
import java.text.FieldPosition;
import java.text.Format;
import java.util.Date;
import java.util.Locale;
import javax.imageio.ImageIO;
......@@ -43,7 +42,8 @@ public abstract class AbstractImageGenerator {
String typ,
String signal,
String os,
Date date
String formattedTime,
String formattedDate
) throws IOException;
protected BufferedImage loadTemplateImage(String name) throws IOException {
......
......@@ -7,9 +7,6 @@ import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
/**
* Created by bcx on 1/30/18.
......@@ -28,7 +25,7 @@ public class ForumBannerGenerator extends AbstractImageGenerator {
private Font secondaryFont = null;
@Override
public BufferedImage generateImage(String lang, double upload, double download, double ping, String isp, String typ, String signal, String os, Date time) throws IOException {
public BufferedImage generateImage(String lang, double upload, double download, double ping, String isp, String typ, String signal, String os, String formattedTime, String formattedDate) throws IOException {
//Initial setup of final image
BufferedImage finalImage = new BufferedImage(BANNER_WIDTH, BANNER_HEIGHT, BufferedImage.TYPE_INT_RGB);
Graphics2D finalImageGraphics = finalImage.createGraphics();
......@@ -41,14 +38,14 @@ public class ForumBannerGenerator extends AbstractImageGenerator {
renderMainValue(formatNumber(upload, lang), 280, 132, finalImageGraphics);
renderMainValue(formatNumber(ping, lang), 469, 132, finalImageGraphics);
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd-MM-yyyy", Locale.ENGLISH);
renderAdditionalValue(isp, 151, 246, finalImageGraphics);
// renderAdditionalValue(typ, 151, 278, finalImageGraphics);
renderAdditionalValue(simpleDateFormat.format(time), 151, 278, finalImageGraphics); //TODO
renderAdditionalValue(typ, 151, 278, finalImageGraphics);
renderAdditionalValue(signal, 432, 246, finalImageGraphics);
renderAdditionalValue(os, 432, 278, finalImageGraphics);
renderTimeDateValue(formattedTime, 550, 16, finalImageGraphics);
renderTimeDateValue(formattedDate, 550, 40, finalImageGraphics);
return finalImage;
}
......@@ -92,4 +89,19 @@ public class ForumBannerGenerator extends AbstractImageGenerator {
graphics.drawString(value, posX, posY + height/2);
}
private void renderTimeDateValue(String value, int posX, int posY, Graphics2D graphics) {
if(value == null || value.isEmpty()) value = "n/a";
graphics.setColor(new Color(22, 47, 103));
Font font = secondaryFont.deriveFont(20f);
graphics.setFont(font);
FontMetrics fontMetrics = graphics.getFontMetrics(font);
int width = fontMetrics.stringWidth(value);
int height = fontMetrics.getHeight();
graphics.drawString(value, posX - width, posY + height/2);
}
}
......@@ -27,6 +27,7 @@ import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.sql.Types;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
......@@ -48,6 +49,38 @@ import at.alladin.rmbt.statisticServer.ServerResource;
*/
public class ImageExport extends ServerResource {
enum TimeDateFormatting {
en("en", "dd-MM-yyyy", "HH:mm"),
de("de", "dd.MM.yyyy", "HH:mm"),
cs("cs", "dd.MM.yyyy", "HH:mm");
private static final TimeDateFormatting FALLBACK = en;
private final String lang;
private final String date;
private final String time;
TimeDateFormatting(String lang, String date, String time) {
this.lang = lang;
this.date = date;
this.time = time;
}
public static String getDateFormat(String lang) {
for(TimeDateFormatting formatting : values()) {
if(lang.equals(formatting.lang)) return formatting.date;
}
return FALLBACK.date;
}
public static String getTimeFormat(String lang) {
for(TimeDateFormatting formatting : values()) {
if(lang.equals(formatting.lang)) return formatting.time;
}
return FALLBACK.time;
}
}
@Get
public Representation retrieve(final String entity) {
if (!getRequest().getAttributes().containsKey("lang") || !getRequest().getAttributes().containsKey("size")) {
......@@ -141,9 +174,11 @@ public class ImageExport extends ServerResource {
}
Date date = new Date(time.getTime());
BufferedImage img = generator.generateImage(lang, upload, download, ping, isp, typ, signal, os, time);
SimpleDateFormat dateFormat = new SimpleDateFormat(TimeDateFormatting.getDateFormat(lang));
SimpleDateFormat timeFormat = new SimpleDateFormat(TimeDateFormatting.getTimeFormat(lang));
BufferedImage img = generator.generateImage(lang, upload, download, ping, isp, typ, signal, os, timeFormat.format(time), dateFormat.format(date));
ImageIO.write(img, "png", out);
}
......@@ -168,7 +203,7 @@ public class ImageExport extends ServerResource {
public class ForumBannerSmallGenerator extends AbstractImageGenerator {
@Override
public BufferedImage generateImage(String lang, double upload, double download, double ping, String isp, String typ, String signal, String os, Date time) throws IOException {
public BufferedImage generateImage(String lang, double upload, double download, double ping, String isp, String typ, String signal, String os, String formattedTime, String formattedDate) throws IOException {
String unknownString = "";
if (lang.equals("cs")) {
......@@ -253,7 +288,7 @@ public class ImageExport extends ServerResource {
public class FacebookThumbnailGenerator extends AbstractImageGenerator {
@Override
public BufferedImage generateImage(String lang, double upload, double download, double ping, String isp, String typ, String signal, String os, Date time) throws IOException {
public BufferedImage generateImage(String lang, double upload, double download, double ping, String isp, String typ, String signal, String os, String formattedTime, String formattedDate) throws IOException {
BufferedImage img = new BufferedImage(100, 100, BufferedImage.TYPE_INT_ARGB);
img.createGraphics();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment