// Copy this code (ctrl-A, ctrl-C), work on it, then paste it back!
class Button {
int x, y, w, h;
String text;
Button() {
}
Button(String t, int _x, int _y, int _w, int _h) {
text = t;
x = _x;
y = _y;
w = _w;
h = _h;
}
void display() {
fill(255);
rect(x, y, w, h);
fill(0);
textSize(25);
text(text, x, y);
}
boolean touchingPoint(int px, int py) {
return (abs(px - x) <w / 2) &&
(abs(py - y) <h / 2);
}
}
// these buttons control nyan cat
Button grow = new Button("Grow", 100, 75, 80, 50);
Button shrink = new Button("Shrink", 200, 75, 80, 50);
Button left = new Button("<<", 300, 75, 80, 50);
Button right = new Button(">>", 400, 75, 80, 50);
Button[] buttons = {grow, shrink, left, right};
float size = 50; // height of nyan cat
float rot = 0; // angle of rotation
float rotSpeed = 0; // rotation speed of nyan cat
float x, y;
String link = "http://www.nyan.cat/cats/original.gif";
PImage nyan;
// perform action for Button b
void action(Button b) {
if (b == grow) {
size += 10;
} else if ( b == shrink) {
size -= 10;
} else if ( b == left) {
rotSpeed -= 0.01;
} else if ( b == right) {
rotSpeed += 0.01;
}
}
void setup() {
size(500, 400);
imageMode(CENTER);
rectMode(CENTER);
textAlign(CENTER, CENTER);
nyan = loadImage(link);
x = width / 2;
y = height / 2;
}
void draw() {
background(0);
for (Button b : buttons) {
b.display();
}
cat();
}
// move and display cat
void cat() {
translate(x, y);
rotate(rot);
rot += rotSpeed;
image(nyan, 0, 0, size * 1.6, size);
resetMatrix();
}
void mousePressed() {
for (Button b : buttons) {
if (b.touchingPoint(mouseX, mouseY)) {
action(b);
}
}
}