Sfoglia il codice sorgente

Fixes invalid progress bar after Integer overflow at long simulations

Andreas T. Meyer-Berg 6 anni fa
parent
commit
29173259c9

+ 13 - 12
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/view/SimulationConfigurator.java

@@ -19,13 +19,16 @@ public class SimulationConfigurator extends JFrame {
 	private JTextField tfStepDuration;
 	private JTextField tfEndTimeLong;
 	private JLabel lbCurrentTimeShow;
+	/**
+	 * Progress between 0-10000
+	 */
 	private JProgressBar progressBar; 
 	private Timer timer;
 	private SimulationManager sim;
 	private long currentTime = 0;
 	private long startTime = 0;
 	private long duration = 100;
-	private long endTime = 0;
+	private long endTime = 1000;
 	
 	public SimulationConfigurator(SimulationManager sim) {
 		this.sim = sim;
@@ -60,13 +63,13 @@ public class SimulationConfigurator extends JFrame {
 		getContentPane().add(lblStepDuration);
 		
 		tfStartTimeLong = new JTextField();
-		tfStartTimeLong.setText("0");
+		tfStartTimeLong.setText(""+startTime);
 		tfStartTimeLong.setBounds(116, 67, 116, 22);
 		getContentPane().add(tfStartTimeLong);
 		tfStartTimeLong.setColumns(10);
 		
 		tfStepDuration = new JTextField();
-		tfStepDuration.setText("1000");
+		tfStepDuration.setText(""+duration);
 		tfStepDuration.setBounds(116, 99, 116, 22);
 		getContentPane().add(tfStepDuration);
 		tfStepDuration.setColumns(10);
@@ -76,7 +79,7 @@ public class SimulationConfigurator extends JFrame {
 		getContentPane().add(lblEndtimeLong);
 		
 		tfEndTimeLong = new JTextField();
-		tfEndTimeLong.setText("1000");
+		tfEndTimeLong.setText(""+endTime);
 		tfEndTimeLong.setBounds(347, 67, 85, 22);
 		getContentPane().add(tfEndTimeLong);
 		tfEndTimeLong.setColumns(10);
@@ -93,6 +96,8 @@ public class SimulationConfigurator extends JFrame {
 		progressBar.setBounds(0, 251, 442, 14);
 		getContentPane().add(progressBar);
 		progressBar.setValue(0);
+		progressBar.setMinimum(0);
+		progressBar.setMaximum(10000);
 		
 		JCheckBox chckbxPrintpackets = new JCheckBox("printPackets");
 		chckbxPrintpackets.setBounds(116, 127, 113, 25);
@@ -109,9 +114,7 @@ public class SimulationConfigurator extends JFrame {
 			endTime = Long.parseLong(tfEndTimeLong.getText());
 			startTime = Long.parseLong(tfStartTimeLong.getText());
 			duration = Long.parseLong(tfStepDuration.getText());
-			progressBar.setMinimum((int) Long.parseLong(tfStartTimeLong.getText()));
-			progressBar.setMaximum((int) Long.parseLong(tfEndTimeLong.getText()));
-			progressBar.setValue((int) Long.parseLong(tfStartTimeLong.getText()));
+			progressBar.setValue(0);
 			lbCurrentTimeShow.setText(currentTime+" ms");
 		}
 		if(timer.isRunning()){
@@ -129,10 +132,8 @@ public class SimulationConfigurator extends JFrame {
 			endTime = Long.parseLong(tfEndTimeLong.getText());
 			startTime = Long.parseLong(tfStartTimeLong.getText());
 			duration = Long.parseLong(tfStepDuration.getText());
-			progressBar.setMinimum((int) startTime);
-			progressBar.setMaximum((int) endTime);
-			progressBar.setValue((int) startTime);
-			lbCurrentTimeShow.setText(currentTime+"");
+			progressBar.setValue(0);
+			lbCurrentTimeShow.setText(currentTime+" ms");
 		} catch (Exception e) {
 			// TODO: handle exception
 		}
@@ -145,7 +146,7 @@ public class SimulationConfigurator extends JFrame {
 		}
 		sim.simulateTimeIntervall(currentTime,duration);
 		currentTime += duration;
-		progressBar.setValue((int) currentTime);
+		progressBar.setValue((int)((currentTime-startTime)*10000/(endTime-startTime)));
 		lbCurrentTimeShow.setText(currentTime+"");
 	}