Loading an image to screen via button.
in
Programming Questions
•
1 year ago
My problem is I have two images which i'll call 'image1' & 'image2'. I'm trying to get image1 to initally load on the screen and when the red button is pressed image2 to be in it's place. But using my code when you click the button it only flashes the image2 for a second and then goes back to image1. So I'm looking at trying to keep image2 on the screen once the button is pressed.
Any help?
Thanks.
- PImage a;
- PImage b;
- int buttonX, buttonY;
- int rectSize = 50;
- boolean buttonOver;
- void setup() {
- size(1024, 769);
- buttonX = 50;
- buttonY = 50;
- a = loadImage("image1.jpg");
- b = loadImage("image2.jpg");
- }
- void draw() {
- if ( mouseX >= buttonX && mouseX <= buttonX+rectSize && mouseY >= buttonY && mouseY <= buttonY+rectSize) {
- buttonOver = true;
- }
- else {
- buttonOver = false;
- }
- fill(200, 0, 0);
- rect(buttonX, buttonY, rectSize, rectSize);
- image(a, 0, 250, a.width/3, a.height/3);
- }
- void mousePressed()
- {
- if(buttonOver) {
- image(b, 0, 250, b.width/3, b.height/3);
- }
- else {
- image(a, 0, 250, a.width/3, b.height/3);
- }
- }
2