Skip to content
Snippets Groups Projects
Commit 4cfe7fbd authored by Paryss Mullier's avatar Paryss Mullier
Browse files

phase implémentation observer, observable

parent 0c7ec41f
No related branches found
No related tags found
No related merge requests found
package fr.univlille.iut.r304.thermo;
import fr.univlille.iut.r304.thermo.model.Thermostat;
import fr.univlille.iut.r304.thermo.view.TemperatureView;
import fr.univlille.iut.r304.thermo.view.TextView;
import fr.univlille.iut.r304.thermo.view.ThermoSlider;
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class Main extends Application {
......@@ -19,27 +16,10 @@ public class Main extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
Thermostat thermo = new Thermostat();
new TextView(thermo);
// Interface avec la température au centre et un bouton decrement et un bouton
// increment en JavaFX
VBox root = new VBox();
root.setAlignment(Pos.CENTER);
root.setSpacing(10);
Label temperatureLabel = new Label(thermo.getTemperature() + "°C");
Button incrementButton = new Button("Increment");
incrementButton.setOnMouseClicked(e -> thermo.incrementTemperature());
Button decrementButton = new Button("Decrement");
decrementButton.setOnMouseClicked(e -> thermo.decrementTemperature());
root.getChildren().addAll(temperatureLabel, incrementButton, decrementButton);
Scene scene = new Scene(root, 300, 200);
primaryStage.setScene(scene);
primaryStage.setTitle("Thermostat");
primaryStage.show();
TemperatureView view = new TemperatureView(thermo);
TextView textView = new TextView(thermo);
ThermoSlider slider = new ThermoSlider(thermo);
slider.start();
}
......
package fr.univlille.iut.r304.thermo.model;
public class Thermostat implements ITemperature {
import fr.univlille.iut.r304.utils.Observable;
public class Thermostat extends Observable implements ITemperature {
private double temperature;
......
package fr.univlille.iut.r304.thermo.view;
import fr.univlille.iut.r304.thermo.model.Thermostat;
import fr.univlille.iut.r304.utils.Observer;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class TemperatureView implements ITemperatureView, Observer {
@Override
public void decrementAction() {
thermostat.decrementTemperature();
updateTemperatureField();
}
@Override
public void incrementAction() {
thermostat.incrementTemperature();
updateTemperatureField();
}
@Override
public double getDisplayedValue() {
return thermostat.getTemperature();
}
private Thermostat thermostat;
private TextField temperatureField;
public TemperatureView(Thermostat thermostat) {
this.thermostat = thermostat;
start(new Stage());
}
public void start(Stage stage) {
VBox root = new VBox();
root.setAlignment(Pos.CENTER);
root.setSpacing(10);
temperatureField = new TextField(thermostat.getTemperature() + "°C");
temperatureField.setMaxWidth(100); // Set a fixed width for alignment
Button incrementButton = new Button("+");
incrementButton.setMaxWidth(100); // Set a fixed width for alignment
incrementButton.setOnMouseClicked(e -> {
thermostat.incrementTemperature();
updateTemperatureField();
});
Button decrementButton = new Button("-");
decrementButton.setMaxWidth(100); // Set a fixed width for alignment
decrementButton.setOnMouseClicked(e -> {
thermostat.decrementTemperature();
updateTemperatureField();
});
temperatureField.setOnAction(e -> {
try {
int newTemp = Integer.parseInt(temperatureField.getText().replace("°C", "").trim());
thermostat.setTemperature(newTemp);
updateTemperatureField();
} catch (NumberFormatException ex) {
updateTemperatureField(); // Reset to current temperature on invalid input
}
});
root.getChildren().addAll(incrementButton, temperatureField, decrementButton);
Scene scene = new Scene(root, 300, 200);
stage.setScene(scene);
stage.setTitle("Thermostat");
stage.show();
}
private void updateTemperatureField() {
temperatureField.setText(thermostat.getTemperature() + "°C");
}
}
package fr.univlille.iut.r304.thermo.view;
import fr.univlille.iut.r304.thermo.model.Thermostat;
import fr.univlille.iut.r304.utils.Observer;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.Slider;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class ThermoSlider implements Observer {
Thermostat thermo;
public ThermoSlider(Thermostat thermo) {
this.thermo = thermo;
}
public void start() {
Stage sliderStage = new Stage();
VBox sliderRoot = createSliderRoot();
Scene sliderScene = new Scene(sliderRoot, 300, 200);
sliderStage.setScene(sliderScene);
sliderStage.setTitle("Adjust Temperature");
sliderStage.show();
}
private VBox createSliderRoot() {
VBox sliderRoot = new VBox();
sliderRoot.setAlignment(Pos.CENTER);
sliderRoot.setSpacing(1);
Label sliderLabel = new Label("Adjust Temperature");
Slider temperatureSlider = createTemperatureSlider();
sliderRoot.getChildren().addAll(sliderLabel, temperatureSlider);
return sliderRoot;
}
private Slider createTemperatureSlider() {
Slider temperatureSlider = new Slider(-20, 40, thermo.getTemperature());
temperatureSlider.setOrientation(javafx.geometry.Orientation.VERTICAL);
temperatureSlider.setShowTickLabels(true);
temperatureSlider.setShowTickMarks(true);
temperatureSlider.setMajorTickUnit(10);
temperatureSlider.setMinorTickCount(1);
temperatureSlider.setBlockIncrement(1);
temperatureSlider.valueProperty().addListener((obs, oldVal, newVal) -> {
thermo.setTemperature(newVal.intValue());
// Assuming temperatureField is a TextField or similar component
// temperatureField.setText(thermo.getTemperature() + "°C");
temperatureSlider.setValue(newVal.doubleValue()); // Update the slider position
});
return temperatureSlider;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment