ControlP5 ListBoxItem won't show text
in
Programming Questions
•
1 year ago
I'm writing my first Processing program for a class project that is due in 4 days. So far I think I've done pretty well and am getting much more comfortable with Processing. I do however have one hang up at this moment, and it won't allow me to continue programming essential parts of my program, because I need to see what filters I'm enabling via labels of entries in a ListBox.
What I want the ListBox to do is to list ListBoxItems with the names of countries that I have stored in a String[]. I also want the ListBoxItems to be color coded. I already have the array of colors that I want, and they load up just fine into the listboxitems, but for some reason the text will not show up! I've tried everything!
I'm confused because I can load ControlP5's example code of a listbox just fine and the text on the ListBoxItems shows up perfectly. I've scanned my code for hours and can't figure this out. This is my first time posting on here, so I'm not sure if I should post my whole code for the main program or not... but here it is!
Thanks for any help.
Here are my my property initializations, setup(), and draw() methods:
import controlP5.*;
ControlP5 cp5;
//Listbox Properties
ListBox l;
int cnt = 0;
Range range;
int displayResolutionX = 1366;
int displayResolutionY = 768;
PImage bg;
//Need Array of Colors, pertaining to Country.
EntryTable data;
String[] arrayOfGraphs = new String[6];
String[] arrayOfFileNames = new String[6];
String[] unitsOfMeasure = new String[6];
float[][] arrayOfSmallestAreas; //holds country's index value within the original data table in column 1 and the total of that respective country's data
float dataMin, dataMax;
float graphMax;
/*
*
*Position variables intialized */
float graphPlotX1, graphPlotY1;//coordinate 1 of the graph table itself
float graphPlotX2, graphPlotY2;// coordinate 2 of the graph table itself
float graphLabelPosX, graphLabelPosY; //position variables for label of currently open graph
int filterGraphPlotX1, filterGraphPlotY1;//coordinate 1 of the graph table itself
int filterGraphPlotX2, filterGraphPlotY2;// coordinate 2 of the graph table itself
float filterLabelX, filterLabelY;
float axisLabelX, axisLabelY;
float[] tabLeft, tabRight; //holds left and right values, respectively, of all 6 graph tabs
float tabTop, tabBottom; //holds values of tab tops (unselected) and all tab bottoms
float selectedTabTop;// holds value of selected tabs height (it is taller than unselected tabs)
float tabPad = 10;
Integrator[][] interpolators; // Changed this to a two dimensional array because the interpolation will occur from graph to graph,
//where in each country(row) has multiple values; 1 per year(col) 1980 - 2009
/*
*
*Variables to hold count */
int rowCount;
int columnCount;
int currentGraph = 0;
/*
*
*Axis Unit Variables */
int yearMin, yearMax;
int[] years;
String[] countries;
boolean[] showCountries;
color[] colorsOfCountries;
int yearInterval = 5;
int roundAway = 10;
int yAxisMeasureMajor;
int yAxisMeasureMinor;
/*
*
*Graph Label Variables */
PFont plotFont; //font for in graph plot point text
PFont tabFont; //font for tabs
PFont axisUnitFont; //for numbers on axis
PFont axisLabelFont; //font for X & Y Labels
PFont countryFont; //font for the countries filter
void setup() {
size(displayResolutionX, displayResolutionY);
colorMode(HSB, 360, 100, 100);
frameRate(30);
smooth();
bg = loadImage("Gradient_Background.jpg");
bg.resize(displayResolutionX, displayResolutionY);
image(bg, 0, 0);
arrayOfGraphs[0] = "Total Primary\nEnergy Production";
arrayOfGraphs[1] = "Total Primary\nEnergy Consumption";
arrayOfGraphs[2] = "Total Primary\nEnergy Consumption\nper Capita";
arrayOfGraphs[3] = "Total CO2 Emissions\nfrom the Consumption\nof Energy";
arrayOfGraphs[4] = "Per Capita CO2\nEmissions from the\nConsumption of Energy";
arrayOfGraphs[5] = "Total Renewable\nElectricity Net\nGeneration";
arrayOfFileNames[0] ="Total_Primary_Energy_Production_(Quadrillion_Btu).txt";
arrayOfFileNames[1] ="Total_Primary_Energy_Consumption_(Quadrillion_Btu).txt";
arrayOfFileNames[2] ="Total_Primary_Energy_Consumption_per_Capita_(Million_Btu_per_Person).txt";
arrayOfFileNames[3] ="Total_Carbon_Dioxide_Emissions_from_the_Consumption_of_Energy_(Million_Metric_Tons).txt";
arrayOfFileNames[4] ="Per_Capita_Carbon_Dioxide_Emissions_from_the_Consumption_of_Energy_(Metric_Tons_of_Carbon_Dioxide_per_Person).txt";
arrayOfFileNames[5] ="Total_Renewable_Electricity_Net_Generation_(Billion_Kilowatthours).txt";
unitsOfMeasure[0] ="Quadrillion\nBtu";
unitsOfMeasure[1] ="Quadrillion\nBtu";
unitsOfMeasure[2] ="Million\nBtu\nper Person";
unitsOfMeasure[3] ="Million\nMetric\nTons";
unitsOfMeasure[4] ="Metric Tons\nof CO2\nper Person";
unitsOfMeasure[5] ="Billion\nKilowatt\nHours";
background(315);
//Define important data members first:
data = new EntryTable("Total_Primary_Energy_Production_(Quadrillion_Btu).txt");
rowCount = data.getRowCount();
columnCount = data.getColumnCount();
years = int(data.getColumnNames());
yearMin = years[0];
yearMax = years[columnCount - 1];
countries = data.getRowNames();
dataMin = 0;
dataMax = ceil(data.getTableMax() / roundAway) * roundAway;
graphMax = dataMax + (dataMax * 0.1);
showCountries = new boolean[countries.length];
colorsOfCountries= new color[rowCount];
colorCodeCountries();
data.calculateSortSumData();
arrayOfSmallestAreas = data.sumData;
//Sets up initial Integrators for the default graph thats loaded
interpolators = new Integrator[rowCount][columnCount];
for(int row = 0; row < rowCount; row++){
for(int col = 0; col < columnCount; col++){
float initialValue = float(data.getEntry(row, col));
interpolators[row][col] = new Integrator(initialValue);
interpolators[row][col].attraction = 0.1; //Set lower than default
showCountries[row] = true;
}
}
configureYMarkers();
plotFont = createFont("Gulim", 8);
tabFont = createFont("Gulim", 12);
axisUnitFont = createFont("Cordia New", 24);
axisLabelFont = createFont("Euphimia", 30);
countryFont = createFont("Gulim", 20);
graphPlotX1 = width * 0.35;
graphPlotX2 = width - (0.01 * width);
axisLabelY = graphPlotX1 - (0.08 *width);
graphPlotY1 = 60;
graphPlotY2 = height - 120;
axisLabelX = height - 25;
filterGraphPlotX1 = int(.01 * width);
filterGraphPlotY1 = int(graphPlotY1 - (.03 * width));
filterGraphPlotX2 = int(graphPlotX1 - (.15 * width));
filterGraphPlotY2 = int(graphPlotY2);
filterLabelX = filterGraphPlotX1 + 10;
filterLabelY = filterGraphPlotY1 + 10;
cp5 = new ControlP5(this);// TEXT WONT POPULATE THE LISTBOXITEMS!!!!!!!
l = cp5.addListBox("myList")
.setPosition(filterGraphPlotX1, (filterGraphPlotY1 + 35))
.setSize((filterGraphPlotX2 - filterGraphPlotX1), (filterGraphPlotY2 - filterGraphPlotY1))
.setItemHeight(20)
.setBarHeight(35)
.setColorBackground(color(250))
.setColorActive(color(330))
;
l.captionLabel().toUpperCase(true);
l.captionLabel().set("Filter by Country:");
l.captionLabel().setColor(#020208);
l.captionLabel().style().marginTop = 3;
l.valueLabel().style().marginTop = 3;
for(int i = 0; i < rowCount; i++){
ListBoxItem lbi = l.addItem(countries[i], i);
lbi.setColorBackground(colorsOfCountries[i]);
}
range = cp5.addRange("rangeController")
// disable broadcasting since setRange and setRangeValues will trigger an event
.setBroadcast(false)
.setPosition(graphPlotX1, (graphPlotY2+45))
.setSize(int(graphPlotX2 - graphPlotX1),10)
.setHandleSize(20)
.setRange(0, 29)
.setRangeValues(0,29)
// after the initialization we turn broadcast back on again
.setBroadcast(true)
.setColorForeground(color(75,360))
.setColorBackground(color(180,180))
.setNumberOfTickMarks(30)
;
}
// println(PFont.list());
/*
println("Number of years: " + years.length );
println("Here are the contents of the array: ");
for(int i = 0; i < years.length; i++){
print(years[i] + "\t");
} */
/*
*
*Check the contents of table currently held in 'data'
println("Number of Rows: " + rowCount + ", Number of columns: " + columnCount);
println("Here are the contents of the table: ");
for(int i = 0; i < rowCount; i++){
println("Row " + (i) + ": " + countries[i]); */
void draw(){
background(315);
noStroke();
fill(350);
rectMode(CORNERS);
rect(graphPlotX1, graphPlotY1, graphPlotX2, graphPlotY2);
drawTitleTabs();
drawAxisLabels();
for(int row = 0; row <rowCount; row++){
for(int col = 0; col < columnCount; col++){
interpolators[row][col].update();
}
}
drawYearLabels();
drawDataArea();
drawVerticalAndHorizontalLabels();
}
void drawColorMode(){
fill(0, 100, 100);
rect(filterGraphPlotX1, filterGraphPlotY1 - 10, filterGraphPlotX2, filterGraphPlotY1);
fill(100, 100, 100);
rect(filterGraphPlotX1, filterGraphPlotY1, filterGraphPlotX2, filterGraphPlotY1+50);
fill(1, 100, 100);
rect(filterGraphPlotX1, filterGraphPlotY1+50, filterGraphPlotX2, filterGraphPlotY1+100);
fill(300, 100, 100);
rect(filterGraphPlotX1, filterGraphPlotY1+100, filterGraphPlotX2, filterGraphPlotY1+150);
fill(400, 100, 100);
rect(filterGraphPlotX1, filterGraphPlotY1+150, filterGraphPlotX2, filterGraphPlotY1+200);
fill(500, 100, 100);
rect(filterGraphPlotX1, filterGraphPlotY1+200, filterGraphPlotX2, filterGraphPlotY1+250);
fill(600, 100, 100);
rect(filterGraphPlotX1, filterGraphPlotY1+250, filterGraphPlotX2, filterGraphPlotY1+300);
fill(700, 100, 100);
rect(filterGraphPlotX1, filterGraphPlotY1+300, filterGraphPlotX2, filterGraphPlotY1+350);
fill(800, 100, 100);
rect(filterGraphPlotX1, filterGraphPlotY1+350, filterGraphPlotX2, filterGraphPlotY1+400);
fill(900, 100, 100);
rect(filterGraphPlotX1, filterGraphPlotY1+400, filterGraphPlotX2, filterGraphPlotY1+450);
}
/*
*
*Label Drawing Methods */
void drawAxisLabels() { //used to be drawVolumeLabels()
fill(0);
textFont(axisUnitFont);
stroke(0);
strokeWeight(1);
for (float v = dataMin; v <= graphMax; v += yAxisMeasureMinor) {
if (v % yAxisMeasureMinor == 0) { // If a tick mark
float y = map(v, dataMin, graphMax, graphPlotY2, graphPlotY1);
if (v % yAxisMeasureMajor == 0) { // If a major tick mark
if (v == dataMin) {
textAlign(RIGHT); // Align by the bottom
} else if (v == dataMax) {
textAlign(RIGHT, TOP); // Align by the top
} else {
textAlign(RIGHT, CENTER); // Center vertically
}
text(floor(v), graphPlotX1 - 15, y - 30); //Write out unit measurements
strokeWeight(2);
stroke(0);
line(graphPlotX1 - 15, y-1, graphPlotX1-1, y-1); // Draw major tick
line(graphPlotX1 - 25, y - 15, graphPlotX1 - 16, y-1);
} else {
// Commented out, too distracting visually
//line(graphPlotX1 - 2, y, graphPlotX1, y); // Draw minor tick
}
}
}
}
void configureYMarkers()
{
yAxisMeasureMajor = (ceil(graphMax / 5 ) / 10) * 10; //Want 5 major ticks in the vertical graph
yAxisMeasureMinor = yAxisMeasureMajor / 2;
}
/*
void drawTitle(String titleOfCurrentGraph) { //Must call this method whenever you define which graph to look at.
fill(0);
textSize(20);
textAlign(LEFT);
String title = titleOfCurrentGraph;
text(title, graphPlotX1, graphPlotY1 - 10);
} */
void drawVerticalAndHorizontalLabels(){ //Used to be drawAxisLabels()
smooth();
fill(0);
textFont(axisLabelFont);
textLeading(30);
textAlign(CENTER, CENTER);
pushMatrix();
rotate(-PI/6);
translate(120, 510);
text(unitsOfMeasure[currentGraph], 0, 0);// axisLabelY, (graphPlotY1+graphPlotY2)/2); //Write out Y axis label
popMatrix();
textAlign(CENTER);
text("Year", (graphPlotX1+graphPlotX2)/2, axisLabelX);
smooth();
}
void drawYearLabels() {
smooth();
fill(0);
textFont(axisUnitFont);
textAlign(CENTER, TOP);
// Use thin, gray lines to draw the grid
stroke(224);
strokeWeight(1);
for (int col = 0; col < columnCount; col++) {
if (years[col] % yearInterval == 0 || col == (columnCount - 1)) {
float x = map(years[col], yearMin, yearMax, graphPlotX1, graphPlotX2);
text(years[col], x, graphPlotY2 + 10);
if(col != 0 && col != years.length - 1){
stroke(0);
strokeWeight(1);
line(x, graphPlotY1 - 1, x, graphPlotY2 - 1);
}
stroke(0);
strokeWeight(2);
line(x, graphPlotY2, x, graphPlotY2 +5);
}
}
}
void drawDataHighlight(int lineSelected) { // How will I pass the value of the line I want to highlight, back to this method?
textFont(plotFont);
for (int col = 0; col < columnCount; col++) {
if (data.isValid(lineSelected, col)) {
String value = data.getEntry(lineSelected, col);
float x = map(years[col], yearMin, yearMax, graphPlotX1, graphPlotX2); //check years[col] dont know if this is right
float y = map(float(value), dataMin, dataMax, graphPlotY2, graphPlotY1);
if (dist(mouseX, mouseY, x, y) < 5) {
// dist returns the distance between two points
// dist(x1, y1, x2, y2);
// dist(x1, y1, z1, x2, y2, z2);
strokeWeight(10);
point(x, y);
fill(0);
textAlign(CENTER);
text(nf(float(value), 0, 3) + " (" + years[col] + ")", x, y-8);
textAlign(LEFT);
}
}
}
}
void drawDataArea(){
smooth();
int countryToGraph;
for(int row = 0; row < rowCount - 1 /*&& showCountries[row]*/; row++){
color cyclingColor = colorsOfCountries[row];
countryToGraph = int(arrayOfSmallestAreas[row][0]);//This holds the index of the country with the current smallest area
strokeWeight(1);
fill(cyclingColor);
beginShape();
for(int col = 0; col < columnCount; col++){
if(showCountries[col] && data.isValid(countryToGraph, col)){
float value = interpolators[countryToGraph][col].value;
float x = map(years[col], yearMin, yearMax, graphPlotX1, graphPlotX2);
float y = map(value, dataMin, graphMax, graphPlotY2, graphPlotY1);
curveVertex(x, y);
if((col == 0) || (col == columnCount -1)){
curveVertex(x, y);
}
}
}
//draw the lower-right and lower-left corners
vertex(graphPlotX2, graphPlotY2);
vertex(graphPlotX1, graphPlotY2);
endShape(CLOSE);
}
}
void drawTitleTabs() {
rectMode(CORNERS);
noStroke();
textFont(tabFont);
textAlign(LEFT, BOTTOM);
// On first use of this method, allocate space for an array
// to store the values for the left and right edges of the tabs
if (tabLeft == null) {
tabLeft = new float[arrayOfGraphs.length];
tabRight = new float[arrayOfGraphs.length];
}
float runningX = graphPlotX1;
tabTop = graphPlotY1 - textAscent() - 30;
tabBottom = graphPlotY1;
for (int col = 0; col < arrayOfGraphs.length; col++) {
String title = arrayOfGraphs[col];
tabLeft[col] = runningX;
float titleWidth = textWidth(title);
tabRight[col] = tabLeft[col] + tabPad + titleWidth + tabPad;
// If the current tab, set its background white, otherwise use pale gray
fill(col == currentGraph ? 360 : 340);
rect(tabLeft[col], tabTop, tabRight[col], tabBottom);
// If the current tab, use black for the text, otherwise use dark gray
fill(col == currentGraph ? 0 : 100);
text(title, runningX + tabPad, graphPlotY1 - 10);
runningX = tabRight[col];
}
}
void drawFilterTabs(){
}
void mousePressed() {
if(mouseY > tabTop && mouseY < tabBottom){ //shouldn't this be reversed?
for(int col = 0; col < arrayOfGraphs.length; col++){
if(mouseX > tabLeft[col] && mouseX < tabRight[col]){
setGraph(col);
}
}
}
}
void mouseDragged(){ //Use to dataHighlight(), can choose each line seperately from others?
}
void setGraph(int graphIndex){
currentGraph = graphIndex;
data = new EntryTable(arrayOfFileNames[graphIndex]); //Assign 'data' to a graph file within your sketchbook directory
updateGraphProperties();
background(315);
for(int row = 0; row < rowCount; row++) {
for(int col = 0; col < columnCount; col++){
interpolators[row][col].target(float(data.getEntry(row, col)));
}
}
}
void updateGraphProperties(){
rowCount = data.getRowCount();
columnCount = data.getColumnCount();
// countriesByRegions[] = new String[rowCount];
dataMin = 0;
dataMax = ceil(data.getTableMax() / roundAway) * roundAway;
graphMax = dataMax + (0.1 * dataMax);
configureYMarkers();
}
void colorCodeCountries(){
colorsOfCountries = new color[rowCount];
color holderColor;
for(int i = 0 ; i < rowCount; i++)
{
if( i >= 0 && i < 7){ //North America GREEN (115, 99, 90) Saturation: 99 ->40
float saturation = map(i, 0, 6, 55, 99);
float hue = map(i, 0, 6, 136, 77);
float brightness = map(i, 0, 6, 90, 99);
holderColor = color(hue, saturation, brightness);
colorsOfCountries[i] = holderColor;
}
else if( i == 7 || ( i >= 9 && i < 53)){ //South and Central America Cyan
float saturation = map(i, 7, 52, 99, 50);
float hue =ceil( map(i, 7, 52, 185, 155));
float brightness =ceil(map(i, 7, 52, 99, 75));
holderColor = color(hue, saturation, brightness);
colorsOfCountries[i] = holderColor;
}
else if( i == 8){//Antarcitca Yellow
holderColor = color(56, 99, 94);
colorsOfCountries[i] = holderColor;
}
else if( i >= 53 && i < 95){//Europe PINK
float saturation = map(i, 53, 94, 99, 20);
float hue =ceil( map(i, 53, 94, 340, 300));
float brightness =ceil(map(i, 53, 94, 99, 75));
holderColor = color(hue, saturation, brightness);
colorsOfCountries[i] = holderColor;
}
else if( i >= 95 && i < 112){ //Eurasia PURPOLE
float saturation = map(i, 95, 111, 99, 50);
float hue = ceil(map(i, 95, 111, 290, 265));
float brightness = ceil(map(i, 95, 111, 99, 90));
holderColor = color(hue, saturation, brightness);
colorsOfCountries[i] = holderColor;
}
else if( i >= 112 && i < 127){ //Middle East ORANGE
float saturation = map(i, 112, 126, 99, 70);
float hue =ceil( map(i, 112, 126, 40, 20));
float brightness =ceil(map(i, 112, 126, 99, 90));
holderColor = color(hue, saturation, brightness);
colorsOfCountries[i] = holderColor;
}
else if (i>=127 && i <184){ // Africa BLUE
float saturation = map(i, 127, 183, 99, 60);
float hue =ceil( map(i, 127, 183, 240, 210));
float brightness =ceil(map(i, 127, 183, 99, 70));
holderColor = color(hue, saturation, brightness);
colorsOfCountries[i] = holderColor;
}
else if (i >= 184 && i < 231){ //Asia & Oceania RED
float saturation = map(i, 184, 230, 99, 70);
float hue =ceil( map(i, 184, 230, 15, 1));
float brightness =ceil(map(i, 184, 230, 99, 60));
holderColor = color(hue, saturation, brightness);
colorsOfCountries[i] = holderColor;
}
else{ //World GREY
colorsOfCountries[i] = color(200);
}
}
}
void filterAll(){
for(int i = 0; i < rowCount; i++){
showCountries[i] = false;
}
}
void filterNone(){
for(int i = 0; i < rowCount; i++){
showCountries[i] = true;
}
}
1