This project demonstrates the central limit theorem using a Bean Machine simulation, also known as a Galton Board. The simulation is implemented in Java using JavaFX for the graphical interface.
Before you begin, ensure you have met the following requirements:
- Java Development Kit (JDK) 11 or later installed on your system
- JavaFX SDK (if using JDK 11 or later)
- An Integrated Development Environment (IDE) such as IntelliJ IDEA, Eclipse, or NetBeans (optional but recommended)
- Clone this repository or download the source files.
- Create a new Java project in your IDE.
- Copy
Bean.java
andBeanMachine.java
into your project'ssrc
directory, maintaining the package structure (both should be in a package calledmachine
).
If you're using JDK 11 or later, you need to set up JavaFX separately:
-
Download the JavaFX SDK from https://openjfx.io/
-
Extract the downloaded file to a location on your computer.
-
Add the JavaFX library to your project:
- File > Project Structure > Libraries > Add > Java > Select the
lib
directory from the extracted JavaFX SDK
- Right-click on the project > Properties > Java Build Path > Libraries > Add External JARs > Select all JAR files from the
lib
directory of the extracted JavaFX SDK
- Install the "Extension Pack for Java" if you haven't already.
- Open your project folder in VS Code.
- Create a new folder in your project root called
lib
. - Copy all the JAR files from the
lib
directory of your JavaFX SDK into this newlib
folder. - In VS Code, press Ctrl+Shift+P (or Cmd+Shift+P on macOS) to open the command palette.
- Type "Java: Configure Classpath" and select it.
- In the "Referenced Libraries" section, click on the "+" button.
- Navigate to your project's
lib
folder and select all the JavaFX JAR files you copied earlier. - Click "Select JAR files" to add them to your classpath.
Next, you'll need to modify your
launch.json
file to include the necessary VM arguments:- Go to the Run view (Ctrl+Shift+D or Cmd+Shift+D on macOS).
- Click on "create a launch.json file" if you don't have one, or open the existing one.
- Add or modify the configuration for your Java application:
{ "type": "java", "name": "Launch BeanMachine", "request": "launch", "mainClass": "machine.BeanMachine", "vmArgs": "--module-path /path/to/javafx-sdk/lib --add-modules javafx.controls,javafx.fxml", "projectName": "YourProjectName" }
Replace
/path/to/javafx-sdk
with the actual path to your JavaFX SDK, and "YourProjectName" with the name of your Java project.If you're using VS Code's Java Project explorer:
- Right-click on your project in the "JAVA PROJECTS" explorer.
- Select "Configure Classpath".
- In the "Referenced Libraries" section, click the "+" button.
- Navigate to and select all the JavaFX JAR files in your
lib
folder.
- File > Project Structure > Libraries > Add > Java > Select the
VM arguments, specified by vmArgs
in the launch.json
file, are parameters passed to the Java Virtual Machine (JVM) when it starts up. For JavaFX applications, these arguments are crucial because they tell the JVM where to find the JavaFX modules and which ones to use.
In our launch.json
configuration, we use two VM arguments:
-
--module-path /path/to/javafx-sdk/lib
: This specifies the path where the JavaFX modules are located. Replace/path/to/javafx-sdk
with the actual path to your JavaFX SDK installation. -
--add-modules javafx.controls,javafx.fxml
: This tells the JVM which JavaFX modules to load. In this case, we're loading thecontrols
module (for UI components) and thefxml
module (for working with FXML files, if used in your project).
These VM arguments are necessary because, starting from Java 11, JavaFX is no longer included in the standard JDK and needs to be specified explicitly when running JavaFX applications.
Example:
"vmArgs": "--module-path C:/Java/javafx-sdk-17.0.2/lib --add-modules javafx.controls,javafx.fxml"
Make sure to adjust the path according to your JavaFX SDK location.
- Set the main class to
machine.BeanMachine
. - Add the following VM options to your run configuration:
Replace
--module-path /path/to/javafx-sdk/lib --add-modules javafx.controls,javafx.fxml
/path/to/javafx-sdk
with the actual path to your JavaFX SDK. - Run the
BeanMachine
class.
- Compile the Java files:
javac --module-path /path/to/javafx-sdk/lib --add-modules javafx.controls,javafx.fxml machine/*.java
- Run the application:
java --module-path /path/to/javafx-sdk/lib --add-modules javafx.controls,javafx.fxml machine.BeanMachine
Replace /path/to/javafx-sdk
with the actual path to your JavaFX SDK.
Once the application is running:
- Click the "Start" button to begin the simulation.
- Beans will start dropping through the machine, demonstrating the central limit theorem.
- Use the "Stop" button to pause the simulation.
- Use the "Clear" button to reset the machine.
- If you encounter
ClassNotFoundException
orNoClassDefFoundError
related to JavaFX classes, ensure that the JavaFX SDK is correctly added to your project and that the module path is correctly set in your run configuration. - For any other issues, make sure you're using a compatible JDK version and that all required files are in the correct package structure.
Contributions to improve the simulation or documentation are welcome. Please feel free to submit a pull request or open an issue to discuss potential changes/additions.
If you have any questions or feedback, please open an issue in this repository.