| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
package ca.spaz.cron.summary; |
| 5 |
|
|
| 6 |
|
import java.awt.*; |
| 7 |
|
import java.util.*; |
| 8 |
|
import java.util.List; |
| 9 |
|
|
| 10 |
|
import javax.swing.*; |
| 11 |
|
import javax.swing.border.Border; |
| 12 |
|
|
| 13 |
|
import ca.spaz.cron.database.*; |
| 14 |
|
import ca.spaz.cron.targets.Target; |
| 15 |
|
import ca.spaz.cron.user.User; |
| 16 |
|
import ca.spaz.cron.user.impl.CRONUser; |
| 17 |
|
|
| 18 |
|
public class TargetSummaryChart extends JComponent { |
| 19 |
0 |
double energy = 0; |
| 20 |
0 |
double protein = 0; |
| 21 |
0 |
double carbs = 0; |
| 22 |
0 |
double lipid = 0; |
| 23 |
|
|
| 24 |
0 |
public TargetSummaryChart() { |
| 25 |
0 |
setBorder(BorderFactory.createEmptyBorder(10,10,10,10)); |
| 26 |
0 |
} |
| 27 |
|
|
| 28 |
|
public void update(Observable source, Object message) { |
| 29 |
0 |
if (!(message instanceof List)) { |
| 30 |
0 |
return; |
| 31 |
|
} |
| 32 |
0 |
List consumed = (List) message; |
| 33 |
0 |
energy = getAmount(consumed, NutrientInfo.getByTableName("kcals")); |
| 34 |
0 |
protein = getAmount(consumed, NutrientInfo.getByTableName("protein")); |
| 35 |
0 |
carbs = getAmount(consumed, NutrientInfo.getByTableName("carbs")); |
| 36 |
0 |
lipid = getAmount(consumed, NutrientInfo.getByTableName("lipid")); |
| 37 |
|
|
| 38 |
0 |
repaint(); |
| 39 |
0 |
} |
| 40 |
|
|
| 41 |
|
private double getAmount(List servings, NutrientInfo ni) { |
| 42 |
0 |
double total = 0; |
| 43 |
0 |
for (Iterator iter = servings.iterator(); iter.hasNext();) { |
| 44 |
0 |
Serving serving = (Serving) iter.next(); |
| 45 |
0 |
double weight = serving.getGrams()/100.0; |
| 46 |
0 |
total += weight * serving.getFood().getNutrientAmount(ni); |
| 47 |
0 |
} |
| 48 |
0 |
return total; |
| 49 |
|
} |
| 50 |
|
|
| 51 |
|
|
| 52 |
|
public void paintComponent(Graphics g) { |
| 53 |
0 |
User user = CRONUser.getUser(); |
| 54 |
|
|
| 55 |
0 |
int w = getWidth(); |
| 56 |
0 |
int h = getHeight(); |
| 57 |
0 |
int xo = 0; |
| 58 |
0 |
int yo = 0; |
| 59 |
0 |
Border border = getBorder(); |
| 60 |
0 |
if (border != null) { |
| 61 |
0 |
Insets insets = border.getBorderInsets(this); |
| 62 |
0 |
w -= insets.left + insets.right; |
| 63 |
0 |
h -= insets.top + insets.bottom; |
| 64 |
0 |
xo = insets.left; |
| 65 |
0 |
yo = insets.top; |
| 66 |
|
} |
| 67 |
|
|
| 68 |
|
|
| 69 |
0 |
int min = w<h?w:h; |
| 70 |
0 |
int barHeight = 24; |
| 71 |
0 |
int pieRadius = 80; |
| 72 |
0 |
double barFill = 0; |
| 73 |
|
|
| 74 |
0 |
g.setFont(g.getFont().deriveFont(Font.BOLD)); |
| 75 |
0 |
FontMetrics fm = g.getFontMetrics(); |
| 76 |
0 |
Graphics2D g2d = (Graphics2D)g; |
| 77 |
0 |
g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.75f)); |
| 78 |
0 |
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); |
| 79 |
|
|
| 80 |
0 |
Target energyTarget = user.getTarget(NutrientInfo.getByTableName("kcals")); |
| 81 |
0 |
g.setColor(Color.LIGHT_GRAY); |
| 82 |
0 |
g.fillRect(xo,yo,w,barHeight); |
| 83 |
0 |
g.setColor(Color.ORANGE); |
| 84 |
0 |
if (energy > 0) { |
| 85 |
0 |
barFill = energy/energyTarget.getMin(); |
| 86 |
0 |
if (barFill > 1) { |
| 87 |
0 |
barFill = 1; |
| 88 |
0 |
g.setColor(Color.ORANGE.brighter()); |
| 89 |
0 |
g.fill3DRect(xo,yo,(int)(w*barFill), barHeight, false); |
| 90 |
0 |
} else { |
| 91 |
0 |
g.fill3DRect(xo,yo,(int)(w*barFill), barHeight, true); |
| 92 |
|
} |
| 93 |
|
} |
| 94 |
0 |
g.setColor(Color.BLACK); |
| 95 |
0 |
g.drawString("Calories: " + (int)energy +" / " + (int)energyTarget.getMin() |
| 96 |
|
+ " (" + Math.round(100*energy/energyTarget.getMin()) + "%)", |
| 97 |
|
xo+10, yo+barHeight/2+fm.getAscent()/2); |
| 98 |
|
|
| 99 |
|
|
| 100 |
0 |
Target proteinTarget = user.getTarget(NutrientInfo.getByTableName("protein")); |
| 101 |
0 |
g.setColor(Color.LIGHT_GRAY); |
| 102 |
0 |
g.fillRect(xo,yo+(barHeight+5),w-pieRadius,barHeight); |
| 103 |
0 |
g.setColor(Color.GREEN); |
| 104 |
0 |
if (protein > 0) { |
| 105 |
0 |
barFill = protein/proteinTarget.getMin(); |
| 106 |
0 |
if (barFill > 1) { |
| 107 |
0 |
barFill = 1; |
| 108 |
0 |
g.setColor(Color.GREEN.brighter()); |
| 109 |
0 |
g.fill3DRect(xo,yo+(barHeight+5),(int)((w-pieRadius)*barFill), barHeight,false); |
| 110 |
0 |
} else { |
| 111 |
0 |
g.fill3DRect(xo,yo+(barHeight+5),(int)((w-pieRadius)*barFill), barHeight,true); |
| 112 |
|
} |
| 113 |
|
} |
| 114 |
0 |
g.setColor(Color.BLACK); |
| 115 |
0 |
g.drawString("Protein: " + (int)protein +"g / " + (int)proteinTarget.getMin() |
| 116 |
|
+ "g (" + Math.round(100*protein/proteinTarget.getMin()) + "%)", |
| 117 |
|
xo+10, yo+(barHeight+5)+barHeight/2+fm.getAscent()/2); |
| 118 |
|
|
| 119 |
|
|
| 120 |
0 |
Target carbTarget = user.getTarget(NutrientInfo.getByTableName("carbs")); |
| 121 |
0 |
g.setColor(Color.LIGHT_GRAY); |
| 122 |
0 |
g.fillRect(xo,yo+(barHeight+5)*2,w-pieRadius,barHeight); |
| 123 |
0 |
g.setColor(Color.BLUE); |
| 124 |
0 |
if (carbs > 0) { |
| 125 |
0 |
barFill = carbs/carbTarget.getMin(); |
| 126 |
0 |
if (barFill > 1) { |
| 127 |
0 |
barFill = 1; |
| 128 |
0 |
g.setColor(Color.BLUE.brighter()); |
| 129 |
0 |
g.fill3DRect(xo,yo+(barHeight+5)*2,(int)((w-pieRadius)*barFill), barHeight,false); |
| 130 |
0 |
} else { |
| 131 |
0 |
g.fill3DRect(xo,yo+(barHeight+5)*2,(int)((w-pieRadius)*barFill), barHeight,true); |
| 132 |
|
} |
| 133 |
|
} |
| 134 |
0 |
g.setColor(Color.BLACK); |
| 135 |
0 |
g.drawString("Carbohydrates: " + (int)carbs +"g / " + (int)carbTarget.getMin() |
| 136 |
|
+ "g (" + Math.round(100*carbs/carbTarget.getMin()) + "%)", |
| 137 |
|
xo+10, yo+(barHeight+5)*2+barHeight/2+fm.getAscent()/2); |
| 138 |
|
|
| 139 |
0 |
Target lipidTarget = user.getTarget(NutrientInfo.getByTableName("lipid")); |
| 140 |
0 |
g.setColor(Color.LIGHT_GRAY); |
| 141 |
0 |
g.fillRect(xo,yo+(barHeight+5)*3,w-pieRadius,barHeight); |
| 142 |
0 |
g.setColor(Color.RED); |
| 143 |
0 |
if (lipid > 0) { |
| 144 |
0 |
barFill = lipid/lipidTarget.getMin(); |
| 145 |
0 |
if (barFill > 1) { |
| 146 |
0 |
barFill = 1; |
| 147 |
0 |
g.setColor(Color.RED.brighter()); |
| 148 |
0 |
g.fill3DRect(xo,yo+(barHeight+5)*3,(int)((w-pieRadius)*barFill), barHeight, false); |
| 149 |
0 |
} else { |
| 150 |
0 |
g.fill3DRect(xo,yo+(barHeight+5)*3,(int)((w-pieRadius)*barFill), barHeight,true); |
| 151 |
|
} |
| 152 |
|
} |
| 153 |
0 |
g.setColor(Color.BLACK); |
| 154 |
0 |
g.drawString("Lipids: " + (int)lipid +"g / " + (int)lipidTarget.getMin() |
| 155 |
|
+ "g (" + Math.round(100*lipid/lipidTarget.getMin()) + "%)", |
| 156 |
|
xo+10, yo+(barHeight+5)*3+barHeight/2+fm.getAscent()/2); |
| 157 |
|
|
| 158 |
0 |
paintPFC(g, xo+(w-pieRadius)+10, yo+(barHeight+5)+10, pieRadius-20); |
| 159 |
0 |
} |
| 160 |
|
|
| 161 |
|
|
| 162 |
|
private void paintPFC(Graphics g, int xo, class="keyword">int yo, class="keyword">int radius) { |
| 163 |
0 |
double total = protein + carbs + lipid; |
| 164 |
0 |
g.setColor(Color.GREEN); |
| 165 |
0 |
int amount = 0; |
| 166 |
0 |
g.fillArc(xo,yo,radius-4,radius-4, amount, (int)(360*(protein/total))); |
| 167 |
0 |
amount += (int)(360*(protein/total)); |
| 168 |
|
|
| 169 |
0 |
g.setColor(Color.BLUE); |
| 170 |
0 |
g.fillArc(xo,yo,radius-4,radius-4, amount, (int)(360*(carbs/total))); |
| 171 |
0 |
amount += (int)(360*(carbs/total)); |
| 172 |
|
|
| 173 |
0 |
g.setColor(Color.RED); |
| 174 |
0 |
g.fillArc(xo,yo,radius-4,radius-4, amount, (int)(360*(lipid/total))); |
| 175 |
0 |
} |
| 176 |
|
|
| 177 |
|
public Dimension getPreferredSize() { |
| 178 |
0 |
return new Dimension(300, 120); |
| 179 |
|
} |
| 180 |
|
} |