Added hourly forecast and message forecast
This commit is contained in:
@@ -0,0 +1,175 @@
|
||||
package com.flaremicro.visualforecast.displays;
|
||||
|
||||
import java.awt.Color;
|
||||
import java.awt.Font;
|
||||
import java.awt.FontMetrics;
|
||||
import java.awt.Graphics2D;
|
||||
import java.awt.Rectangle;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.util.ArrayList;
|
||||
|
||||
import com.flaremicro.visualforecast.RenderPanel;
|
||||
import com.flaremicro.visualforecast.api.ForecastProvider;
|
||||
import com.flaremicro.visualforecast.graphics.DrawingUtil;
|
||||
import com.flaremicro.visualforecast.graphics.FontManager;
|
||||
|
||||
import static com.flaremicro.visualforecast.graphics.RenderConstants.*;
|
||||
|
||||
public class CurrentForecastDisplay implements Display {
|
||||
|
||||
private String[] lines = new String[0];
|
||||
private Font font;
|
||||
|
||||
private int stringOffset = 0;
|
||||
private int ticksBeforeChange = 0;
|
||||
|
||||
public CurrentForecastDisplay() {
|
||||
font = FontManager.getInstance().getOrCreateFont(Font.TRUETYPE_FONT, this.getClass().getResource("/Star4000.ttf")).deriveFont(30F);
|
||||
|
||||
/*
|
||||
int w2 = g2d.getFontMetrics().stringWidth("EXTREME WEATHER ADVISORY");
|
||||
drawOutlinedString(g2d, (W >> 1) - (w2 >> 1), TOPBAR_HEIGHT + 48, "EXTREME WEATHER ADVISORY", Color.RED, Color.BLACK, 2);
|
||||
|
||||
g2d.setFont(font.deriveFont(30F));
|
||||
for (int i = 0; i < testString.length; i++)
|
||||
{
|
||||
drawOutlinedString(g2d, 90, TOPBAR_HEIGHT + 78 + 25 * i, testString[i], Color.WHITE, Color.BLACK, 1);
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
@Override
|
||||
public void tick(RenderPanel renderer, long ticks, int iconTicks) {
|
||||
ticksBeforeChange--;
|
||||
if (ticksBeforeChange <= 0)
|
||||
{
|
||||
stringOffset += 9;
|
||||
if (stringOffset < lines.length)
|
||||
{
|
||||
ticksBeforeChange = 200;
|
||||
renderer.requestExclusiveBoundedRepaint();
|
||||
}
|
||||
else
|
||||
{
|
||||
renderer.nextDisplay();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void initDisplay(RenderPanel renderer, ForecastProvider forecastProvider, long ticks, int iconTicks) {
|
||||
ticksBeforeChange = 200;
|
||||
stringOffset = 0;
|
||||
String test = "*IMPORTANT NOTICE*\n*THIS BETA IS UNFINISHED*\n\nThis is a Beta version of the VisualForeast 1000! Many things are not finished, and only the hourly and 7 day forecasts are ready. There will be a lot more to come!\nPlease note that there are some issues with data collection from Environment Canada resulting in missing or potentially incorrect information around midnight hours. This will be fixed as the project continues and the disclaimer will be removed.";
|
||||
ArrayList<String> linesList = new ArrayList<String>();
|
||||
|
||||
renderer.setCurrentForecast("Welcome to the");
|
||||
renderer.setCurrentTown("VisualForecast 1000");
|
||||
|
||||
BufferedImage disposableImage = new BufferedImage(1, 1, BufferedImage.TYPE_BYTE_GRAY);
|
||||
Graphics2D g = disposableImage.createGraphics();
|
||||
FontMetrics f = g.getFontMetrics(font);
|
||||
String[] lines = test.split("\n");
|
||||
for (int i = 0; i < lines.length; i++)
|
||||
{
|
||||
splitText(lines[i], W - 150 - STROKE_WIDTH, f, linesList);
|
||||
//if(words[i])
|
||||
}
|
||||
|
||||
g.dispose();
|
||||
disposableImage.flush();
|
||||
|
||||
redrawRegionlost(renderer);
|
||||
|
||||
this.lines = linesList.toArray(new String[0]);
|
||||
}
|
||||
|
||||
private void splitText(String text, int maxWidth, FontMetrics fontMetrics, ArrayList<String> lineList) {
|
||||
|
||||
text = text.trim();
|
||||
//this.text.add(text);
|
||||
if (fontMetrics.stringWidth(text) <= maxWidth)
|
||||
lineList.add(text);
|
||||
else
|
||||
{
|
||||
while (text.length() > 0)
|
||||
{
|
||||
int idx = binSearch(text, fontMetrics, maxWidth, 1, text.length());
|
||||
if (idx <= 0)
|
||||
break;
|
||||
String texPart = text.substring(0, idx).trim();
|
||||
if (fontMetrics.stringWidth(text.substring(0, Math.min(text.length(), idx + 1))) > maxWidth && texPart.contains(" "))
|
||||
{
|
||||
idx = texPart.lastIndexOf(" ");
|
||||
texPart = texPart.substring(0, idx);
|
||||
}
|
||||
lineList.add(texPart);
|
||||
text = text.substring(idx).trim();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static int binSearch(String string, FontMetrics fontMetrics, int key, int low, int high) {
|
||||
int index = 0;
|
||||
|
||||
while (low <= high)
|
||||
{
|
||||
int mid = low + ((high - low) / 2);
|
||||
int midmetric = fontMetrics.stringWidth(string.substring(0, mid));
|
||||
if (midmetric < key)
|
||||
{
|
||||
low = mid + 1;
|
||||
index = mid;
|
||||
}
|
||||
else if (midmetric > key)
|
||||
{
|
||||
high = mid - 1;
|
||||
}
|
||||
else if (midmetric == key)
|
||||
{
|
||||
index = mid;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return index;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void drawDisplay(RenderPanel renderer, Graphics2D g2d, long ticks, int iconTicks) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void drawBoundLimitedDisplay(RenderPanel renderer, Graphics2D g2d, Rectangle bounds, long ticks, int iconTicks) {
|
||||
DrawingUtil.drawGradientRect(g2d, 60, TOPBAR_HEIGHT, W - 120, MAINBAR_HEIGHT, 20, BG_BLUE.darker(), BG_BLUE.brighter());
|
||||
g2d.setFont(font);
|
||||
FontMetrics fontMetrics = g2d.getFontMetrics();
|
||||
g2d.setColor(BG_BLUE.darker());
|
||||
g2d.drawRect(60 + STROKE_OFFSET, TOPBAR_HEIGHT + STROKE_OFFSET, W - 120 - STROKE_WIDTH, MAINBAR_HEIGHT - STROKE_WIDTH);
|
||||
for (int i = 0; i < Math.min(9, lines.length - this.stringOffset); i++)
|
||||
{
|
||||
if (lines[i + stringOffset].startsWith("*") && lines[i].endsWith("*"))
|
||||
{
|
||||
String line = lines[i + stringOffset].substring(1, lines[i + stringOffset].length() - 1);
|
||||
DrawingUtil.drawOutlinedString(g2d, 60 + 20 + STROKE_OFFSET, TOPBAR_HEIGHT + STROKE_OFFSET + 40 + i * 30, "*", Color.WHITE, Color.BLACK, 2);
|
||||
DrawingUtil.drawOutlinedString(g2d, 60 + 20 + STROKE_OFFSET + (W - 150 - STROKE_WIDTH) / 2 - fontMetrics.stringWidth(line) / 2, TOPBAR_HEIGHT + STROKE_OFFSET + 40 + i * 30, line, Color.WHITE, Color.BLACK, 2);
|
||||
DrawingUtil.drawOutlinedString(g2d, W - 95 - STROKE_WIDTH, TOPBAR_HEIGHT + STROKE_OFFSET + 40 + i * 30, "*", Color.WHITE, Color.BLACK, 2);
|
||||
}
|
||||
else if (lines[i + stringOffset].startsWith("[") && lines[i].endsWith("]"))
|
||||
{
|
||||
String line = lines[i + stringOffset].substring(1, lines[i + stringOffset].length() - 1);
|
||||
DrawingUtil.drawOutlinedString(g2d, 60 + 20 + STROKE_OFFSET + (W - 150 - STROKE_WIDTH) / 2 - fontMetrics.stringWidth(line) / 2, TOPBAR_HEIGHT + STROKE_OFFSET + 40 + i * 30, line, Color.WHITE, Color.BLACK, 2);
|
||||
}
|
||||
else DrawingUtil.drawOutlinedString(g2d, 60 + 20 + STROKE_OFFSET, TOPBAR_HEIGHT + STROKE_OFFSET + 40 + i * 30, lines[i + stringOffset], Color.WHITE, Color.BLACK, 2);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void redrawRegionlost(RenderPanel renderer) {
|
||||
renderer.addRedrawBound(TOPBAR_HEIGHT, W - 120, MAINBAR_HEIGHT, 20);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void notifyForecastProviderUpdate(RenderPanel renderer, ForecastProvider forecastProvider) {
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package com.flaremicro.visualforecast.displays;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import com.flaremicro.visualforecast.PropertyManager;
|
||||
|
||||
public class DisplayFactory {
|
||||
int currentIndex = -1;
|
||||
String indexValue = "";
|
||||
|
||||
private List<String> nameOrder = new ArrayList<String>();
|
||||
private Map<String, Display> displays = new HashMap<String, Display>();
|
||||
|
||||
public DisplayFactory(PropertyManager propertyManager)
|
||||
{
|
||||
displays.put("message", new MessageForecastDisplay());
|
||||
displays.put("day", new DayForecastDisplay());
|
||||
displays.put("hourly", new HourlyForecastDisplay());
|
||||
nameOrder.add("message");
|
||||
nameOrder.add("day");
|
||||
nameOrder.add("hourly");
|
||||
}
|
||||
|
||||
public Display nextDisplay() {
|
||||
currentIndex = (currentIndex + 1) % nameOrder.size();
|
||||
return displays.get(nameOrder.get(currentIndex));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -31,7 +31,6 @@ import com.flaremicro.visualforecast.graphics.RenderConstants;
|
||||
import com.flaremicro.visualforecast.icons.IconProvider;
|
||||
|
||||
public class HourlyForecastDisplay implements Display {
|
||||
private int dayOffset = 0;
|
||||
private Font font;
|
||||
private Font smallFont;
|
||||
private ForecastDetails details;
|
||||
@@ -41,7 +40,7 @@ public class HourlyForecastDisplay implements Display {
|
||||
private int ticksBeforeChange = 200;
|
||||
private int animationTicks = -1;
|
||||
|
||||
private byte[] dewval = null;
|
||||
private byte[] windchill = null;
|
||||
private byte[] tempval = null;
|
||||
private float[] percipval = null;
|
||||
|
||||
@@ -56,7 +55,26 @@ public class HourlyForecastDisplay implements Display {
|
||||
|
||||
@Override
|
||||
public void tick(RenderPanel renderer, long ticks, int iconTicks) {
|
||||
ticksBeforeChange--;
|
||||
if (ticksBeforeChange <= 0)
|
||||
{
|
||||
ticksBeforeChange = 200;
|
||||
|
||||
do
|
||||
{
|
||||
townIndex++;
|
||||
if (details == null || townIndex >= details.getTownForecast().length)
|
||||
{
|
||||
renderer.nextDisplay();
|
||||
return;
|
||||
}
|
||||
this.currentTown = details.getTownForecast()[townIndex];
|
||||
}
|
||||
while (this.currentTown == null || currentTown.getHourlyForecast() == null || currentTown.getHourlyForecast().length == 0);
|
||||
renderer.setCurrentTown(currentTown.getTownName());
|
||||
setGraphValues(currentTown);
|
||||
renderer.requestFullRepaint();
|
||||
}
|
||||
}
|
||||
|
||||
public void setGraphValues(TownForecast town) {
|
||||
@@ -66,12 +84,12 @@ public class HourlyForecastDisplay implements Display {
|
||||
if (forecast != null && forecast.length > 0)
|
||||
{
|
||||
int size = Math.min(12, forecast.length);
|
||||
dewval = new byte[size];
|
||||
windchill = new byte[size];
|
||||
tempval = new byte[size];
|
||||
percipval = new float[size];
|
||||
for (int i = 0; i < size; i++)
|
||||
{
|
||||
dewval[i] = forecast[i].dewPoint;
|
||||
windchill[i] = forecast[i].windChill;
|
||||
tempval[i] = forecast[i].temp;
|
||||
percipval[i] = forecast[i].percip;
|
||||
}
|
||||
@@ -133,15 +151,17 @@ public class HourlyForecastDisplay implements Display {
|
||||
FontMetrics fm = g2d.getFontMetrics();
|
||||
|
||||
DrawingUtil.drawOutlinedString(g2d, 100, RenderConstants.TOPBAR_HEIGHT + 35, "Temperature\u00B0c", Color.RED, Color.BLACK, 2);
|
||||
DrawingUtil.drawOutlinedString(g2d, 260, RenderConstants.TOPBAR_HEIGHT + 35, "Dew point\u00B0c", Color.WHITE, Color.BLACK, 2);
|
||||
DrawingUtil.drawOutlinedString(g2d, 260, RenderConstants.TOPBAR_HEIGHT + 35, "Wind Chill\u00B0c", Color.WHITE, Color.BLACK, 2);
|
||||
DrawingUtil.drawOutlinedString(g2d, 410, RenderConstants.TOPBAR_HEIGHT + 35, "Precipitation%", Color.CYAN, Color.BLACK, 2);
|
||||
|
||||
g2d.setFont(font.deriveFont(20F));
|
||||
String minText = min + "\u00B0";
|
||||
String maxText = max + "\u00B0";
|
||||
|
||||
DrawingUtil.drawOutlinedString(g2d, RenderConstants.SIDE_OFFSET + 40 - fm.stringWidth(maxText), RenderConstants.TOPBAR_HEIGHT + 53, maxText, Color.YELLOW, Color.BLACK, 2);
|
||||
DrawingUtil.drawOutlinedString(g2d, RenderConstants.SIDE_OFFSET + 40 - fm.stringWidth(minText), RenderConstants.TOPBAR_HEIGHT + MAINBAR_HEIGHT - 92, minText, Color.YELLOW, Color.BLACK, 2);
|
||||
DrawingUtil.drawOutlinedString(g2d, RenderConstants.SIDE_OFFSET + 38 - fm.stringWidth(maxText), RenderConstants.TOPBAR_HEIGHT + 53, maxText, Color.YELLOW, Color.BLACK, 2);
|
||||
DrawingUtil.drawOutlinedString(g2d, RenderConstants.SIDE_OFFSET + 38 - fm.stringWidth(minText), RenderConstants.TOPBAR_HEIGHT + MAINBAR_HEIGHT - 92, minText, Color.YELLOW, Color.BLACK, 2);
|
||||
|
||||
g2d.setFont(font.deriveFont(20F));
|
||||
g2d.setColor(RenderConstants.BG_ORANGE.darker());
|
||||
g2d.drawLine(RenderConstants.SIDE_OFFSET + 40, (RenderConstants.TOPBAR_HEIGHT + 50), (RenderConstants.W - RenderConstants.SIDE_OFFSET - 20), (RenderConstants.TOPBAR_HEIGHT + 50));
|
||||
|
||||
@@ -150,6 +170,7 @@ public class HourlyForecastDisplay implements Display {
|
||||
|
||||
//BlackLines
|
||||
|
||||
g2d.setFont(smallFont.deriveFont(20F));
|
||||
if (max > 0 && min < 0)
|
||||
{
|
||||
int nextTemp = (RenderConstants.TOPBAR_HEIGHT + RenderConstants.MAINBAR_HEIGHT - 94) + (int) (((min) / (float) range) * (RenderConstants.MAINBAR_HEIGHT - 144));
|
||||
@@ -166,10 +187,10 @@ public class HourlyForecastDisplay implements Display {
|
||||
|
||||
g2d.translate(2, 2);
|
||||
g2d.setColor(Color.BLACK);
|
||||
this.graphTemp(g2d, this.dewval, min, slotWidth, range, RenderConstants.MAINBAR_HEIGHT - 144);
|
||||
this.graphTemp(g2d, this.windchill, min, slotWidth, range, RenderConstants.MAINBAR_HEIGHT - 144);
|
||||
g2d.setColor(Color.WHITE);
|
||||
g2d.translate(-2, -2);
|
||||
this.graphTemp(g2d, this.dewval, min, slotWidth, range, RenderConstants.MAINBAR_HEIGHT - 144);
|
||||
this.graphTemp(g2d, this.windchill, min, slotWidth, range, RenderConstants.MAINBAR_HEIGHT - 144);
|
||||
|
||||
g2d.translate(2, 2);
|
||||
g2d.setColor(Color.BLACK);
|
||||
@@ -196,6 +217,7 @@ public class HourlyForecastDisplay implements Display {
|
||||
for (int i = 0; i < Math.min(12, forecast.length); i++)
|
||||
{
|
||||
String timeString = simpleDateFormat.format(forecast[i].hour);
|
||||
timeString = timeString.substring(0, timeString.length() - 1);
|
||||
if ((i % 2) == 0)
|
||||
DrawingUtil.drawOutlinedString(g2d, RenderConstants.SIDE_OFFSET + 40 + slotWidth / 2 + (slotWidth * i) - fm.stringWidth(timeString) / 2, RenderConstants.TOPBAR_HEIGHT + MAINBAR_HEIGHT - 70, timeString, Color.YELLOW, Color.BLACK, 2);
|
||||
else DrawingUtil.drawOutlinedString(g2d, RenderConstants.SIDE_OFFSET + 40 + slotWidth / 2 + (slotWidth * i) - fm.stringWidth(timeString) / 2, RenderConstants.TOPBAR_HEIGHT + MAINBAR_HEIGHT - 60, timeString, Color.YELLOW, Color.BLACK, 2);
|
||||
@@ -210,13 +232,13 @@ public class HourlyForecastDisplay implements Display {
|
||||
for (int i = 0; i < forecast.length; i++)
|
||||
{
|
||||
int currMin;
|
||||
if (ValueCheck.valueNoData(forecast[i].dewPoint) && ValueCheck.valueNoData(forecast[i].temp))
|
||||
if (ValueCheck.valueNoData(forecast[i].windChill) && ValueCheck.valueNoData(forecast[i].temp))
|
||||
continue;
|
||||
else if (ValueCheck.valueNoData(forecast[i].dewPoint))
|
||||
else if (ValueCheck.valueNoData(forecast[i].windChill))
|
||||
currMin = forecast[i].temp;
|
||||
else if (ValueCheck.valueNoData(forecast[i].temp))
|
||||
currMin = forecast[i].dewPoint;
|
||||
else currMin = Math.min(forecast[i].temp, forecast[i].dewPoint);
|
||||
currMin = forecast[i].windChill;
|
||||
else currMin = Math.min(forecast[i].temp, forecast[i].windChill);
|
||||
if (currMin < min)
|
||||
min = currMin;
|
||||
}
|
||||
@@ -228,13 +250,13 @@ public class HourlyForecastDisplay implements Display {
|
||||
for (int i = 0; i < forecast.length; i++)
|
||||
{
|
||||
int currMax;
|
||||
if (ValueCheck.valueNoData(forecast[i].dewPoint) && ValueCheck.valueNoData(forecast[i].temp))
|
||||
if (ValueCheck.valueNoData(forecast[i].windChill) && ValueCheck.valueNoData(forecast[i].temp))
|
||||
continue;
|
||||
else if (ValueCheck.valueNoData(forecast[i].dewPoint))
|
||||
else if (ValueCheck.valueNoData(forecast[i].windChill))
|
||||
currMax = forecast[i].temp;
|
||||
else if (ValueCheck.valueNoData(forecast[i].temp))
|
||||
currMax = forecast[i].dewPoint;
|
||||
else currMax = Math.max(forecast[i].temp, forecast[i].dewPoint);
|
||||
currMax = forecast[i].windChill;
|
||||
else currMax = Math.max(forecast[i].temp, forecast[i].windChill);
|
||||
if (currMax > max)
|
||||
max = currMax;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,175 @@
|
||||
package com.flaremicro.visualforecast.displays;
|
||||
|
||||
import java.awt.Color;
|
||||
import java.awt.Font;
|
||||
import java.awt.FontMetrics;
|
||||
import java.awt.Graphics2D;
|
||||
import java.awt.Rectangle;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.util.ArrayList;
|
||||
|
||||
import com.flaremicro.visualforecast.RenderPanel;
|
||||
import com.flaremicro.visualforecast.api.ForecastProvider;
|
||||
import com.flaremicro.visualforecast.graphics.DrawingUtil;
|
||||
import com.flaremicro.visualforecast.graphics.FontManager;
|
||||
|
||||
import static com.flaremicro.visualforecast.graphics.RenderConstants.*;
|
||||
|
||||
public class MessageForecastDisplay implements Display {
|
||||
|
||||
private String[] lines = new String[0];
|
||||
private Font font;
|
||||
|
||||
private int stringOffset = 0;
|
||||
private int ticksBeforeChange = 0;
|
||||
|
||||
public MessageForecastDisplay() {
|
||||
font = FontManager.getInstance().getOrCreateFont(Font.TRUETYPE_FONT, this.getClass().getResource("/Star4000.ttf")).deriveFont(30F);
|
||||
|
||||
/*
|
||||
int w2 = g2d.getFontMetrics().stringWidth("EXTREME WEATHER ADVISORY");
|
||||
drawOutlinedString(g2d, (W >> 1) - (w2 >> 1), TOPBAR_HEIGHT + 48, "EXTREME WEATHER ADVISORY", Color.RED, Color.BLACK, 2);
|
||||
|
||||
g2d.setFont(font.deriveFont(30F));
|
||||
for (int i = 0; i < testString.length; i++)
|
||||
{
|
||||
drawOutlinedString(g2d, 90, TOPBAR_HEIGHT + 78 + 25 * i, testString[i], Color.WHITE, Color.BLACK, 1);
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
@Override
|
||||
public void tick(RenderPanel renderer, long ticks, int iconTicks) {
|
||||
ticksBeforeChange--;
|
||||
if (ticksBeforeChange <= 0)
|
||||
{
|
||||
stringOffset += 9;
|
||||
if (stringOffset < lines.length)
|
||||
{
|
||||
ticksBeforeChange = 200;
|
||||
renderer.requestExclusiveBoundedRepaint();
|
||||
}
|
||||
else
|
||||
{
|
||||
renderer.nextDisplay();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void initDisplay(RenderPanel renderer, ForecastProvider forecastProvider, long ticks, int iconTicks) {
|
||||
ticksBeforeChange = 200;
|
||||
stringOffset = 0;
|
||||
String test = "*IMPORTANT NOTICE*\n*THIS BETA IS UNFINISHED*\n\nThis is a Beta version of the VisualForeast 1000! Many things are not finished, and only the hourly and 7 day forecasts are ready. There will be a lot more to come!\nPlease note that there are some issues with data collection from Environment Canada resulting in missing or potentially incorrect information around midnight hours. This will be fixed as the project continues and the disclaimer will be removed.";
|
||||
ArrayList<String> linesList = new ArrayList<String>();
|
||||
|
||||
renderer.setCurrentForecast("Welcome to the");
|
||||
renderer.setCurrentTown("VisualForecast 1000");
|
||||
|
||||
BufferedImage disposableImage = new BufferedImage(1, 1, BufferedImage.TYPE_BYTE_GRAY);
|
||||
Graphics2D g = disposableImage.createGraphics();
|
||||
FontMetrics f = g.getFontMetrics(font);
|
||||
String[] lines = test.split("\n");
|
||||
for (int i = 0; i < lines.length; i++)
|
||||
{
|
||||
splitText(lines[i], W - 150 - STROKE_WIDTH, f, linesList);
|
||||
//if(words[i])
|
||||
}
|
||||
|
||||
g.dispose();
|
||||
disposableImage.flush();
|
||||
|
||||
redrawRegionlost(renderer);
|
||||
|
||||
this.lines = linesList.toArray(new String[0]);
|
||||
}
|
||||
|
||||
private void splitText(String text, int maxWidth, FontMetrics fontMetrics, ArrayList<String> lineList) {
|
||||
|
||||
text = text.trim();
|
||||
//this.text.add(text);
|
||||
if (fontMetrics.stringWidth(text) <= maxWidth)
|
||||
lineList.add(text);
|
||||
else
|
||||
{
|
||||
while (text.length() > 0)
|
||||
{
|
||||
int idx = binSearch(text, fontMetrics, maxWidth, 1, text.length());
|
||||
if (idx <= 0)
|
||||
break;
|
||||
String texPart = text.substring(0, idx).trim();
|
||||
if (fontMetrics.stringWidth(text.substring(0, Math.min(text.length(), idx + 1))) > maxWidth && texPart.contains(" "))
|
||||
{
|
||||
idx = texPart.lastIndexOf(" ");
|
||||
texPart = texPart.substring(0, idx);
|
||||
}
|
||||
lineList.add(texPart);
|
||||
text = text.substring(idx).trim();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static int binSearch(String string, FontMetrics fontMetrics, int key, int low, int high) {
|
||||
int index = 0;
|
||||
|
||||
while (low <= high)
|
||||
{
|
||||
int mid = low + ((high - low) / 2);
|
||||
int midmetric = fontMetrics.stringWidth(string.substring(0, mid));
|
||||
if (midmetric < key)
|
||||
{
|
||||
low = mid + 1;
|
||||
index = mid;
|
||||
}
|
||||
else if (midmetric > key)
|
||||
{
|
||||
high = mid - 1;
|
||||
}
|
||||
else if (midmetric == key)
|
||||
{
|
||||
index = mid;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return index;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void drawDisplay(RenderPanel renderer, Graphics2D g2d, long ticks, int iconTicks) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void drawBoundLimitedDisplay(RenderPanel renderer, Graphics2D g2d, Rectangle bounds, long ticks, int iconTicks) {
|
||||
DrawingUtil.drawGradientRect(g2d, 60, TOPBAR_HEIGHT, W - 120, MAINBAR_HEIGHT, 20, BG_BLUE.darker(), BG_BLUE.brighter());
|
||||
g2d.setFont(font);
|
||||
FontMetrics fontMetrics = g2d.getFontMetrics();
|
||||
g2d.setColor(BG_BLUE.darker());
|
||||
g2d.drawRect(60 + STROKE_OFFSET, TOPBAR_HEIGHT + STROKE_OFFSET, W - 120 - STROKE_WIDTH, MAINBAR_HEIGHT - STROKE_WIDTH);
|
||||
for (int i = 0; i < Math.min(9, lines.length - this.stringOffset); i++)
|
||||
{
|
||||
if (lines[i + stringOffset].startsWith("*") && lines[i].endsWith("*"))
|
||||
{
|
||||
String line = lines[i + stringOffset].substring(1, lines[i + stringOffset].length() - 1);
|
||||
DrawingUtil.drawOutlinedString(g2d, 60 + 20 + STROKE_OFFSET, TOPBAR_HEIGHT + STROKE_OFFSET + 40 + i * 30, "*", Color.WHITE, Color.BLACK, 2);
|
||||
DrawingUtil.drawOutlinedString(g2d, 60 + 20 + STROKE_OFFSET + (W - 150 - STROKE_WIDTH) / 2 - fontMetrics.stringWidth(line) / 2, TOPBAR_HEIGHT + STROKE_OFFSET + 40 + i * 30, line, Color.WHITE, Color.BLACK, 2);
|
||||
DrawingUtil.drawOutlinedString(g2d, W - 95 - STROKE_WIDTH, TOPBAR_HEIGHT + STROKE_OFFSET + 40 + i * 30, "*", Color.WHITE, Color.BLACK, 2);
|
||||
}
|
||||
else if (lines[i + stringOffset].startsWith("[") && lines[i].endsWith("]"))
|
||||
{
|
||||
String line = lines[i + stringOffset].substring(1, lines[i + stringOffset].length() - 1);
|
||||
DrawingUtil.drawOutlinedString(g2d, 60 + 20 + STROKE_OFFSET + (W - 150 - STROKE_WIDTH) / 2 - fontMetrics.stringWidth(line) / 2, TOPBAR_HEIGHT + STROKE_OFFSET + 40 + i * 30, line, Color.WHITE, Color.BLACK, 2);
|
||||
}
|
||||
else DrawingUtil.drawOutlinedString(g2d, 60 + 20 + STROKE_OFFSET, TOPBAR_HEIGHT + STROKE_OFFSET + 40 + i * 30, lines[i + stringOffset], Color.WHITE, Color.BLACK, 2);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void redrawRegionlost(RenderPanel renderer) {
|
||||
renderer.addRedrawBound(TOPBAR_HEIGHT, W - 120, MAINBAR_HEIGHT, 20);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void notifyForecastProviderUpdate(RenderPanel renderer, ForecastProvider forecastProvider) {
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,155 @@
|
||||
package com.flaremicro.visualforecast.displays;
|
||||
|
||||
import java.awt.Color;
|
||||
import java.awt.Font;
|
||||
import java.awt.FontMetrics;
|
||||
import java.awt.Graphics2D;
|
||||
import java.awt.Rectangle;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.util.ArrayList;
|
||||
|
||||
import com.flaremicro.visualforecast.RenderPanel;
|
||||
import com.flaremicro.visualforecast.api.ForecastProvider;
|
||||
import com.flaremicro.visualforecast.graphics.DrawingUtil;
|
||||
import com.flaremicro.visualforecast.graphics.FontManager;
|
||||
|
||||
import static com.flaremicro.visualforecast.graphics.RenderConstants.*;
|
||||
|
||||
public class ThirtySixHourDisplay implements Display {
|
||||
|
||||
private String[] lines = new String[0];
|
||||
private Font font;
|
||||
|
||||
|
||||
public ThirtySixHourDisplay() {
|
||||
font = FontManager.getInstance().getOrCreateFont(Font.TRUETYPE_FONT, this.getClass().getResource("/Star4000.ttf")).deriveFont(30F);
|
||||
|
||||
/*
|
||||
int w2 = g2d.getFontMetrics().stringWidth("EXTREME WEATHER ADVISORY");
|
||||
drawOutlinedString(g2d, (W >> 1) - (w2 >> 1), TOPBAR_HEIGHT + 48, "EXTREME WEATHER ADVISORY", Color.RED, Color.BLACK, 2);
|
||||
|
||||
g2d.setFont(font.deriveFont(30F));
|
||||
for (int i = 0; i < testString.length; i++)
|
||||
{
|
||||
drawOutlinedString(g2d, 90, TOPBAR_HEIGHT + 78 + 25 * i, testString[i], Color.WHITE, Color.BLACK, 1);
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
@Override
|
||||
public void tick(RenderPanel renderer, long ticks, int iconTicks) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void initDisplay(RenderPanel renderer, ForecastProvider forecastProvider, long ticks, int iconTicks) {
|
||||
String test = "*IMPORTANT NOTICE*\n*POWER OUTAGE POSSIBLE*\n*DUE TO EXTREME WINDS*\n\nHurricane-force winds capable of reaching 127km/h will be hitting the east coast around noon tomorrow. Keep windows and doors barricaded to prevent";
|
||||
ArrayList<String> linesList = new ArrayList<String>();
|
||||
|
||||
BufferedImage disposableImage = new BufferedImage(1, 1, BufferedImage.TYPE_BYTE_GRAY);
|
||||
Graphics2D g = disposableImage.createGraphics();
|
||||
FontMetrics f = g.getFontMetrics(font);
|
||||
System.out.println("yas2");
|
||||
String[] lines = test.split("\n");
|
||||
for(int i = 0; i < lines.length; i++)
|
||||
{
|
||||
splitText(lines[i], W - 150 - STROKE_WIDTH, f, linesList);
|
||||
//if(words[i])
|
||||
}
|
||||
|
||||
g.dispose();
|
||||
disposableImage.flush();
|
||||
|
||||
System.out.println("yas");
|
||||
|
||||
this.lines = linesList.toArray(new String[0]);
|
||||
}
|
||||
|
||||
private void splitText(String text, int maxWidth, FontMetrics fontMetrics, ArrayList<String> lineList) {
|
||||
|
||||
text = text.trim();
|
||||
//this.text.add(text);
|
||||
if (fontMetrics.stringWidth(text) <= maxWidth)
|
||||
lineList.add(text);
|
||||
else
|
||||
{
|
||||
while (text.length() > 0)
|
||||
{
|
||||
int idx = binSearch(text, fontMetrics, maxWidth, 1, text.length());
|
||||
if (idx <= 0)
|
||||
break;
|
||||
String texPart = text.substring(0, idx).trim();
|
||||
if (fontMetrics.stringWidth(text.substring(0, Math.min(text.length(), idx + 1))) > maxWidth && texPart.contains(" "))
|
||||
{
|
||||
idx = texPart.lastIndexOf(" ");
|
||||
texPart = texPart.substring(0, idx);
|
||||
}
|
||||
lineList.add(texPart);
|
||||
text = text.substring(idx).trim();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static int binSearch(String string, FontMetrics fontMetrics, int key, int low, int high) {
|
||||
int index = 0;
|
||||
|
||||
while (low <= high)
|
||||
{
|
||||
int mid = low + ((high - low) / 2);
|
||||
int midmetric = fontMetrics.stringWidth(string.substring(0, mid));
|
||||
if (midmetric < key)
|
||||
{
|
||||
low = mid + 1;
|
||||
index = mid;
|
||||
}
|
||||
else if (midmetric > key)
|
||||
{
|
||||
high = mid - 1;
|
||||
}
|
||||
else if (midmetric == key)
|
||||
{
|
||||
index = mid;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return index;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void drawDisplay(RenderPanel renderer, Graphics2D g2d, long ticks, int iconTicks) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void drawBoundLimitedDisplay(RenderPanel renderer, Graphics2D g2d, Rectangle bounds, long ticks, int iconTicks) {
|
||||
DrawingUtil.drawGradientRect(g2d, 60, TOPBAR_HEIGHT, W - 120, MAINBAR_HEIGHT, 20, BG_BLUE.brighter(), BG_BLUE.darker());
|
||||
g2d.setFont(font);
|
||||
FontMetrics fontMetrics = g2d.getFontMetrics();
|
||||
g2d.setColor(BG_BLUE.brighter());
|
||||
g2d.drawRect(60 + STROKE_OFFSET, TOPBAR_HEIGHT + STROKE_OFFSET, W - 120 - STROKE_WIDTH, MAINBAR_HEIGHT - STROKE_WIDTH);
|
||||
for(int i = 0; i < lines.length; i++)
|
||||
{
|
||||
if(lines[i].startsWith("*") && lines[i].endsWith("*"))
|
||||
{
|
||||
String line = lines[i].substring(1, lines[i].length()-1);
|
||||
DrawingUtil.drawOutlinedString(g2d, 60 + 20 + STROKE_OFFSET, TOPBAR_HEIGHT + STROKE_OFFSET + 40 + i*30, "*", Color.WHITE, Color.BLACK, 2);
|
||||
DrawingUtil.drawOutlinedString(g2d, 60 + 20 + STROKE_OFFSET + (W - 150 - STROKE_WIDTH)/2 - fontMetrics.stringWidth(line)/2, TOPBAR_HEIGHT + STROKE_OFFSET + 40 + i*30, line, Color.WHITE, Color.BLACK, 2);
|
||||
DrawingUtil.drawOutlinedString(g2d, W - 95 - STROKE_WIDTH, TOPBAR_HEIGHT + STROKE_OFFSET + 40 + i*30, "*", Color.WHITE, Color.BLACK, 2);
|
||||
}
|
||||
else if(lines[i].startsWith("[") && lines[i].endsWith("]"))
|
||||
{
|
||||
String line = lines[i].substring(1, lines[i].length()-1);
|
||||
DrawingUtil.drawOutlinedString(g2d, 60 + 20 + STROKE_OFFSET + (W - 150 - STROKE_WIDTH)/2 - fontMetrics.stringWidth(line)/2, TOPBAR_HEIGHT + STROKE_OFFSET + 40 + i*30, line, Color.WHITE, Color.BLACK, 2);
|
||||
}
|
||||
else
|
||||
DrawingUtil.drawOutlinedString(g2d, 60 + 20 + STROKE_OFFSET, TOPBAR_HEIGHT + STROKE_OFFSET + 40 + i*30, lines[i], Color.WHITE, Color.BLACK, 2);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void redrawRegionlost(RenderPanel renderer) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void notifyForecastProviderUpdate(RenderPanel renderer, ForecastProvider forecastProvider) {
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user