Loading...
Logo
Processing Forum
hi 
im just starting out in processing ( making a data visualization) and having some trouble with the if mousepressed function with text and having it display an image. Basically, i want the user to be able to click on a piece of text and have a certain image appear. Any help with what I am doing wrong would be great... heres my code


PImage blue;  
PImage red; 
PImage green;
PImage orange;
PImage yellow;
PImage original;
void setup() {
  size(800,800);
 // f = loadFont("Calibri-48.vlw");

  // Make a new instance of a PImage by loading an image file
  blue = loadImage("bluemap.gif");
  image(blue,0,0);
  blue.resize(800,0);
  image(blue,0,0);
  red = loadImage("redmap.gif");
  image(red,0,0);
  red.resize(800,0);
  image(red,0,0);
  green = loadImage("greenmap.gif");
  image(green,0,0);
  green.resize(800,0);
  image(green,0,0);
  orange = loadImage("orangemap.gif");
  image(orange,0,0);
  orange.resize(800,0);
  image(orange,0,0);
  yellow = loadImage("yellowmap.gif");
  image(yellow,0,0);
  yellow.resize(800,0);
  image(yellow,0,0);
  original = loadImage("originalmap.png");
  image(original,0,0);
  original.resize(800,0);
  image(original,0,0);
  
}

void draw() {
  background(0);
  // Draw the image to the screen at coordinate (0,0)
  image(original,0,0);

 // legend 
 textSize(15);
 // textFont(f,48); 
 textAlign(CENTER); // position of the text at the center
  fill(240,15,15);
 text("white",60,540);
   fill(5,40,230);
 text("black",60,560);
   fill(20,120,10);
 text("asian",60,580);
  fill(240,130,15);
 text("hispanic",60,600);
  fill(250,235,10);
 text("other",60,620); 
 if ( mousePressed == text("black",60,540)) {
   image(blue,0,0);
 }
}

Replies(4)

Check for mouse coordinates instead
 
mousePressed()
{
x=mouseX;
y=mouseY;
 
if(x>xYouWant&&x<xYouWant+Shift&&y>yYouWant&&y<yYouWant+Shift)
{
DoWhatYouNeedHere();
}
}
Also
mousePressed() is an event driven function and does not need to be inside void daw()
edit: ops... late :)

MousePressed is a boolean, either true or false it can't be compared to a method. What you are trying to do is to check the mouse position when it  is clicked.  So you gotta do it :) Something like:

untested

if ( mousePressed  &&  mouseX > leftLImit  && mouseX < rightLimit &&  mouseY > topLimit && mouseY < bottomLimit){
do something.

Where right, left top and bottom limits are the bounding box of the text you have draw.
For this the textWidth() ,   textAscent() and textDescent() are very handy. 

Also you can do check inside the mousePressed(){} the method instead of using mousePressed the boolean.