We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hello! So I have 3 images displayed on a screen and am trying to get it so when I roll over an image it becomes tinted. I thought that I had the correct code but when I implement the section of the code that is supposed to shade my images, none of my images show up. Without the tinting portion of the code, the images do show up. Can anybody help me with this problem? Posted below is my code for the project. The problem deals with main, the Images class, and the population class, but I have included the rest of the classes for the project as well.
Main:
Images photoimg; population population; Button button;
void settings() { //Setup the box containing the images size(1600,600); }
void setup() { colorMode(RGB,1.0); int popmax = 3; //float mutationRate = 0.05; // A pretty high mutation rate here, our population is rather small we need to enforce variety // Create a population with a target phrase, mutation rate, and population max //population = new Population(mutationRate,popmax); population = new population(popmax); //create a new population button = new Button(15,560,160,20, "Evolve new generation"); //create a button //photoimg = new Images(); }
void draw() { background(1.0); // Display the faces population.display(); population.rollover(mouseX,mouseY); // Display some text textAlign(LEFT); fill(0); //text("Generation #:" + population.getGenerations(),15,190);
// Display the button button.display(); button.rollover(mouseX,mouseY); }
void mousePressed() { if (button.clicked(mouseX,mouseY)) { //population.selection(); //population.reproduction(); } }
void mouseReleased() { button.released();
class Images{ float increment = 0.02; float x, y; //postion of image int w, h; PImage photoimg, destination; //int wh = 600; boolean rolloverOn, clickedOn = false;
Rectangle r;
Images(float x_, float y_) { x = x_; y = y_; photoimg = new PImage(); photoimg = loadImage("jefferson.jpg"); w = photoimg.width; h = photoimg.height; //r = new Rectangle(int(x), int(y), int(wh), int(wh)); //destination = perlin(photoimg); }
/void settings(){ size(500,500); }/
void display(){ //update(mouseX, mouseY);
pushMatrix();
translate(x, y);
//stroke(255);
//image(photoimg,0,0);
//popMatrix();
noStroke();
/*if (rolloverOn == true){
tint(100);
}
else{
tint(255);
}*/
//stroke(255);
image(photoimg,0,0);
//image(destination,0,0);
popMatrix();
}
void update(int mx, int my) { if( roll_over(mx, my, photoimg.width, photoimg.height) ) { rolloverOn = true; } else { rolloverOn = false; } }
//position, width, height boolean roll_over(int p, int z, int w, int h){ if (p >= x && p <= x+w && z >= y && z <= y+h) { //tint(100); return true; } else { //tint(255); return false; } }
boolean clicked(int mx, int my) { if (r.contains(mx,my)) clickedOn = true; return clickedOn; } }
class population {
Images[] population; int generations;
population(int num) { population = new Images[num]; generations=0; for (int i = 0; i < population.length; i++) { population[i] = new Images(15+i*530, 30); } }
void display() { for (int i = 0; i < population.length; i++) { population[i].display(); } }
void rollover(int mx, int my) { for (int i = 0; i < population.length; i++) { population[i].update(mx, my); } }
class Rectangle { int x; int y; int width; int height;
Rectangle(int x_, int y_, int w, int h) { x = x_; y = y_; width = w; height = h; }
boolean contains(int px, int py) { return (px > x && px < x + width && py > y && py < y + height); }
}
class Button { Rectangle r; // Button's rectangle String txt; // Button's text boolean clickedOn; // Did i click on it? boolean rolloverOn; // Did i rollover it?
Button(int x, int y, int w, int h, String s) { r = new Rectangle(x,y,w,h); txt = s; }
void display() { // Draw rectangle and text based on whether rollover or clicked rectMode(CORNER); stroke(0); noFill(); if (rolloverOn) fill(0.5); if (clickedOn) fill(0); rect(r.x,r.y,r.width,r.height); float b = 0.0; if (clickedOn) b = 1; else if (rolloverOn) b = 0.2; else b = 0; fill(b); textAlign(LEFT); text(txt,r.x+10,r.y+14);
}
// Methods to check rollover, clicked, or released (must be called from appropriate // Places in draw, mousePressed, mouseReleased boolean rollover(int mx, int my) { if (r.contains(mx,my)) rolloverOn = true; else rolloverOn = false; return rolloverOn; }
boolean clicked(int mx, int my) { if (r.contains(mx,my)) clickedOn = true; return clickedOn; }
void released() { clickedOn = false; }
}
Thank you!
Answers
Highlight code, press ctrl-o...