Answered step by step
Verified Expert Solution
Question
1 Approved Answer
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 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 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 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 materialpackage fallingsand;
import java.awt.;
public class SandLab
add constants for particle types here
public static final int EMPTY ;
public static final int METAL ;
do not add any more fields
private int grid;
private SandDisplay display;
public static final int ROWS ;
public static final int COLUMNS ;
public static void mainString args
SandLab lab new SandLabROWS COLUMNS;
lab.run;
public SandLabint numRows, int numCols
String names;
names new String;
namesEMPTY "Empty";
namesMETAL "Metal";
display new SandDisplayFalling Sand", numRows, numCols, names;
called when the user clicks on a location using the given tool
private void locationClickedint row, int col, int tool
copies each element of grid into the display
public void updateDisplay
Color empty new Color;
for int row ; row ROWS; row
for int column ; column COLUMNS; column
if gridrowcolumn EMPTY
display.setColorrow 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 ; i display.getSpeed; i
step;
updateDisplay;
display.repaint;
display.pause; wait for redrawing and for mouse
int mouseLoc display.getMouseLocation;
if mouseLoc nulltest if mouse clicked
locationClickedmouseLoc mouseLoc display.getTool;
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started