-
Notifications
You must be signed in to change notification settings - Fork 0
/
Statistics.java
151 lines (146 loc) · 6.27 KB
/
Statistics.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;
import java.awt.Font;
/**
* The statistics display for a given base. The display information includes the population, food, and weapons
* inside the base. The statstics can be made visible by clicking the bar, which will be created to be on top
* of their respective bases
*
* @author Marco Ly, Ahrenn Sivananthan
* @version Oct 2014
*/
public class Statistics extends DisplayBars
{
// Initializing instance variables
private int population; // the population of the base
private int food; // the food supply of the base
private int weapons; // the weapons supply of the base
private Color backgroundColor;
private Color textColorPop;// population value's color
private Color textColorFood; // food value's color
private Color textColorWeap; // weapons value's color
private Font textFont;
private GreenfootImage stats; // the base image that all other images will be drawn onto
private GreenfootImage background; // the background of the statistics
private GreenfootImage foodIcon; // the image of the food icon
private GreenfootImage weaponsIcon; // the image of the weapons icon
private GreenfootImage populationIcon; // the image of a scout that represents the population
private GreenfootImage foodValue; // the amount of food of the base
private GreenfootImage weaponsValue; // the amount of weapons in the base
private GreenfootImage populationValue; // the total population in the base
public Statistics (int population, int food, int weapons) {
// setting starting values
isShowing = false;
this.population = population;
this.food = food;
this.weapons = weapons;
// setting the unchanging images
stats = new GreenfootImage(234, 125);
background = new GreenfootImage(234,125);
foodIcon = new GreenfootImage("Food Icon.png");
weaponsIcon = new GreenfootImage("Weapon Icon.png");
populationIcon = new GreenfootImage("Default Scout.png");
// setting the images that will be changed along with their fonts and colors
textColorPop = new Color(0, 255, 100);
textColorFood = new Color(0, 255, 100);
textColorWeap = new Color(255, 0, 0);
textFont = new Font("Courrier", Font.BOLD, 20);
populationValue = new GreenfootImage("= " + population, 20, textColorPop, null);
foodValue = new GreenfootImage("= " + food, 20, textColorFood, null);
weaponsValue = new GreenfootImage("= " + weapons, 20, textColorWeap, null);
populationValue.setFont(textFont);
foodValue.setFont(textFont);
weaponsValue.setFont(textFont);
// filling the background
backgroundColor = new Color(255, 255, 255);
background.setColor(backgroundColor);
background.setTransparency(125);
background.fill();
// drawing all images onto the basic stats image
stats.drawImage(background, 0, 0);
stats.drawImage(populationIcon, 57, 8);
stats.drawImage(foodIcon, 57, 51);
stats.drawImage(weaponsIcon, 57, 94);
stats.drawImage(populationValue, 96, 8);
stats.drawImage(foodValue, 96, 51);
stats.drawImage(weaponsValue, 96, 94);
// hiding the stats image initially and setting the Statistics object to be stats
stats.setTransparency(0);
this.setImage(stats);
}
/**
* Act - do whatever the Statistics wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
showStatistics();
updateImage();
}
/**
* Mutator that updates the display with all information from their base
*
* @param population the population value given by the base
* @param food the food value given by the base
* @param weapons the weapons value given by the base
*/
public void updateValues (int population, int food, int weapons) {
this.population = population;
this.food = food;
this.weapons = weapons;
if (population <= 10) {
textColorPop = new Color(255, 0, 0);
} else {
textColorPop = new Color(0, 255, 100);
}
if (food <= 25) {
textColorFood = new Color(255, 0, 0);
} else {
textColorFood = new Color(0, 255, 100);
}
if (weapons <= 2) {
textColorWeap = new Color(255, 0, 0);
} else {
textColorWeap = new Color(0, 255, 100);
}
}
/**
* Makes the stats image visible or invisible when clicked on by the mouse
*/
private void showStatistics() {
if(Greenfoot.mouseClicked(this) && isShowing == true) {
stats.setTransparency(0); // makes stats invisible
isShowing = false;
} else if (Greenfoot.mouseClicked(this) && isShowing == false) {
stats.setTransparency(255); // makes stats fully visible
isShowing = true;
}
}
/**
* Updates the object's image with any new values by clearing previous images and updating them with the new values
*/
private void updateImage() {
// Clearing all images
populationValue.clear();
foodValue.clear();
weaponsValue.clear();
stats.clear();
// Re-setting the changing images along with their fonts
populationValue = new GreenfootImage("= " + population, 20, textColorPop, null);
foodValue = new GreenfootImage("= " + food, 20, textColorFood, null);
weaponsValue = new GreenfootImage("= " + weapons, 20, textColorWeap, null);
populationValue.setFont(textFont);
foodValue.setFont(textFont);
weaponsValue.setFont(textFont);
// Re-draws all images onto the stats image
stats.drawImage(background, 0, 0);
stats.drawImage(populationIcon, 57, 8);
stats.drawImage(foodIcon, 57, 51);
stats.drawImage(weaponsIcon, 57, 94);
stats.drawImage(populationValue, 96, 8);
stats.drawImage(foodValue, 96, 51);
stats.drawImage(weaponsValue, 96, 94);
// Sets the Statistics object image to the stats image
this.setImage(stats);
}
}