Skip to content
Snippets Groups Projects
Commit 6a2fc1e3 authored by NicolasAnquetil's avatar NicolasAnquetil
Browse files

beta version de l'enoncé

parent 4a8cd055
No related branches found
No related tags found
No related merge requests found
Showing
with 508 additions and 2 deletions
bin bin
target
.classpath
.project
File added
pom.xml 0 → 100644
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>fr.univlille.iutinfo.m3501</groupId>
<artifactId>TP2</artifactId>
<version>1.0</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>11</source>
<target>11</target>
</configuration>
</plugin>
<plugin>
<groupId>org.openjfx</groupId>
<artifactId>javafx-maven-plugin</artifactId>
<version>0.0.4</version>
<configuration>
<mainClass>fr.univlille.iutinfo.m3105.Main</mainClass>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-controls</artifactId>
<version>14</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.6.2</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
...@@ -2,6 +2,7 @@ package fr.univlille.iutinfo.m3105; ...@@ -2,6 +2,7 @@ package fr.univlille.iutinfo.m3105;
import fr.univlille.iutinfo.m3105.modele.Echelle; import fr.univlille.iutinfo.m3105.modele.Echelle;
import fr.univlille.iutinfo.m3105.modele.Thermogeekosta; import fr.univlille.iutinfo.m3105.modele.Thermogeekosta;
import fr.univlille.iutinfo.m3105.vue.VueSlide;
import fr.univlille.iutinfo.m3105.vue.VueTexte; import fr.univlille.iutinfo.m3105.vue.VueTexte;
import javafx.application.Application; import javafx.application.Application;
import javafx.stage.Stage; import javafx.stage.Stage;
...@@ -15,8 +16,9 @@ public class Main extends Application { ...@@ -15,8 +16,9 @@ public class Main extends Application {
@Override @Override
public void start(Stage primaryStage) throws Exception { public void start(Stage primaryStage) throws Exception {
Thermogeekosta thermo = new Thermogeekosta(); Thermogeekosta thermo = new Thermogeekosta();
new VueTexte(Echelle.CELSIUS, thermo); new VueTexte( thermo.getTemp(Echelle.CELSIUS));
new VueTexte(Echelle.FAHRENHEIT, thermo); new VueTexte( thermo.getTemp(Echelle.FAHRENHEIT));
new VueSlide( thermo.getTemp(Echelle.KELVIN));
} }
} }
package fr.univlille.iutinfo.m3105.modele;
public enum Echelle {
CELSIUS("Celcius", "C", -10, 50),
DELISLE("Delisle", "D", 150, 0),
FAHRENHEIT("Fahrenheit", "F", 0, 200),
KELVIN("Kelvin", "K", 0, 360),
LEYDEN("Leyden", "L", -10, 80),
NEWTON("Newton", "N", -5, 30),
RANKINE("Rankine", "Ra", -260, 360),
REAUMUR("Reaumur", "Re", -10, 80),
ROMER("Romer", "Ro", 5, 60);
private String nom;
private String abbrev;
private int min;
private int max;
private Echelle(String nom, String abbrev, int min, int max) {
this.nom = nom;
this.abbrev = abbrev;
this.min = min;
this.max = max;
}
public String getNom() {
return nom;
}
public String getAbbrev() {
return abbrev;
}
public int getMin() {
return min;
}
public int getMax() {
return max;
}
public double toKelvin(double value) {
switch (this) {
case CELSIUS:
return value + 273.15;
case DELISLE:
return value;
case FAHRENHEIT:
return value - 32.0 * 5.0 / 9.0 + 273.15;
case KELVIN:
return value;
case LEYDEN:
return value;
case NEWTON:
return value / 0.33 + 273.15;
case RANKINE:
return value / 1.8;
case REAUMUR:
return value;
case ROMER:
return value;
}
return 0;
}
protected double fromKelvin(double value) {
switch (this) {
case CELSIUS:
return value - 273.15;
case DELISLE:
return( value);
case FAHRENHEIT:
return (value - 273.15) * 9.0 / 5.0 + 32.0;
case KELVIN:
return( value);
case LEYDEN:
return value;
case NEWTON:
return (value - 273.15) * 0.33;
case RANKINE:
return value * 1.8;
case REAUMUR:
return( value);
case ROMER:
return( value);
default:
return value; // should never happen
}
}
}
package fr.univlille.iutinfo.m3105.modele;
import fr.univlille.iutinfo.m3105.utils.ConnectableProperty;
import fr.univlille.iutinfo.m3105.utils.Subject;
/** Stores a temperature (value) + knows in what temperature scale it is expressed
*/
public class Temperature extends ConnectableProperty {
/** the scale in which the temperature is stored
*/
protected Echelle echelle;
public Temperature(Echelle e) {
super();
echelle = e;
}
public void temperaturePlus() {
setValue( (double)value + 1);
}
public void temperatureMoins() {
setValue( (double)value - 1);
}
public Echelle getEchelle() {
return echelle;
}
@Override
public void update(Subject other, Object value) {
Echelle otherEchelle = ((Temperature) other).getEchelle();
setValue( echelle.fromKelvin( otherEchelle.toKelvin((double) value)) );
}
}
package fr.univlille.iutinfo.m3105.modele;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
/** Main model class
* this is actually a factory that can return Temperature object in any scale
*/
public class Thermogeekosta {
public static final Echelle DEFAULT_ECHELLE = Echelle.CELSIUS;
public static final double DEFAULT_VALUE = 18.0;
protected Map<Echelle,Temperature> temps;
public Thermogeekosta() {
temps = new HashMap<>(Echelle.values().length);
// initialize a default temperature to a decent value
getTemp(DEFAULT_ECHELLE).setValue(DEFAULT_VALUE);
}
/** Returns a <code>Temperature</code> object in the right scale
* If such a <code>Temperature</code> was already created, returns it, otherwise create one
* Each new <code>Temperature</code> (except the first) is biConnected to a previously created one
* so that all evolves jointly
*/
public Temperature getTemp(Echelle echelle) {
Temperature t = temps.get(echelle);
if (t == null) {
t = new Temperature( echelle);
Collection<Temperature> existing = temps.values();
if (! existing.isEmpty() ) {
//connect the new Temperature to any previously created one
t.biconnectTo(existing.iterator().next());
}
temps.put(echelle, t);
}
return t;
}
}
package fr.univlille.iutinfo.m3105.utils;
public class ConnectableProperty extends ObservableProperty implements Observer {
protected boolean propagating;
public ConnectableProperty() {
propagating = false;
}
public void connectTo(ConnectableProperty other) {
other.attach(this);
}
public void biconnectTo(ConnectableProperty other) {
connectTo(other);
update(other, other.getValue());
other.connectTo(this);
}
public void unconnectFrom(ConnectableProperty other) {
other.detach(this);
}
public Object getValue() {
return value;
}
public void setValue(Object val) {
if (! propagating) {
propagating = true;
super.setValue(val);
propagating = false;
}
}
@Override
public void update(Subject subj) {
// never called so does not do anything
}
@Override
public void update(Subject other, Object data) {
setValue(data);
}
}
package fr.univlille.iutinfo.m3105.utils;
public class ObservableProperty extends Subject {
protected Object value;
public Object getValue() {
return value;
}
public void setValue(Object val) {
value = val;
notifyObservers(val);
}
}
package fr.univlille.iutinfo.m3105.utils;
public interface Observer {
public void update(Subject subj);
public void update(Subject subj, Object data);
}
package fr.univlille.iutinfo.m3105.utils;
import java.util.ArrayList;
import java.util.List;
public abstract class Subject {
protected List<Observer> attached;
public Subject() {
attached = new ArrayList<>();
}
public void attach(Observer obs) {
if (! attached.contains( obs)) {
attached.add(obs);
}
}
public void detach(Observer obs) {
if (attached.contains( obs)) {
attached.remove(obs);
}
}
public void notifyObservers() {
for (Observer o : attached) {
o.update(this);
}
}
public void notifyObservers(Object data) {
for (Observer o : attached) {
o.update(this, data);
}
}
}
package fr.univlille.iutinfo.m3105.vue;
import fr.univlille.iutinfo.m3105.modele.Echelle;
import fr.univlille.iutinfo.m3105.modele.Temperature;
import fr.univlille.iutinfo.m3105.utils.Observer;
import fr.univlille.iutinfo.m3105.utils.Subject;
import javafx.scene.Scene;
import javafx.scene.layout.Region;
import javafx.stage.Stage;
public abstract class AbstractVue implements Observer {
protected Temperature modele;
public AbstractVue(Temperature temp) {
this.modele = temp;
this.modele.attach(this);
Scene sc = new Scene(createSceneContent());
Stage st = new Stage();
st.setScene(sc);
st.setTitle(getEchelle().getNom());
update(null, null); // initialize display to current modele value
st.show();
}
protected Echelle getEchelle() {
return modele.getEchelle();
}
protected abstract Region createSceneContent();
@Override
public void update(Subject subj) {
update(subj, null);
}
public abstract double getValeurAffichee();
public abstract void setValeurAffichee(double val);
@Override
public void update(Subject subj, Object data) {
setValeurAffichee((double) modele.getValue());
}
}
\ No newline at end of file
package fr.univlille.iutinfo.m3105.vue;
import fr.univlille.iutinfo.m3105.modele.Temperature;
import javafx.geometry.Insets;
import javafx.geometry.Orientation;
import javafx.geometry.Pos;
import javafx.scene.control.Slider;
import javafx.scene.layout.Region;
import javafx.scene.layout.VBox;
public class VueSlide extends AbstractVue {
protected Slider saisie;
public VueSlide(Temperature temp) {
super(temp);
}
@Override
protected Region createSceneContent() {
VBox vb = new VBox();
vb.setAlignment(Pos.CENTER);
vb.setPadding(new Insets(5, 10, 5, 10));
vb.setSpacing(10);
saisie = createSlider();
vb.getChildren().add( saisie );
return vb;
}
private Slider createSlider() {
Slider slid;
int min = modele.getEchelle().getMin();
int max = modele.getEchelle().getMax();
slid = new Slider(min,max,0);
slid.setOrientation(Orientation.VERTICAL);
slid.setMajorTickUnit( Math.abs((max - min) / 10) );
slid.setShowTickMarks(true);
slid.setShowTickLabels(true);
slid.valueProperty().addListener( (e,o,n) -> modele.setValue(n) );
return slid;
}
@Override
public double getValeurAffichee() {
return saisie.getValue();
}
@Override
public void setValeurAffichee(double val) {
saisie.setValue( val );
}
}
package fr.univlille.iutinfo.m3105.vue;
import fr.univlille.iutinfo.m3105.modele.Temperature;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Node;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Region;
import javafx.scene.layout.VBox;
public class VueTexte extends AbstractVue {
protected TextField saisie;
public VueTexte(Temperature temp) {
super(temp);
}
@Override
protected Region createSceneContent() {
VBox vb = new VBox();
vb.setAlignment(Pos.CENTER);
vb.setPadding(new Insets(5, 10, 5, 10));
vb.setSpacing(10);
vb.getChildren().add(new Label( "Température en " + getEchelle().getNom()));
saisie = new TextField();
saisie.setMaxWidth(Region.USE_PREF_SIZE);
saisie.setPrefWidth(70.0);
saisie.setAlignment(Pos.CENTER_RIGHT);
saisie.setOnAction( (e) -> modele.setValue( getValeurAffichee()) );
vb.getChildren().addAll( saisie , makeButtons());
return vb;
}
protected Node makeButtons() {
HBox hbox = new HBox();
hbox.setAlignment(Pos.CENTER);
hbox.setSpacing(10);
hbox.setPadding(new Insets(5, 10, 5, 10));
Button b;
b = new Button("+");
b.setMinWidth(Region.USE_PREF_SIZE);
b.setPrefWidth(40.0);
b.setOnAction( (e) -> modele.temperaturePlus() );
hbox.getChildren().add(b);
b = new Button("-");
b.setMinWidth(Region.USE_PREF_SIZE);
b.setPrefWidth(40.0);
b.setOnAction( (e) -> modele.temperatureMoins() );
hbox.getChildren().add(b);
return hbox;
}
public double getValeurAffichee() {
return Double.parseDouble( saisie.getText());
}
public void setValeurAffichee(double val) {
saisie.setText( String.format("%.1f", val) );
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment