Loading...
Processing Forum
Recent Topics
All Forums
Screen name:
june55936
june55936's Profile
3
Posts
2
Responses
0
Followers
Activity Trend
Last 30 days
Last 30 days
Date Interval
From Date :
To Date :
Go
Loading Chart...
Posts
Responses
PM
Show:
All
Discussions
Questions
Expanded view
List view
Private Message
ControlP5 Logic problem
[1 Reply]
21-Aug-2012 08:28 AM
Forum:
Contributed Library Questions
I have a java file and a processing file.
FoodData.txt looks like this:
ham
23
30
50
50
100
corn
5
10
5
10
15.5
In the function checkForFile(),
println (food[x].toString()) gives the right information as:
ham
23
30
50
50
100
corn
5
10
5
10
15.5
However, when I use the function LIST() to the textarea called searchList.
The information of ham changes into that of the corn like this:
ham
5
10
5
10
15.5
corn
5
10
5
10
15.5...
you need to compare the two in the interactive console, not on the actual window.
import controlP5.*;
ControlP5 controlP5;
//INPUT
Textfield search;
//OUTPUT
Textarea searchList;
Textarea console;
//FOOD
final int maxFood = 100;
private Food []food = new Food[maxFood];
private int numFood = 0;
void setup()
{
size(1000,500);
background(255);
frameRate(30);
controlP5 = new ControlP5(this);
//Search
search = controlP5.addTextfield("",50,50,150,20);
controlP5.addButton("SEARCH",0,210,50,50,20);
searchList = controlP5.addTextarea("lable1","List",50,90,150,300);
searchList.setColorBackground(0xff0066cc);
//List
controlP5.addButton("LIST",1,210,70,50,20);
//Console
console = controlP5.addTextarea("Console","Console",50,400,300,100);
console.setColorBackground(0xff0066cc);
}
void draw()
{
}
//Search Button
public void SEARCH(int theValue) {
console.setText("Searching...");
if(checkForFile()) {
console.setText("Data file located and loaded with "+ numFood + ".");
}
}
//List
public void LIST(){
String total ="";
for (int x = 0; x<numFood;x++)
{
total += food[x].toString() +"\n";
println (food[x].toString());
}
searchList.setText(total);
}
//Check for the validity of database
private boolean checkForFile() {
try {
BufferedReader inFile = createReader("FoodData.txt");
String name;
int x = 0;
double nutrient[] = new double[5];
while((name=inFile.readLine()) != null) {
for (int p = 0; p<5; p++)
{
nutrient[p] = Double.parseDouble(inFile.readLine());
}
food[x] = new Food(name,nutrient);
println(food[x].toString());
x++;
}
numFood = x;
LIST(); //List function
inFile.close();
return true;
}
catch (IOException io) {
println(io.getMessage());
}
return false;
}
Java:
public class Food {
private double info[];
private String name;
public Food(String name,double []info) {
setFoodName(name);
setInfo(info);
}
public String getFoodName() {
return name;
}
public void setFoodName(String name) {
this.name = name;
}
public double[] getInfo() {
return info;
}
public void setInfo(double[] info) {
this.info = info;
}
public String toString() {
String result = "";
result += name +"\n-----------\n"+"Calorie: "+ info[0]+"\n"+
"Fat: "+info[1]+ "\n" + "Cholesterol: "+ info[2] +"\n"+"Sodium: " + info[3] +"\n" +
"Protein: "+ info[4]+"\n";
return result;
}
}
ControlP5 listBox error
[5 Replies]
12-Apr-2012 09:06 PM
Forum:
Contributed Library Questions
import controlP5.*;
ControlP5 controlP5;
Textfield search;
Textarea foodInfo;
ListBox searchBox;
//FOOD
final int Max_Food = 1000;
private Food []f = new Food[Max_Food];
private int num = 0;
void setup() {
size(600,300);
frameRate(30);
controlP5 = new ControlP5(this);
//Buttons
controlP5.addButton("search",0, 410,50, 50, 20);
/*Textarea
foodInfo = controlP5.addTextarea("label1","INFO", 200, 200, 100, 200);
foodInfo.setColorForeground(0xff669933);
foodInfo.setColorBackground(0xff0066cc);*/
//ListBox
searchBox = controlP5.addListBox("after", 100,70,300,200);
searchBox.setItemHeight(20);
searchBox.setBarHeight(20);
searchBox.setHeight(200);
searchBox.captionLabel().set("Search");
searchBox.setColorBackground(color(126,125));
//Textfield
search = controlP5.addTextfield(" ", 100, 50, 300, 20);
}
//ERROR
private boolean checkForFile() {
try {
String name;
int i[] = new int[5];
int x = 0;
println("ok");
BufferedReader inFile = createReader("/Users/JUNE55936/Desktop/IB (2011)/Computer Science/Dossier/DOSSIER_GUI/FoodDataBase.txt");
while ((name=inFile.readLine()) != null) {
i[0] = Integer.parseInt(inFile.readLine());
i[1] = Integer.parseInt(inFile.readLine());
i[2] = Integer.parseInt(inFile.readLine());
i[3] = Integer.parseInt(inFile.readLine());
i[4] = Integer.parseInt(inFile.readLine());
f[x] = new Food(name,i);
println(f[x].toString());
x++;
}
num=x;
inFile.close();
return true;
}
catch(IOException io) {
println(io.getMessage());
}
return false;
}
public void search(int theValue) {
searchBox.clear();
System.out.println("searching");
if (checkForFile()) {
println("Data file located and loaded");
}
String nameSearched = search.getText();
int counter = 0;
int i=0;
for (i = 0; i < num; i++)
{
if (f[i].getFoodName().indexOf(nameSearched) != -1)
{
searchBox.addItem(f[i].getFoodName(),i);
counter++;
}
}
println(counter);
if (counter == 0) {
searchBox.clear();
searchBox.addItem("No data match",100);
}
}
void draw() {
background(50);
}
Java class: Food.java:
public class Food {
private int info[];
private String name;
public int num;
Food(String name,int []info) {
this.name = name;
this.info = info;
}
public String getFoodName(){
return name;
}
public void setFoodName(String name){
this.name = name;
}
public int[] getInfo(){
return info;
}
public void setInfo(int[] info){
this.info = info;
}
public String toString() {
String result = name +"\n"+"cal: "+ info[0]+"\n"+
"fat: "+info[1]+ "\n" + "chol: "+ info[2] +"\n"+"sod: " + info[3] +"\n" +
"prot: "+ info[4];
return result;
}
}
The database I read text from looks like this:
ham
90
90
49
20
10
ham
102
402
405
304
30
ham
90
48
80
59
40
When you type "ham" and press "search" and press "ham" and "search" then I get this error:
Apr 13, 2012 10:45:52 AM controlP5.ControlBroadcaster printMethodError
SEVERE: An error occured while forwarding a Controller value
to a method in your program. Please check your code for any
possible errors that might occur in this method .
e.g. check for casting errors, possible nullpointers, array overflows ... .
method: search
exception: java.lang.reflect.InvocationTargetException
Also if I search"ham" and search"corn", it gives that error too.
Please Help!
ControlP5 listBox addItems error
[1 Reply]
12-Apr-2012 08:53 AM
Forum:
Contributed Library Questions
Code:
import controlP5.*;
ControlP5 controlP5;
Textfield search;
Textarea foodInfo;
ListBox searchBox;
//FOOD
final int Max_Food = 1000;
private Food []f = new Food[Max_Food];
private int num = 0;
void setup() {
size(600,300);
frameRate(30);
controlP5 = new ControlP5(this);
//Buttons
controlP5.addButton("search",0, 410,50, 50, 20);
/*Textarea
foodInfo = controlP5.addTextarea("label1","INFO", 200, 200, 100, 200);
foodInfo.setColorForeground(0xff669933);
foodInfo.setColorBackground(0xff0066cc);*/
//ListBox
searchBox = controlP5.addListBox("after", 100,70,300,200);
searchBox.setItemHeight(20);
searchBox.setBarHeight(20);
searchBox.setHeight(200);
searchBox.captionLabel().set("Search");
searchBox.setColorBackground(color(126,125));
//Textfield
search = controlP5.addTextfield(" ", 100, 50, 300, 20);
}
//ERROR
private boolean checkForFile() {
try {
String name;
int i[] = new int[5];
int x = 0;
println("ok");
BufferedReader inFile = createReader("/Users/JUNE55936/Desktop/IB (2011)/Computer Science/Dossier/DOSSIER_GUI/FoodDataBase.txt");
while ((name=inFile.readLine()) != null) {
i[0] = Integer.parseInt(inFile.readLine());
i[1] = Integer.parseInt(inFile.readLine());
i[2] = Integer.parseInt(inFile.readLine());
i[3] = Integer.parseInt(inFile.readLine());
i[4] = Integer.parseInt(inFile.readLine());
f[x] = new Food(name,i);
println(f[x].toString());
x++;
}
num=x;
inFile.close();
return true;
}
catch(IOException io) {
println(io.getMessage());
}
return false;
}
public void search(int theValue) {
searchBox.clear();
System.out.println("searching");
if (checkForFile()) {
println("Data file located and loaded");
}
String nameSearched = search.getText();
int counter = 0;
int i=0;
for (i = 0; i < num; i++)
{
if (f[i].getFoodName().indexOf(nameSearched) != -1)
{
searchBox.addItem(f[i].getFoodName(),i);
counter++;
}
}
println(counter);
if (counter == 0) {
searchBox.clear();
searchBox.addItem("No data match",100);
}
}
void draw() {
background(50);
}
Food.java code:
public class Food {
private int info[];
private String name;
public int num;
Food(String name,int []info) {
this.name = name;
this.info = info;
}
public String getFoodName(){
return name;
}
public void setFoodName(String name){
this.name = name;
}
public int[] getInfo(){
return info;
}
public void setInfo(int[] info){
this.info = info;
}
public String toString() {
String result = name +"\n"+"cal: "+ info[0]+"\n"+
"fat: "+info[1]+ "\n" + "chol: "+ info[2] +"\n"+"sod: " + info[3] +"\n" +
"prot: "+ info[4];
return result;
}
}
This is my code and the text file that works as a database looks like this:
ham
20
10
20
40
50
ham
20
10
20
40
50
ham
20
10
20
40
50
I am having an error if I put "ham" in the textfield and press "Search" many times.
Also, I am having an error when I put "ham" and press "Search" and later put "corn" and press "Search".
I am having an error on addItems methods, but I don't know why....
My console says:
Apr 13, 2012 10:45:52 AM controlP5.ControlBroadcaster printMethodError
SEVERE: An error occured while forwarding a Controller value
to a method in your program. Please check your code for any
possible errors that might occur in this method .
e.g. check for casting errors, possible nullpointers, array overflows ... .
method: search
exception: java.lang.reflect.InvocationTargetException
PLEASE HELP!!!
I
«Prev
Next »
Moderate user : june55936
Forum