Advertisement
Guest User

Helen example

a guest
Aug 31st, 2015
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.22 KB | None | 0 0
  1. ///////////////////////////////////////////////////////////////////////////
  2.  
  3. Helen, feel free to send me a msg (marzinp@free.fr) if you need more help.
  4.  
  5. ///////////////////////////////////////////////////////////////////////////
  6.  
  7. Ball []myballs;//global objects array
  8. void setup() {
  9.   size(300, 300);
  10.   myballs=new Ball[4];
  11.   for (int i=0; i<4; i++) {
  12.     myballs[i]=new Ball();//Ball objects instanciation (creation of an object according to the class constructor
  13.   }
  14. }
  15. void draw() {// Processing looping method
  16.   background(255);
  17.   for (Ball myball : myballs) {
  18.     myball.display();
  19.   }
  20. }
  21. void mouseReleased() {//this method is special, it is fired by mouseReleased system event
  22.   for (Ball myball : myballs) {
  23.     myball.reactToMouse();
  24.   }
  25. }
  26.  
  27. //from here this is my class definition
  28. class Ball {
  29.   float x, y;
  30.   int diameter=20;
  31.   Ball() {//Constructor
  32.     x=random(width);
  33.     y=random(height);
  34.   }
  35.   void display() {//display method
  36.     ellipse(x, y, diameter, diameter);
  37.   }
  38.   void reactToMouse() {//event answer method cannot be triggered directly, it has to be called from the main program mouseReleased() method
  39.     fill(random(255));
  40.     x=random(width);
  41.     y=random(height);
  42.     display();
  43.   }
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement