Skip to content

Commit 9569f65

Browse files
committed
100% Finished
1 parent 144a4aa commit 9569f65

10 files changed

Lines changed: 58 additions & 58 deletions

src/simulator/factories/Builder.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ public JSONObject getInfo() {
2222
JSONObject info = new JSONObject();
2323
info.put("type", _typeTag);
2424
info.put("desc", _desc);
25+
JSONObject data = new JSONObject();
26+
fillInData(data);
27+
info.put("data", data);
2528
return info;
2629
}
2730

@@ -31,4 +34,7 @@ public String toString() {
3134
}
3235

3336
protected abstract T createInstance(JSONObject data);
37+
38+
protected void fillInData(JSONObject data) {
39+
}
3440
}

src/simulator/factories/MovingTowardsFixedPointBuilder.java

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,27 +16,23 @@ public MovingTowardsFixedPointBuilder() {
1616
@Override
1717
protected MovingTowardsFixedPoint createInstance(JSONObject data) {
1818
Vector2D c = new Vector2D();
19-
19+
2020
if (data.has("c")) {
2121
JSONArray _c = data.getJSONArray("c");
22-
22+
2323
if (_c.length() != 2)
2424
throw new IllegalArgumentException("El argumento c no es un Vector2D");
25-
25+
2626
c = new Vector2D(_c.getDouble(0), _c.getDouble(1));
2727
}
2828

2929
return new MovingTowardsFixedPoint(c, data.has("g") ? data.getDouble("g") : 9.81);
3030
}
31-
31+
3232
@Override
33-
public JSONObject getInfo() {
34-
JSONObject jo = super.getInfo();
35-
JSONObject data = new JSONObject();
33+
protected void fillInData(JSONObject data) {
3634
data.put("c", "the point towards which bodies move (e.g., [100.0,50.0])");
3735
data.put("g", "the length of the acceleration vector (a number)");
38-
jo.put("data", data);
39-
return jo;
4036
}
4137

4238
}

src/simulator/factories/NewtonUniversalGravitationBuilder.java

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,9 @@ public NewtonUniversalGravitationBuilder() {
1515
protected NewtonUniversalGravitation createInstance(JSONObject data) {
1616
return new NewtonUniversalGravitation(data.has("G") ? data.getDouble("G") : 6.67E-11);
1717
}
18-
18+
1919
@Override
20-
public JSONObject getInfo() {
21-
JSONObject jo = super.getInfo();
22-
JSONObject data = new JSONObject();
20+
protected void fillInData(JSONObject data) {
2321
data.put("G", "the gravitational constant (a number)");
24-
jo.put("data", data);
25-
return jo;
2622
}
27-
2823
}

src/simulator/factories/NoForceBuilder.java

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,5 @@ public NoForceBuilder() {
1515
protected NoForce createInstance(JSONObject data) {
1616
return new NoForce();
1717
}
18-
19-
@Override
20-
public JSONObject getInfo() {
21-
JSONObject jo = super.getInfo();
22-
jo.put("data", new JSONObject());
23-
return jo;
24-
}
2518

2619
}

src/simulator/launcher/Main.java

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ public class Main {
3838
private static Double _dtime = null;
3939
private static String _inFile = null;
4040
private static String _outFile = null;
41+
private static String _mode = null;
4142
private static JSONObject _forceLawsInfo = null;
4243

4344
// factories
@@ -71,8 +72,8 @@ private static void parseArgs(String[] args) {
7172
parseDeltaTimeOption(line);
7273
parseForceLawsOption(line);
7374
parseHelpOption(line, cmdLineOptions);
74-
//parseInFileOption(line);
7575
parseModeOption(line);
76+
parseInFileOption(line);
7677
parseOutFileOption(line);
7778
parseStepOption(line);
7879

@@ -159,17 +160,20 @@ private static void parseHelpOption(CommandLine line, Options cmdLineOptions) {
159160
}
160161
}
161162

163+
private static void parseModeOption(CommandLine line) throws ParseException {
164+
_mode = line.getOptionValue("m");
165+
if (_mode == null)
166+
_mode = "gui";
167+
else if (!_mode.equalsIgnoreCase("gui") && !_mode.equalsIgnoreCase("batch"))
168+
throw new ParseException("Illegal mode argument");
169+
}
170+
162171
private static void parseInFileOption(CommandLine line) throws ParseException {
163172
_inFile = line.getOptionValue("i");
164-
if (_inFile == null) {
173+
if (_mode.equalsIgnoreCase("batch") && _inFile == null) {
165174
throw new ParseException("In batch mode an input file of bodies is required");
166175
}
167176
}
168-
169-
private static void parseModeOption(CommandLine line) throws ParseException {
170-
String mode = line.getOptionValue("m");
171-
172-
}
173177

174178
private static void parseOutFileOption(CommandLine line) throws ParseException {
175179
_outFile = line.getOptionValue("o");
@@ -258,21 +262,20 @@ private static void startBatchMode() throws Exception {
258262

259263
private static void startGUIMode() throws Exception {
260264
PhysicsSimulator ps = new PhysicsSimulator(_forceLawsFactory.createInstance(_forceLawsInfo), _dtime);
261-
//InputStream in = new FileInputStream(_inFile);
262-
OutputStream out = _outFile == null ? System.out : new FileOutputStream(_outFile);
263265
Controller ctrl = new Controller(ps, _forceLawsFactory, _bodyFactory);
264266

265-
//ctrl.loadData(in);
267+
if (_inFile != null)
268+
ctrl.loadData(new FileInputStream(_inFile));
266269

267270
SwingUtilities.invokeAndWait(() -> new MainWindow(ctrl));
268-
269-
if (_outFile != null)
270-
out.close();
271271
}
272272

273273
private static void start(String[] args) throws Exception {
274274
parseArgs(args);
275-
startGUIMode();
275+
if (_mode.equalsIgnoreCase("gui"))
276+
startGUIMode();
277+
else
278+
startBatchMode();
276279
}
277280

278281
public static void main(String[] args) {

src/simulator/view/ControlPanel.java

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -165,15 +165,15 @@ private void run_sim(int n) {
165165
try {
166166
_ctrl.run(1);
167167
} catch (Exception e) {
168-
Utils.showErrorMsg(e.getMessage());
169-
170168
_quitButton.setEnabled(true);
171169
_fileButton.setEnabled(true);
172170
_forceLawsButton.setEnabled(true);
173171
_viewerButton.setEnabled(true);
174172
_runButton.setEnabled(true);
175-
176173
_stopped = true;
174+
175+
Utils.showErrorMsg(e.getMessage());
176+
177177
return;
178178
}
179179
SwingUtilities.invokeLater(() -> run_sim(n - 1));
@@ -194,14 +194,11 @@ public void onAdvance(Map<String, BodiesGroup> groups, double time) {
194194

195195
@Override
196196
public void onReset(Map<String, BodiesGroup> groups, double time, double dt) {
197-
// TODO Auto-generated method stub
198-
199197
}
200198

201199
@Override
202200
public void onRegister(Map<String, BodiesGroup> groups, double time, double dt) {
203-
// TODO Auto-generated method stub
204-
201+
_deltaTimeEnter.setText(Double.toString(dt));
205202
}
206203

207204
@Override
@@ -214,6 +211,7 @@ public void onBodyAdded(Map<String, BodiesGroup> groups, Body b) {
214211

215212
@Override
216213
public void onDeltaTimeChanged(double dt) {
214+
_deltaTimeEnter.setText(Double.toString(dt));
217215
}
218216

219217
@Override

src/simulator/view/ForceLawsDialog.java

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,18 @@
2222
import javax.swing.table.DefaultTableModel;
2323
import javax.swing.table.TableCellRenderer;
2424
import javax.swing.table.TableColumn;
25+
26+
import org.json.JSONArray;
2527
import org.json.JSONObject;
2628
import simulator.control.Controller;
2729
import simulator.model.BodiesGroup;
2830
import simulator.model.Body;
2931
import simulator.model.SimulatorObserver;
3032

3133
class ForceLawsDialog extends JDialog implements SimulatorObserver {
32-
34+
3335
private static final long serialVersionUID = 1L;
34-
36+
3537
private DefaultComboBoxModel<String> _lawsModel;
3638
private DefaultComboBoxModel<String> _groupsModel;
3739
private DefaultTableModel _dataTableModel;
@@ -74,9 +76,9 @@ private void initGUI() {
7476
mainPanel.add(Box.createRigidArea(new Dimension(0, 20)));
7577

7678
_dataTableModel = new DefaultTableModel() {
77-
79+
7880
private static final long serialVersionUID = 1L;
79-
81+
8082
@Override
8183
public boolean isCellEditable(int row, int column) {
8284
return column == 1;
@@ -149,8 +151,14 @@ public void actionPerformed(ActionEvent e) {
149151
try {
150152
JSONObject aux = new JSONObject();
151153
for (int i = 0; i < _dataTableModel.getRowCount(); i++) {
152-
// TODO
153-
aux.put((String) _dataTableModel.getValueAt(i, 0), (String) _dataTableModel.getValueAt(i, 1));
154+
String str = (String) _dataTableModel.getValueAt(i, 1);
155+
try {
156+
Double n = Double.parseDouble(str);
157+
aux.put((String) _dataTableModel.getValueAt(i, 0), n);
158+
} catch (NumberFormatException ex) {
159+
JSONArray ja = new JSONArray(str);
160+
aux.put((String) _dataTableModel.getValueAt(i, 0), ja);
161+
}
154162
}
155163

156164
JSONObject jo = new JSONObject();

src/simulator/view/GroupsTableModel.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,6 @@ public void onGroupAdded(Map<String, BodiesGroup> groups, BodiesGroup g) {
8181

8282
@Override
8383
public void onBodyAdded(Map<String, BodiesGroup> groups, Body b) {
84-
// TODO
8584
fireTableStructureChanged();
8685
}
8786

@@ -91,7 +90,6 @@ public void onDeltaTimeChanged(double dt) {
9190

9291
@Override
9392
public void onForceLawsChanged(BodiesGroup g) {
94-
// TODO Auto-generated method stub
9593
fireTableStructureChanged();
9694
}
9795
}

src/simulator/view/StatusBar.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,18 @@ public void onAdvance(Map<String, BodiesGroup> groups, double time) {
5555

5656
@Override
5757
public void onReset(Map<String, BodiesGroup> groups, double time, double dt) {
58-
_time = 0;
59-
_groups = 0;
58+
_time = time;
59+
_groups = groups.size();
60+
_timeLabel.setText("Time: " + _time);
61+
_groupsLabel.setText("Groups: " + _groups);
6062
}
6163

6264
@Override
6365
public void onRegister(Map<String, BodiesGroup> groups, double time, double dt) {
64-
_time = 0;
65-
_groups = 0;
66+
_time = time;
67+
_groups = groups.size();
68+
_timeLabel.setText("Time: " + _time);
69+
_groupsLabel.setText("Groups: " + _groups);
6670
}
6771

6872
@Override

src/simulator/view/Viewer.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
import java.awt.event.KeyListener;
1111
import java.awt.event.MouseEvent;
1212
import java.awt.event.MouseListener;
13-
import java.awt.geom.Ellipse2D;
1413
import java.util.ArrayList;
1514
import java.util.HashMap;
1615
import java.util.List;
@@ -220,7 +219,7 @@ private void drawBodies(Graphics2D g) {
220219
if (isVisible(b)) {
221220
Vector2D pos = new Vector2D(_centerX, _centerY).plus(b.getPosition().scale(1.0 / _scale));
222221
g.setColor(_gColor.get(b.getgId()));
223-
g.fill(new Ellipse2D.Double(pos.getX() - 5, pos.getY() - 5, 10, 10));
222+
g.fillOval((int) pos.getX() - 5, (int) pos.getY() - 5, 10, 10);
224223

225224
if (_showVectors) {
226225
Vector2D force = pos.plus(b.getForce().direction().scale(30));

0 commit comments

Comments
 (0)