Added hourly forecast
This commit is contained in:
@@ -0,0 +1,252 @@
|
||||
package com.flaremicro.visualforecast.displays;
|
||||
|
||||
import static com.flaremicro.visualforecast.graphics.RenderConstants.BG_BLUE;
|
||||
import static com.flaremicro.visualforecast.graphics.RenderConstants.MAINBAR_HEIGHT;
|
||||
|
||||
import java.awt.BasicStroke;
|
||||
import java.awt.Color;
|
||||
import java.awt.Font;
|
||||
import java.awt.FontMetrics;
|
||||
import java.awt.GradientPaint;
|
||||
import java.awt.Graphics2D;
|
||||
import java.awt.Rectangle;
|
||||
import java.awt.Transparency;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.text.DateFormat;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Calendar;
|
||||
import java.util.Locale;
|
||||
|
||||
import com.flaremicro.visualforecast.RenderPanel;
|
||||
import com.flaremicro.visualforecast.api.ForecastProvider;
|
||||
import com.flaremicro.visualforecast.forecast.DayForecast;
|
||||
import com.flaremicro.visualforecast.forecast.ForecastDetails;
|
||||
import com.flaremicro.visualforecast.forecast.HourlyForecast;
|
||||
import com.flaremicro.visualforecast.forecast.TownForecast;
|
||||
import com.flaremicro.visualforecast.forecast.ValueCheck;
|
||||
import com.flaremicro.visualforecast.graphics.DrawingUtil;
|
||||
import com.flaremicro.visualforecast.graphics.FontManager;
|
||||
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;
|
||||
private TownForecast currentTown = null;
|
||||
private int townIndex;
|
||||
|
||||
private int ticksBeforeChange = 200;
|
||||
private int animationTicks = -1;
|
||||
|
||||
DateFormat simpleDateFormat = new SimpleDateFormat("ha");
|
||||
|
||||
public HourlyForecastDisplay() {
|
||||
font = FontManager.getInstance().getOrCreateFont(Font.TRUETYPE_FONT, this.getClass().getResource("/Star4000.ttf"));
|
||||
smallFont = FontManager.getInstance().getOrCreateFont(Font.TRUETYPE_FONT, this.getClass().getResource("/Star4000 Small.ttf"));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void tick(RenderPanel renderer, long ticks, int iconTicks) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void initDisplay(RenderPanel renderer, ForecastProvider forecastProvider, long ticks, int iconTicks) {
|
||||
this.details = forecastProvider != null ? forecastProvider.getForecast() : null;
|
||||
renderer.setCurrentForecast("12 Hour Forecast");
|
||||
if (details == null || details.getTownForecast() == null || details.getTownForecast().length <= 0)
|
||||
this.details = null;
|
||||
else
|
||||
{
|
||||
townIndex = 0;
|
||||
currentTown = details.getTownForecast()[townIndex];
|
||||
renderer.setCurrentTown(currentTown.getTownName());
|
||||
}
|
||||
redrawRegionlost(renderer);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void drawDisplay(RenderPanel renderer, Graphics2D g2d, long ticks, int iconTicks) {
|
||||
}
|
||||
|
||||
public int minTemp(HourlyForecast[] forecast) {
|
||||
int min = Integer.MAX_VALUE;
|
||||
for (int i = 0; i < forecast.length; i++)
|
||||
{
|
||||
int currMin;
|
||||
if (ValueCheck.valueNoData(forecast[i].dewPoint) && ValueCheck.valueNoData(forecast[i].temp))
|
||||
continue;
|
||||
else if (ValueCheck.valueNoData(forecast[i].dewPoint))
|
||||
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);
|
||||
if (currMin < min)
|
||||
min = currMin;
|
||||
}
|
||||
return min;
|
||||
}
|
||||
|
||||
public int maxTemp(HourlyForecast[] forecast) {
|
||||
int max = Integer.MIN_VALUE;
|
||||
for (int i = 0; i < forecast.length; i++)
|
||||
{
|
||||
int currMax;
|
||||
if (ValueCheck.valueNoData(forecast[i].dewPoint) && ValueCheck.valueNoData(forecast[i].temp))
|
||||
continue;
|
||||
else if (ValueCheck.valueNoData(forecast[i].dewPoint))
|
||||
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);
|
||||
if (currMax > max)
|
||||
max = currMax;
|
||||
}
|
||||
return max;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void drawBoundLimitedDisplay(RenderPanel renderer, Graphics2D g2d, Rectangle bounds, long ticks, int iconTicks) {
|
||||
HourlyForecast[] forecast = currentTown.getHourlyForecast();
|
||||
if(forecast == null)
|
||||
return;
|
||||
|
||||
g2d.setColor(RenderConstants.BG_BLUE);
|
||||
//g2d.fillRect(RenderConstants.SIDE_OFFSET, RenderConstants.TOPBAR_HEIGHT + 20, RenderConstants.W - RenderConstants.SIDE_OFFSET * 2, RenderConstants.MAINBAR_HEIGHT - 40);
|
||||
|
||||
DrawingUtil.drawGradientRect(g2d, RenderConstants.SIDE_OFFSET, RenderConstants.TOPBAR_HEIGHT + 20, RenderConstants.W - RenderConstants.SIDE_OFFSET * 2, RenderConstants.MAINBAR_HEIGHT - 40, 15, BG_BLUE.darker(), BG_BLUE.brighter());
|
||||
|
||||
int min = minTemp(forecast);
|
||||
int max = maxTemp(forecast);
|
||||
|
||||
float range = Math.abs(min)+Math.abs(max);
|
||||
|
||||
|
||||
|
||||
g2d.setPaint(new GradientPaint(0, RenderConstants.TOPBAR_HEIGHT + 40, RenderConstants.BG_ORANGE, 0, RenderConstants.MAINBAR_HEIGHT, RenderConstants.BG_PURPLE));
|
||||
g2d.fillRect(RenderConstants.SIDE_OFFSET + 40, RenderConstants.TOPBAR_HEIGHT + 40, RenderConstants.W - RenderConstants.SIDE_OFFSET * 2 - 60, RenderConstants.MAINBAR_HEIGHT - 124);
|
||||
|
||||
g2d.setColor(Color.BLACK);
|
||||
g2d.drawRect(RenderConstants.SIDE_OFFSET, RenderConstants.TOPBAR_HEIGHT + 20, RenderConstants.W - RenderConstants.SIDE_OFFSET * 2, RenderConstants.MAINBAR_HEIGHT - 40);
|
||||
|
||||
/*float lineHeight = (RenderConstants.MAINBAR_HEIGHT - 104) / 9F;
|
||||
for(int i = 0; i < 10; i++)
|
||||
{
|
||||
g2d.drawLine(RenderConstants.SIDE_OFFSET + 36, (int)(RenderConstants.TOPBAR_HEIGHT + 40 + lineHeight * i), RenderConstants.W - RenderConstants.SIDE_OFFSET - 36, (int) (RenderConstants.TOPBAR_HEIGHT + 40 + lineHeight * i));
|
||||
}*/
|
||||
g2d.setFont(smallFont.deriveFont(20F));
|
||||
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, 410, RenderConstants.TOPBAR_HEIGHT + 35, "Precipitation%", Color.CYAN, Color.BLACK, 2);
|
||||
|
||||
|
||||
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);
|
||||
|
||||
int slotWidth = (RenderConstants.W - RenderConstants.SIDE_OFFSET * 2 - 60) / 12;
|
||||
|
||||
int lastDewpoint = 0;
|
||||
int lastTemperature = 0;
|
||||
int lastPercip = 0;
|
||||
|
||||
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));
|
||||
|
||||
g2d.setColor(RenderConstants.BG_PURPLE.brighter());
|
||||
g2d.drawLine(RenderConstants.SIDE_OFFSET+40, RenderConstants.TOPBAR_HEIGHT + 50 + RenderConstants.MAINBAR_HEIGHT - 144, (RenderConstants.W - RenderConstants.SIDE_OFFSET - 20), RenderConstants.TOPBAR_HEIGHT + 50 + RenderConstants.MAINBAR_HEIGHT - 144);
|
||||
|
||||
//BlackLines
|
||||
g2d.setClip(RenderConstants.SIDE_OFFSET + 40, RenderConstants.TOPBAR_HEIGHT + 40, RenderConstants.W - RenderConstants.SIDE_OFFSET * 2 - 60, RenderConstants.MAINBAR_HEIGHT - 124);
|
||||
g2d.setStroke(new BasicStroke(2));
|
||||
g2d.setColor(Color.BLACK);
|
||||
for(int i = 0; i < Math.min(12, forecast.length); i++)
|
||||
{
|
||||
int nextDewpoint = (RenderConstants.TOPBAR_HEIGHT + 50) + (RenderConstants.MAINBAR_HEIGHT - 144) - (int)(((max-forecast[i].dewPoint)/range)*(RenderConstants.MAINBAR_HEIGHT - 144));
|
||||
int nextTemperature = (RenderConstants.TOPBAR_HEIGHT + 50) + (RenderConstants.MAINBAR_HEIGHT - 144) - (int)(((max-forecast[i].temp)/range)*(RenderConstants.MAINBAR_HEIGHT - 144));
|
||||
int nextPercip = (RenderConstants.TOPBAR_HEIGHT + 50) + (RenderConstants.MAINBAR_HEIGHT - 144) - (int) ((forecast[i].percip/100F)*(RenderConstants.MAINBAR_HEIGHT - 144));
|
||||
if(i == 0)
|
||||
{
|
||||
lastDewpoint = nextDewpoint;
|
||||
lastTemperature = nextTemperature;
|
||||
lastPercip = nextPercip;
|
||||
}
|
||||
g2d.drawLine(RenderConstants.SIDE_OFFSET+40+slotWidth/2+(slotWidth*(i-1))+1, lastDewpoint+2, RenderConstants.SIDE_OFFSET+40+slotWidth/2+(slotWidth*i)+2, nextDewpoint+2);
|
||||
g2d.drawLine(RenderConstants.SIDE_OFFSET+40+slotWidth/2+(slotWidth*(i-1))+1, lastPercip+2, RenderConstants.SIDE_OFFSET+40+slotWidth/2+(slotWidth*i)+2, nextPercip+2);
|
||||
g2d.drawLine(RenderConstants.SIDE_OFFSET+40+slotWidth/2+(slotWidth*(i-1))+1, lastTemperature+2, RenderConstants.SIDE_OFFSET+40+slotWidth/2+(slotWidth*i)+2, nextTemperature+2);
|
||||
if(i == Math.min(12, forecast.length)-1)
|
||||
{
|
||||
g2d.drawLine(RenderConstants.SIDE_OFFSET+40+slotWidth/2+(slotWidth*(i)), nextDewpoint+2, RenderConstants.SIDE_OFFSET+40+slotWidth/2+(slotWidth*(i+2)), nextDewpoint+2);
|
||||
g2d.drawLine(RenderConstants.SIDE_OFFSET+40+slotWidth/2+(slotWidth*(i)), nextPercip+2, RenderConstants.SIDE_OFFSET+40+slotWidth/2+(slotWidth*(i+2)), nextPercip+2);
|
||||
g2d.drawLine(RenderConstants.SIDE_OFFSET+40+slotWidth/2+(slotWidth*(i)), nextTemperature+2, RenderConstants.SIDE_OFFSET+40+slotWidth/2+(slotWidth*(i+2)), nextTemperature+2);
|
||||
}
|
||||
lastPercip = nextPercip;
|
||||
lastTemperature = nextTemperature;
|
||||
lastDewpoint = nextDewpoint;
|
||||
}
|
||||
g2d.setClip(null);
|
||||
|
||||
for(int i = 0; i < Math.min(12, forecast.length); i++)
|
||||
{
|
||||
g2d.setClip(RenderConstants.SIDE_OFFSET + 40, RenderConstants.TOPBAR_HEIGHT + 40, RenderConstants.W - RenderConstants.SIDE_OFFSET * 2 - 60, RenderConstants.MAINBAR_HEIGHT - 124);
|
||||
int nextDewpoint = (RenderConstants.TOPBAR_HEIGHT + 50) + (RenderConstants.MAINBAR_HEIGHT - 144) - (int)(((max-forecast[i].dewPoint)/range)*(RenderConstants.MAINBAR_HEIGHT - 144));
|
||||
int nextTemperature = (RenderConstants.TOPBAR_HEIGHT + 50) + (RenderConstants.MAINBAR_HEIGHT - 144) - (int)(((max-forecast[i].temp)/range)*(RenderConstants.MAINBAR_HEIGHT - 144));
|
||||
int nextPercip = (RenderConstants.TOPBAR_HEIGHT + 50) + (RenderConstants.MAINBAR_HEIGHT - 144) - (int) ((forecast[i].percip/100F)*(RenderConstants.MAINBAR_HEIGHT - 144));
|
||||
if(i == 0)
|
||||
{
|
||||
lastDewpoint = nextDewpoint;
|
||||
lastTemperature = nextTemperature;
|
||||
lastPercip = nextPercip;
|
||||
}
|
||||
g2d.setStroke(new BasicStroke(2));
|
||||
g2d.setColor(Color.WHITE);
|
||||
g2d.drawLine(RenderConstants.SIDE_OFFSET+40+slotWidth/2+(slotWidth*(i-1)), lastDewpoint, RenderConstants.SIDE_OFFSET+40+slotWidth/2+(slotWidth*i), nextDewpoint);
|
||||
g2d.setColor(Color.CYAN);
|
||||
g2d.drawLine(RenderConstants.SIDE_OFFSET+40+slotWidth/2+(slotWidth*(i-1)), lastPercip, RenderConstants.SIDE_OFFSET+40+slotWidth/2+(slotWidth*i), nextPercip);
|
||||
g2d.setColor(Color.RED);
|
||||
g2d.drawLine(RenderConstants.SIDE_OFFSET+40+slotWidth/2+(slotWidth*(i-1)), lastTemperature, RenderConstants.SIDE_OFFSET+40+slotWidth/2+(slotWidth*i), nextTemperature);
|
||||
if(i == Math.min(12, forecast.length)-1)
|
||||
{
|
||||
g2d.setColor(Color.WHITE);
|
||||
g2d.drawLine(RenderConstants.SIDE_OFFSET+40+slotWidth/2+(slotWidth*(i)), nextDewpoint, RenderConstants.SIDE_OFFSET+40+slotWidth/2+(slotWidth*(i+1)), nextDewpoint);
|
||||
g2d.setColor(Color.CYAN);
|
||||
g2d.drawLine(RenderConstants.SIDE_OFFSET+40+slotWidth/2+(slotWidth*(i)), nextPercip, RenderConstants.SIDE_OFFSET+40+slotWidth/2+(slotWidth*(i+1)), nextPercip);
|
||||
g2d.setColor(Color.RED);
|
||||
g2d.drawLine(RenderConstants.SIDE_OFFSET+40+slotWidth/2+(slotWidth*(i)), nextTemperature, RenderConstants.SIDE_OFFSET+40+slotWidth/2+(slotWidth*(i+1)), nextTemperature);
|
||||
}
|
||||
|
||||
g2d.setClip(null);
|
||||
lastPercip = nextPercip;
|
||||
lastTemperature = nextTemperature;
|
||||
lastDewpoint = nextDewpoint;
|
||||
|
||||
String timeString = simpleDateFormat.format(forecast[i].hour);
|
||||
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);
|
||||
|
||||
IconProvider.drawIcon(g2d, IconProvider.INDEXED_ICONS[forecast[i].iconId], RenderConstants.SIDE_OFFSET + 45 + i * slotWidth , RenderConstants.TOPBAR_HEIGHT + MAINBAR_HEIGHT - 55, slotWidth - 10, iconTicks);
|
||||
}
|
||||
g2d.setStroke(new BasicStroke(2));
|
||||
g2d.setColor(Color.BLACK);
|
||||
g2d.drawRect(RenderConstants.SIDE_OFFSET + 40, RenderConstants.TOPBAR_HEIGHT + 40, RenderConstants.W - RenderConstants.SIDE_OFFSET * 2 - 60, RenderConstants.MAINBAR_HEIGHT - 124);
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void redrawRegionlost(RenderPanel renderer) {
|
||||
renderer.addRedrawBound(0, RenderConstants.TOPBAR_HEIGHT, RenderConstants.W, RenderConstants.MAINBAR_HEIGHT);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void notifyForecastProviderUpdate(RenderPanel renderer, ForecastProvider forecastProvider) {
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user