Ajout de deux controller (Page des TopPlayers.fxml et MainPage.fxml)

Création d'une classe database avec un serializer er un deserializer
julien
ZartaX0O3 4 years ago
parent 3bbfc808a4
commit a424a8ca20

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.6 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 260 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.3 KiB

@ -0,0 +1,5 @@
#player{
-fx-text-fill: #ffffff;
}

@ -0,0 +1,43 @@
package sample;
import javafx.scene.layout.Pane;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;
public class DataTable implements Serializable {
private static int id=0;
private int gameId;
private int score;
private int rounds;
public DataTable(){
id++;
this.gameId=id;
//this.score=score; Calcul du score avec la difficulté !
}
//Function update
public void saveGame(){
Main.getDatabase().getDatabaseFiles().add(this);
try{
Main.serialize();
}
catch (IOException e){
//System.out.println("Cant close stream");
}
}
public String toString()
{
return " Valeur a mettre";
}
}

@ -0,0 +1,41 @@
package sample;
import java.io.*;
import java.util.ArrayList;
public class Database implements Serializable {
private int maxScore;
private static Database db;
private final ArrayList<DataTable> databaseFiles;
public Database() {
this.maxScore = 1;
databaseFiles = new ArrayList<DataTable>();
}
public int getMaxScore() {
return maxScore;
}
public void setMaxScore(int maxLevel) {
if (this.maxScore < maxLevel) this.maxScore = maxLevel;
}
public void addData(DataTable d) {
databaseFiles.add(d);
}
public void removeData(DataTable d) {
databaseFiles.remove(d);
}
public static void resetScore() {
db = new Database();
}
public ArrayList<DataTable> getDatabaseFiles() {
return databaseFiles;
}
}

@ -6,18 +6,45 @@ import javafx.scene.Parent;
import javafx.scene.Scene; import javafx.scene.Scene;
import javafx.stage.Stage; import javafx.stage.Stage;
import java.io.*;
import java.util.Objects; import java.util.Objects;
public class Main extends Application { public class Main extends Application {
private static Database currentd;
@Override @Override
public void start(Stage primaryStage) throws Exception{ public void start(Stage primaryStage) throws Exception{
Parent root = FXMLLoader.load(Objects.requireNonNull(getClass().getResource("sample.fxml"))); Parent root = FXMLLoader.load(Objects.requireNonNull(getClass().getResource("MainPage.fxml")));
primaryStage.setTitle("Hello World"); primaryStage.setTitle("Hello World");
primaryStage.setScene(new Scene(root, 900, 600)); primaryStage.setScene(new Scene(root, 900, 600));
primaryStage.setResizable(false);
primaryStage.show(); primaryStage.show();
} }
public static Database getDatabase(){
return currentd;
}
public static void serialize() throws IOException {
try (ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("database.txt"))) {
out.writeObject(currentd);
} finally {
//System.out.println("Saved!");
System.exit(0);
}
}
public static void deserialize() throws ClassNotFoundException, IOException{
ObjectInputStream in = null;
try {
in=new ObjectInputStream (new FileInputStream("database.txt"));
currentd=(Database) in.readObject();
in.close();
}
catch (FileNotFoundException | NullPointerException e){
currentd=new Database();
}
}
public static void main(String[] args) { public static void main(String[] args) {
launch(args); launch(args);

@ -0,0 +1,44 @@
<?xml version="1.0" encoding="UTF-8"?>
<?import com.jfoenix.controls.JFXButton?>
<?import javafx.scene.effect.Shadow?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.shape.Circle?>
<?import javafx.scene.text.Font?>
<AnchorPane fx:id="mainRoot" xmlns:fx="http://javafx.com/fxml/1" id="pane" maxHeight="-Infinity" maxWidth="-Infinity"
minHeight="-Infinity"
minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0"
xmlns="http://javafx.com/javafx/11" fx:controller="sample.MainPageController">
<Circle layoutX="146.0" layoutY="138.0" opacity="0.42" radius="219.0" stroke="BLACK" strokeType="INSIDE"/>
<ImageView fitHeight="600.0" fitWidth="1300" layoutX="133.0" layoutY="150.0" pickOnBounds="true" preserveRatio="true"
AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0"
AnchorPane.topAnchor="0.0">
<Image url="/fire.gif"/>
</ImageView>
<ImageView fitHeight="205.0" fitWidth="204.0" layoutX="61.0" layoutY="-26.0" pickOnBounds="true"
preserveRatio="true">
<Image url="/logo.png"/>
</ImageView>
<JFXButton layoutX="104.0" layoutY="163.0" prefHeight="30.0" prefWidth="119.0"
style="-fx-background-color: #EB9C0A;" text="New Game" textFill="WHITE" onMouseClicked="#startGame">
<font>
<Font name="Segoe UI Black" size="15.0"/>
</font>
<effect>
<Shadow/>
</effect>
</JFXButton>
<JFXButton layoutX="104.0" layoutY="222.0" prefHeight="30.0" prefWidth="119.0"
style="-fx-background-color: #EB9C0A;" text="Best Players" textFill="WHITE" onMouseClicked="#viewScore">
<font>
<Font name="Segoe UI Black" size="15.0"/>
</font>
<effect>
<Shadow/>
</effect>
</JFXButton>
</AnchorPane>

@ -0,0 +1,58 @@
package sample;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.chart.PieChart;
import javafx.scene.control.Button;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.AnchorPane;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Objects;
import java.util.ResourceBundle;
public class MainPageController implements Initializable {
@FXML
private AnchorPane mainRoot;
@FXML
private Button startGame;
@FXML
private Button viewScore;
@FXML
private ImageView exitGame;
@FXML
private ImageView loadGame;
@FXML
void exitGame(MouseEvent event) {
System.exit(0);
}
@Override
public void initialize(URL url, ResourceBundle resourceBundle) {
}
@FXML
void viewScore(MouseEvent event) throws Exception{
AnchorPane pane = FXMLLoader.load(Objects.requireNonNull(getClass().getResource("TopPlayers.fxml")));
mainRoot.getChildren().setAll(pane);
}
@FXML
void startGame(MouseEvent event) throws Exception{
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("GamePlay.fxml"));
AnchorPane pane=fxmlLoader.load();
// Appelle au controler de jeu....
}
}

@ -0,0 +1,39 @@
package sample;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.chart.PieChart;
import javafx.scene.control.Button;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.AnchorPane;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Objects;
import java.util.ResourceBundle;
public class TopPlayerController implements Initializable {
@FXML
private AnchorPane levelRoot;
@FXML
private Button returnToMenu;
@Override
public void initialize(URL url, ResourceBundle resourceBundle) {
}
@FXML
void returnToMenu(MouseEvent event) throws Exception{
AnchorPane pane = FXMLLoader.load(Objects.requireNonNull(getClass().getResource("MainPage.fxml")));
levelRoot.getChildren().setAll(pane);
}
}

@ -0,0 +1,138 @@
<?xml version="1.0" encoding="UTF-8"?>
<?import com.jfoenix.controls.JFXButton?>
<?import javafx.scene.control.*?>
<?import javafx.scene.effect.Shadow?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.paint.*?>
<?import javafx.scene.shape.Rectangle?>
<?import javafx.scene.text.*?>
<?import java.net.URL?>
<AnchorPane fx:id="levelRoot" xmlns:fx="http://javafx.com/fxml/1" id="pane" opacity="0.91" prefHeight="600.0"
prefWidth="800.0"
xmlns="http://javafx.com/javafx/11" fx:controller="sample.TopPlayerController">
<stylesheets>
<URL value="@../css/anchor.css"/>
</stylesheets>
<ImageView fitHeight="600.0" fitWidth="1300" layoutX="133.0" layoutY="150.0" pickOnBounds="true" preserveRatio="true"
AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0"
AnchorPane.topAnchor="0.0">
<Image url="/fire.gif"/>
</ImageView>
<JFXButton onMouseClicked="#returnToMenu" layoutX="20.0" layoutY="10.0" prefHeight="30.0" prefWidth="80.0"
style="-fx-background-color: #EB9C0A;"
text="Retour" textFill="WHITE">
<font>
<Font name="Segoe UI Black" size="13.0"/>
</font>
<effect>
<Shadow/>
</effect>
</JFXButton>
<Rectangle arcHeight="5.0" arcWidth="5.0" height="600.0" layoutX="200" opacity="0.42" stroke="BLACK"
strokeType="INSIDE" width="500.0"/>
<ImageView fitHeight="205.0" fitWidth="204.0" layoutX="350.0" layoutY="-4.0" pickOnBounds="true"
preserveRatio="true">
<Image url="/logo.png"/>
</ImageView>
<Label layoutX="387.0" layoutY="168.0" text="Top Players">
<font>
<Font name="Segoe UI Black" size="22.0"/>
</font>
</Label>
<Label layoutX="395.0" layoutY="211.0" text="1 - ZartaX0O3">
<font>
<Font name="Segoe UI Black" size="17.0"/>
</font>
<textFill>
<LinearGradient startX="0.3981042654028436">
<stops>
<Stop>
<color>
<Color red="0.9342105388641357" green="0.7629386186599731"/>
</color>
</Stop>
<Stop offset="1.0">
<color>
<Color red="1.0" green="0.5557553768157959"/>
</color>
</Stop>
</stops>
</LinearGradient>
</textFill>
</Label>
<Label layoutX="250.0" layoutY="260.0" text="2 - ZartaX0O3">
<font>
<Font name="Segoe UI Black" size="17.0"/>
</font>
<textFill>
<LinearGradient endX="1.0" endY="1.0" startX="0.4075829383886256">
<stops>
<Stop>
<color>
<Color/>
</color>
</Stop>
<Stop offset="1.0">
<color>
<Color red="1.0" green="1.0" blue="1.0"/>
</color>
</Stop>
</stops>
</LinearGradient>
</textFill>
</Label>
<Label layoutX="530.0" layoutY="260.0" text="3 - ZartaX0O3">
<font>
<Font name="Segoe UI Black" size="17.0"/>
</font>
<textFill>
<LinearGradient endX="1.0" endY="1.0" startX="0.3981042654028436">
<stops>
<Stop>
<color>
<Color red="0.4868420958518982" green="0.32828032970428467" blue="0.03380848094820976"/>
</color>
</Stop>
<Stop offset="1.0">
<color>
<Color red="0.05263157933950424" green="0.051315788179636" blue="0.049707602709531784"/>
</color>
</Stop>
</stops>
</LinearGradient>
</textFill>
</Label>
<Label id="player" layoutX="395.0" layoutY="315.0" text="4 - ZartaX0O3">
<font>
<Font name="Segoe UI Black" size="17.0"/>
</font>
</Label>
<Label id="player" layoutX="395.0" layoutY="382.0" text="6 - ZartaX0O3">
<font>
<Font name="Segoe UI Black" size="17.0"/>
</font>
</Label>
<Label id="player" layoutX="395.0" layoutY="347.0" text="5 - ZartaX0O3">
<font>
<Font name="Segoe UI Black" size="17.0"/>
</font>
</Label>
<Label id="player" layoutX="395.0" layoutY="417.0" text="7 - ZartaX0O3">
<font>
<Font name="Segoe UI Black" size="17.0"/>
</font>
</Label>
<Label id="player" layoutX="395.0" layoutY="450.0" text="8 - ZartaX0O3">
<font>
<Font name="Segoe UI Black" size="17.0"/>
</font>
</Label>
<Label id="player" layoutX="395.0" layoutY="486.0" text="9 - ZartaX0O3">
<font>
<Font name="Segoe UI Black" size="17.0"/>
</font>
</Label>
</AnchorPane>
Loading…
Cancel
Save