123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167 |
- package de.tu_darmstadt.tk.SmartHomeNetworkSim.core.distributionHandler;
- import java.awt.Color;
- import java.awt.Dimension;
- import javax.swing.JFrame;
- import javax.swing.JLabel;
- import javax.swing.JPanel;
- import javax.swing.JTextField;
- import org.apache.commons.math3.distribution.NormalDistribution;
- import org.apache.commons.math3.random.RandomGenerator;
- import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.ProbabilityDistributionHandler;
- /**
- * Example class representing a Normal Distribution
- *
- * @author Andreas T. Meyer-Berg
- */
- public class NormalDistributionHandler implements
- ProbabilityDistributionHandler {
- /**
- * Mean value
- */
- private double mean;
-
- /**
- * Standard Deviation
- */
- private double sd;
-
- /**
- * Normal Distribution
- */
- private NormalDistribution dist;
-
- /**
- *
- */
- private RandomGenerator gen = null;
-
- /**
- * Creates a new distribution which returns the given value
- * @param value value to be returned
- */
- public NormalDistributionHandler(double mean, double sd) {
- this.mean = mean;
- this.sd = sd;
- dist = new NormalDistribution(mean, sd);
- }
- /**
- * Creates a default distribution, which returns 50.
- */
- public NormalDistributionHandler() {
- this.mean = 50.0;
- this.sd = 12.0;
- dist = new NormalDistribution(mean, sd);
- }
-
- @Override
- public void setRandomGenerator(RandomGenerator rng) {
- gen = rng;
- dist = new NormalDistribution(rng, mean, sd);
- }
- @Override
- public long sampleNextValue() {
- return (long)Math.round(dist.sample());
- }
- @Override
- public JPanel getConfigurationPanel() {
- /**
- * JPanel which allows configuration of this Distribution
- */
- JPanel panel = new JPanel();
- panel.setMinimumSize(new Dimension(90, 80));
- panel.setLayout(null);
-
- /**
- * Label
- */
- JLabel label = new JLabel("Mean: ");
- panel.add(label);
- label.setLocation(20, 20);
- label.setSize(50,20);
- label.setMinimumSize(new Dimension(50, 20));
- /**
- * Textfield for changing the value
- */
- JTextField valueConfig = new JTextField(""+mean);
- panel.add(valueConfig);
- valueConfig.setLocation(80, 20);
- valueConfig.setMinimumSize(new Dimension(70, 20));
- valueConfig.setSize(70, 20);
- valueConfig.addActionListener(a->{
- try {
- /**
- * Update the value
- */
- mean = Double.parseDouble(valueConfig.getText());
- initializeDistribution();
- valueConfig.setBackground(Color.WHITE);
- } catch (Exception e) {
- valueConfig.setBackground(Color.RED);
- }
- });
-
-
-
- /**
- * Label
- */
- JLabel lbSD = new JLabel("SD: ");
- panel.add(lbSD);
- lbSD.setLocation(20, 50);
- lbSD.setSize(50,20);
- lbSD.setMinimumSize(new Dimension(50, 20));
- /**
- * Textfield for changing the value
- */
- JTextField tfSDvalue = new JTextField(""+sd);
- panel.add(tfSDvalue);
- tfSDvalue.setLocation(80, 50);
- tfSDvalue.setMinimumSize(new Dimension(70, 20));
- tfSDvalue.setSize(70, 20);
- tfSDvalue.addActionListener(a->{
- try {
- /**
- * Update the value
- */
- sd = Double.parseDouble(tfSDvalue.getText());
- tfSDvalue.setBackground(Color.WHITE);
- } catch (Exception e) {
- tfSDvalue.setBackground(Color.RED);
- }
- });
- return panel;
- }
- private void initializeDistribution() {
- if(gen==null)
- dist = new NormalDistribution(mean, sd);
- else
- dist = new NormalDistribution(gen, mean, sd);
-
- }
- public static void main(String[] args) {
- NormalDistributionHandler c = new NormalDistributionHandler(12.0, 5.0);
- JFrame test = new JFrame("test");
- test.setSize(400, 400);
- test.add(c.getConfigurationPanel());
- test.setEnabled(true);
- test.setVisible(true);
- }
- @Override
- public String getSimpleDescription() {
- return "Normal Distribution";
- }
- }
|