I want to have text printed out on the condition that the mouse positions hover over a certain coordinate range for both x and y coordinates. However, the text can not print even if I state in the code that the text should be printed if its on a certain coordinate range. Can anyone offer me some advice towards what I should be doing? Any help can be appreciated.
The code to print the text is after the "// Gamma" comment
PImage[] imgs;
String[] greekAlphabet = {"Gamma", "Zeta", "Eta", "Kappa", "Lambda", "Phi", "Psi"};
void setup(){
background(0);
size(500, 500);
textSize(50);
textAlign(CENTER, CENTER);
imgs = new PImage[greekAlphabet.length];
for (int i = 0; i < imgs.length; i++){
imgs[i] = loadImage(greekAlphabet[i]+".png");
}
text("Press Any Key", width/2, height/2);
}
void draw(){
if (keyPressed){
background(0);
text("Choose any clan", width/2, height/2 - 180);
for (int i = 0; i < imgs.length; i++){
if (i >= 0 && i <= 3){
image(imgs[i], (((i)%5 * 120)), 120, 150, 150);
}
else if (i >=4 && i <= 6){
image(imgs[i], (55+((i)%4 * 120)), 310, 150, 150);
}
}
// Gamma
if (overImage(25, 120, 140, 250)){
text("Gamma", width/2, height/2);
}
}
println(mouseX + " " + mouseY);
}
boolean overImage(int x1, int x2, int y1, int y2){
if ((mouseX >= x1 && mouseX <= x2) && (mouseY >= y1 && mouseY <= y2)){
return true;
}
else{
return false;
}
}
As noted above the if(keyPressed) needs to be removed. The following works on my system. I changed the images to some that I had for testing purposes; you'll need to change them back for your setup. I also added an extra display function to simplify things a little. There is likely more optimization that could be done. Make sure that the _imgW and _imgH values are correct for your images.
PImage[] imgs;
String[] greekAlphabet = {"Gamma", "Omega", "Delta", "Alpha", "Sigma", "Phi", "Psi"};
final int _imgW = 150;
final int _imgH = 150;
void displayImages() {
text("Choose any clan", width/2, height/2 - 180);
for (int i = 0; i < imgs.length; i++) {
if (i >= 0 && i <= 3) {
image(imgs[i], (((i)%5 * 120)), 120, _imgW, _imgH);
} else if (i >=4 && i <= 6) {
image(imgs[i], (55+((i)%4 * 120)), 310, _imgW, _imgH);
}
}
}
void setup() {
background(0);
size(600, 500);
textSize(30);
textAlign(CENTER, CENTER);
imgs = new PImage[greekAlphabet.length];
for (int i = 0; i < imgs.length; i++) {
imgs[i] = loadImage(greekAlphabet[i]+".png");
}
}
void draw() {
background(0);
displayImages();
if (overImage(0, 120)) {
text("Gamma", 120, 20);
}
if (overImage(120, 120)) {
text("Omega", 120, 20);
}
if (overImage(240, 120)) {
text("Delta", 120, 20);
}
if (overImage(360, 120)) {
text("Alpha", 120, 20);
}
if (overImage(55, 310)) {
text("Sigma", 120, 20);
}
if (overImage(175, 310)) {
text("Phi", 120, 20);
}
if (overImage(245, 310)) {
text("Psi", 120, 20);
}
println("x = ", mouseX + " : " + "y =", mouseY);
}
boolean overImage(int x, int y) {
if (mouseX >= x && mouseX <= x+_imgW && mouseY >= y && mouseY <= y+_imgH) {
return true;
} else {
return false;
}
}