-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFriction.java
More file actions
112 lines (88 loc) · 3.95 KB
/
Friction.java
File metadata and controls
112 lines (88 loc) · 3.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* World representing the friction simulation
*
* @author Austin
* @version 0
*/
public class Friction extends Simulation {
private Slider height;
private Slider length;
private Slider mu;
private Label heightLabel;
private Label lengthLabel;
private Label muLabel;
private Label time;
private Label x;
private Label v;
private Label a;
private Ramp ramp;
private Box box;
/**
* Constructor for objects of class Friction.
*/
public Friction() {
super();
setBackground(new GreenfootImage("Friction.png"));
}
/**
* Listen for mouse input for sliders and buttons
*/
public void act() {
super.act();
setBackground(new GreenfootImage("Friction.png"));
if (resetButton.mouseDown()) {
Greenfoot.setWorld(new Friction());
}
// Set attributes of ramp and box for live feedback
if (!started) {
ramp.setLength((int) length.getValue());
ramp.setHeight((int) height.getValue());
box.setMu(mu.getValue());
}
}
/**
* Create all objects and add them to the world
*/
protected void prepare() {
super.prepare();
ramp = new Ramp();
ramp.addToWorld(this);
box = new Box(ramp);
box.addToWorld(this);
// Sliders
height = new Slider(141, 156, 336, 14, new Color(188, 190, 192), 20, 150, 500, new Color(57, 181, 74), new Color(0, 148, 68), new Color(0, 148, 68), 3);
height.addToWorld(this);
length = new Slider(141, 210, 336, 14, new Color(188, 190, 192), 20, 150, 1000, new Color(57, 181, 74), new Color(0, 148, 68), new Color(0, 148, 68), 3);
length.addToWorld(this);
mu = new Slider(141, 264, 336, 14, new Color(188, 190, 192), 20, 0, 1, new Color(57, 181, 74), new Color(0, 148, 68), new Color(0, 148, 68), 3);
mu.addToWorld(this);
// Labels for sliders
heightLabel = new LinkedLabel(470, 144, 80, 36, new Color(57, 181, 74, 0), () -> String.format("%.01f", height.getValue()), 30, new Color(0, 148, 68));
heightLabel.addToWorld(this);
lengthLabel = new LinkedLabel(470, 198, 80, 36, new Color(57, 181, 74, 0), () -> String.format("%.01f", length.getValue()), 30, new Color(0, 148, 68));
lengthLabel.addToWorld(this);
muLabel = new LinkedLabel(470, 252, 80, 36, new Color(57, 181, 74, 0), () -> String.format("%.02f", mu.getValue()), 30, new Color(0, 148, 68));
muLabel.addToWorld(this);
// Output labels
time = new LinkedLabel(945, 45, 300, 200, new Color(0, 0, 0, 0), () -> String.format("%.02f", box.getTime()) + " s", 25, new Color(0, 148, 68));
time.addToWorld(this);
x = new LinkedLabel(945, 95, 300, 200, new Color(0, 0, 0, 0), () -> String.format("%.01f", box.getPosition().getX()) + " m, " + String.format("%.01f", box.getPosition().getY()) + " m", 25, new Color(0, 148, 68));
x.addToWorld(this);
v = new LinkedLabel(945, 145, 300, 200, new Color(0, 0, 0, 0), () -> String.format("%.01f", box.getVelocity().getX()) + " ms⁻¹, " + String.format("%.01f", box.getVelocity().getY()) + " ms⁻¹", 25, new Color(0, 148, 68));
v.addToWorld(this);
a = new LinkedLabel(945, 195, 300, 200, new Color(0, 0, 0, 0), () -> String.format("%.01f", box.getAcceleration().getX()) + " ms⁻², " + String.format("%.01f", box.getAcceleration().getY()) + " ms⁻²", 25, new Color(0, 148, 68));
a.addToWorld(this);
}
/**
* Starts the simulation with given inputs
*/
protected void begin() {
super.begin();
box.resume();
box.showForces();
height.disable();
length.disable();
mu.disable();
}
}