Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

First, create a class Particle whose instances represent individual particles that will randomly around the two-dimensional plane. Each particle should remember itsx - andy -coordinates

First, create a class Particle whose instances represent individual particles that will randomly around the two-dimensional plane. Each particle should remember itsx - andy -coordinates on the two-dimensional plane, stored in two data fields of the typedouble. This class should also have a random number generator shared between all objects: private static final Random rng = new Random(); The class should then have the following methods: public Particle(int width, int height) The constructor that places this particle in random coordinates inside the box whose possible values for the x -coordinate range from 0 to width, and for the y -coordinate from 0 to height. public double getX() public double getY() The accessor methods for the current x - and y -coordinates of this particle. public void move() Adds the value of the expression rng.nextGaussian() separately to thex - andy -coordinates of this particle, thus moving this particle a random distance to a random direction in the spirit of Brownian motion . Having completed the class to represent individual particles, write a class ParticleField that extends javax.swing.JPanel. The instances of this class represent an entire field of random particles. This class should have the following private instance fields. private boolean running = true; private java.util.List particles = new java.util.ArrayList();

The class should have the following public methods: public ParticleField(int n, int width, int height) The constructor that first sets the preferred size of this component to bewidth-by-height. Next, creates n separate instances of Particle and places them in the particles list. Having initialized the individual particles, this constructor should create and launch one new ThreadusingaRunnableargumentwhosemethodrunconsistsofonewhile(running)loop.The body of this loop should first sleep for 20 milliseconds. After waking up from its sleep, it should loop through all theparticlesandcallthemethodmove()foreachofthem.Thencallrepaint() and go to the next round of the while-loop. @Override public void paintComponent(Graphics g) Rendersthis component by looping through particles and rendering eachoneofthemasa3-by-3 pixel rectangle to its current x - and y -coordinates. public void terminate() Sets the fieldrunning ofthis component to befalse, causing theanimationthreadtoterminate in the near future. To admire the literal and metaphorical buzz produced by your particle swarm, writeanotherclass ParticleMain that contains a main method that creates one JFrame instance that contains a ParticleField of size 800-by-800 that contains 2,000 instances of Particle. Using the main method of the example classSpaceFiller asamodelofhowtodothis,attachaWindowListener to the JFrame so that the listener's methodwindowClosing first calls the methodterminate of the ParticleField instance shown inside the JFrame before disposing that JFrame itself.

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

Building Database Driven Catalogs

Authors: Sherif Danish

1st Edition

0070153078, 978-0070153073

More Books

Students also viewed these Databases questions