Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Falling Sand Project: Part 4 - updateDisplay Method The updateDisplay method is called ( by the run method ) at regular intervals. Its job is

Falling Sand Project: Part 4- updateDisplay Method
The updateDisplay method is called (by the run method) at regular intervals. Its job is to draw each particle (and empty space) found in grid onto the display, using SandDisplay's setColor method.
I have provided some starter code for you. This code creates a Color object to use for the empty cells. Color objects are defined by the Red-Green-Blue (RGB) color scheme. Setting each of the values to 0 results in a black color.
The nested for loops iterate through all the cells of the grid. When a cell is found to be empty, we call the SandDisplay's setColor method to draw the cell with the appropriate color.
Create a Color object to use for metal particles, and add code to the nested for loops to check for metal cells. When you find one, call the SandDisplay's setColor method with the appropriate values.
Tip for creating Color objects: each of the Red-Green-Blue components can be set to a value between 0 and 255. Here are some examples.
When you have accomplished this, you should now be able to paint metal particles and erase them. Run your program and test this.
Part 5- Sand Particles
Modify your program so that you can also paint with sand particles (probably in yellow or some other sand-like color). To do this, perform the following steps:
Add a constant at the top of the file for sand.
In the constructor, change the size of the "names" array to 3 and store the name of the sand particle in the 3rd cell.
In the updateDisplay method, create a new Color object for sand and add an additional "else if" inside the loop to set sand particles.
Part 6- step Method
The step method is called (by the run method) at regular intervals. This method should choose a single random valid location (do not use a loop). If that location contains a sand particle and the location below it is empty, the particle should move down one row. (Metal particles will never move.) This code should only modify the array. Do not set any colors in the display. Test that your sand particles fall now.
Tip 1: Make sure your sand particles do not fall out of your grid (the two-dimensional array). The origin (0,0) is at the top-left corner of the grid, so when a sand particle falls down, it will be increasing its row value. The row value should never exceed the bounds of the two-dimensional array. The end result is that sand particles will collect at the bottom of the grid.
Tip 2: If particles fall too quickly or too slowly, the speed can be adjusted by adjusting the slider in the display or by changing the dimensions passed to the SandLab constructor (from main).
Note: Because the step method picks a single random particle to move (or act in some way) each time it is called, it is possible that some sand particles will move several times before others have the chance to move at all. In practice, the step method is called so rapidly that you are unlikely to notice this effect when you run the code.
Part 7- Water Particles
Modify your program so that you can also paint with water particles, which move in one of three randomly chosen directions: down, left, or right. In the step method, when the randomly chosen location contains a water particle, pick one of three random directions. If the location in that randomly chosen direction is empty, the water particle moves there. Test that the water behaves roughly like a liquid, taking the shape of a container.
Part 8- Dropping Sand into Water
What happens now when you drop sand particles into water? Right now, sand is only allowed to move into empty spaces. Modify your code so that a sand particle can also move into a space containing a water particle (by trading places with the water particle). Test that you can drop sand into water now (without destroying the water).
Part 9- Additional Particles
Congrats, you're doing well! To complete the assignment, add two additional particle types that behave differently than the existing ones. You can add whatever you want. Here are some ideas:
a gas that raises to the top of the grid
a particle that floats on water (maybe wood or plastic)
an acid that eats metal (or another type of material) package fallingsand;
import java.awt.*;
public class SandLab
{
//add constants for particle types here
public static final int EMPTY =0;
public static final int METAL =1;
//do not add any more fields
private int[][] grid;
private SandDisplay display;
public static final int ROWS =120;
public static final int COLUMNS =80;
public static void main(String[] args)
{
SandLab lab = new SandLab(ROWS, COLUMNS);
lab.run();
}
public SandLab(int numRows, int numCols)
{
String[] names;
names = new String[2];
names[EMPTY]= "Empty";
names[METAL]= "Metal";
display = new SandDisplay("Falling Sand",

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions