Question: Part 7 - Water Particles Modify your program so that you can also paint with water particles, which move in one of three randomly chosen

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", numRows, numCols, names);
}
//called when the user clicks on a location using the given tool
private void locationClicked(int row, int col, int tool)
{
}
//copies each element of grid into the display
public void updateDisplay()
{
Color empty = new Color(0,0,0);
for (int row =0; row < ROWS; row++)
{
for (int column =0; column < COLUMNS; column++)
{
if (grid[row][column]== EMPTY)
{
display.setColor(row, column, empty);
}
}
}
}
//called repeatedly.
//causes one random particle to maybe do something.
public void step()
{
}
//do not modify
public void run()
{
while (true)
{
for (int i =0; i < display.getSpeed(); i++)
{
step();
}
updateDisplay();
display.repaint();
display.pause(1); //wait for redrawing and for mouse
int[] mouseLoc = display.getMouseLocation();
if (mouseLoc != null)//test if mouse clicked
{
locationClicked(mouseLoc[0], mouseLoc[1], display.getTool());
}
}
}
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!