Java

OOP GUIDE / WORK

 

GRID CLASS 2

 

Topics

  • Object arrays
  • Animation with NOOPDraw

 

TASK – PART 1 – SETUP


If you haven’t done so, you should really start with the Grid class work.  This will allow you to understand the workings of the Tile and Grid classes.

 

Note that you can get these classes from the Solutions.  However, understanding the classes is quite important.

 

You should have the following classes before starting:

 

  • Tile class
  • TileTester class (not needed for this work)
  • Grid class
  • GridTester class (not needed for this work)

 

 

TASK – PART 2 – GRID’S UPDATE

 

Inside the Grid class, create the following method:

 

public void update()

{

 

}

 

This method will be called regularly to make changes to the array.

 

For now, let’s make the update method reset all of the tile colours to a new random colour.  To do this, you simply loop over all tiles in the array (nested for loops) and call the Tile method setRandomColour();

 

 

TASK – PART 3 – ANIMATION LOOP

 

Do the following steps:

 

  • Create a new class called AnimatedGridTester and add a main function to it.

 

  • Inside main, start by creating a NOOPDraw window.

 

  • Now create a Grid object g with 10 rows and 10 columns.

 

  • We can now display the first frame of our animation by doing the following two things:

 

    • Call g.draw(); to draw the grid on the screen.

    • Pausing the program for 1000 milliseconds by using Thread.sleep(1000);
      Note: You will have to mouseOver the above statement and choose Add throws declaration.

  • We can now write the statements for the rest of the frames by doing this:

 

    • Add a while(true) statement that will loop forever.

    • Inside the loop, call g.update();

    • Then call g.draw(); once again.

    • Then, pause the program again using Thread.sleep(1000);

You can now run your program.  Your tiled grid should change colours every 1000 milliseconds (1 second).