Take the bouncing ball example (Example 5-6). Display the X and Y coordinates as text next to the ball.
// Learning Processing
// Daniel Shiffman
// http://www.learningprocessing.com
// Exercise 17-5: Take the bouncing ball example from Chapter 5.
// Display the X and Y coordinates as text next to the ball.
// This answer also incorporates movement along the y-axis, as
// well as object orientation
Ball b;
PFont f;
void setup() {
size(200,200);
smooth();
b = new Ball(150,100,1,1);
f = createFont("Arial",12);
}
void draw() {
background(255);
b.move();
b.display();
}
class Ball {
int x;
int y;
int xspeed;
int yspeed;
Ball(int x_, int y_, int xspeed_, int yspeed_) {
x = x_;
y = y_;
xspeed = xspeed_;
yspeed = yspeed_;
}
void move() {
// Add the current speed to the location.
x = x + xspeed;
y = y + yspeed;
// Remember, || means "or."
if ((x > width) || (x < 0)) {
// If the object reaches either edge, multiply speed by -1 to turn it around.
xspeed = xspeed * -1;
}
// Remember, || means "or."
if ((y > height) || (y < 0)) {
// If the object reaches either edge, multiply speed by -1 to turn it around.
yspeed = yspeed * -1;
}
}
void display() {
// Display circle at x location
stroke(0);
fill(175);
ellipse(x,y,32,32);
fill(0);
textFont(f);
text(x + "," + y,x+18,y);
}
}








