Hi so I have tried to put a condition for the program to produce a different result if a certain text did not match what was in the array. This technically works however because processing cannot find the file extension, it crashes my program every time. I wanted to see if there was a way to bypass this. This is my code below for the Processing language.
import controlP5.*;
import gifAnimation.*;
ControlP5 cp5;
String textValue = "";
String typing = "";
Gif loopingGif;
PImage img;
PImage imgbg;
boolean PictureofPlanetcheck = true;
String[] planets = {"earth", "venus", "jupiter", "uranus", "saturn", "neptune", "mercury", "mars", "pluto"};
String filetype =".jpg";
void setup() {
size(480,270);
loopingGif = new Gif(this, "Star-background.gif");
loopingGif.loop();
PFont font = createFont("arial",20);
cp5 = new ControlP5(this);
cp5.addTextfield("textValue")
.setPosition(80,120)
.setSize(200,40)
.setFont(createFont("arial",20))
.setAutoClear(false)
;
cp5.addBang("clear")
.setPosition(300,120)
.setSize(80,40)
.getCaptionLabel().align(ControlP5.CENTER, ControlP5.CENTER)
;
textFont(font);
}
void draw() {
image(loopingGif, 0, height / 2 - loopingGif.height / 2);
text(textValue, 360,180);
String legend1 = "S to show text box and H to hide text box ";
String legend2 = "Enter the name of a planet in the text box";
fill(#FFFFFF);
text(legend1, 60, 30);
text(legend2, 60, 90);
if (!textValue.equals ("")) {
evaluateAnswer();
}
else
{
text(typing, 50, 100);
}
}
public void clear() {
//String picture = textValue + filetype;
cp5.get(Textfield.class,"textValue").clear();
background(loopingGif);
}
void controlEvent(ControlEvent theEvent) {
if(theEvent.isAssignableFrom(Textfield.class)) {
println("controlEvent: accessing a string from controller '"
+theEvent.getName()+"': "
+theEvent.getStringValue()
);
}
}
public void input(String theText) {
// automatically receives results from controller input
println("a textfield event for controller 'input' : "+theText);
}
void keyPressed() {
if (key == 's' || key == 'S') {
cp5.show();
} else if (key == 'h' || key == 'H') {
cp5.hide();
}
}
void evaluateAnswer() {
String[] planets = new String[7];
for (int i = 0; i < planets.length; i++) {
textValue.equals(planets[i]);
String picture = textValue + filetype;
PictureofPlanetcheck = true;
img = loadImage(picture);
if (textValue.equals(planets[i]) == false);
PictureofPlanetcheck = false;
println("Please enter the name of a planet");
background(loopingGif);
img.resize(100,100);
imageMode(CENTER);
image(img, 200, 70);
}
//for (int i = 0; i < planets.length; i++) {
// if (textValue.equals(planets[i]) == false);
// String message = "Please enter the name of a planet";
// println(message);
// }
}
Due to this line of code: String picture = textValue + filetype; it looks for the text written and then a filetype .jpg This as a result makes Processing look for an image all the time.
Would love to know if there is a way to fix this!
Thanks in advance
1