From 6ceeffac0e16c0dfbbbcdb6b2698071da7084d60 Mon Sep 17 00:00:00 2001 From: Nathan Baudrier Date: Thu, 2 Oct 2025 10:48:05 +0200 Subject: [PATCH] some changes --- DEV/DEV3.1/TP01/Exercise2/Database.java | 68 +++++++++++++++ DEV/DEV3.1/TP01/Exercise2/Main.java | 3 + .../TP01/Exercise2/ScoreBoardWindow.java | 5 ++ .../TP02/Part1/ClickSwapImageEvent.java | 5 +- DEV/DEV3.1/TP02/Part1/ImageManager.java | 49 +++++++++++ DEV/DEV3.1/TP02/Part1/ImageWindow.java | 53 ++---------- DEV/DEV3.1/TP03/Exercise1/GridWindow.java | 86 +++++++++++++++++++ .../TP03/Exercise1/GridWindowClosedEvent.java | 19 ++++ DEV/DEV3.1/TP03/Exercise1/Main.java | 7 ++ DEV/DEV3.1/TP03/Exercise2/Champ.java | 18 ++++ DEV/DEV3.1/TP03/Exercise2/Database.java | 44 ++++++++++ DEV/DEV3.1/TP03/Exercise2/Main.java | 1 + DEV/DEV3.1/TP03/Exercise2/schema.sql | 21 +++++ SCR/SCR3.1/TP02/Exercise1/answer.txt | 11 +++ 14 files changed, 342 insertions(+), 48 deletions(-) create mode 100644 DEV/DEV3.1/TP01/Exercise2/Database.java create mode 100644 DEV/DEV3.1/TP01/Exercise2/Main.java create mode 100644 DEV/DEV3.1/TP01/Exercise2/ScoreBoardWindow.java create mode 100644 DEV/DEV3.1/TP02/Part1/ImageManager.java create mode 100644 DEV/DEV3.1/TP03/Exercise1/GridWindow.java create mode 100644 DEV/DEV3.1/TP03/Exercise1/GridWindowClosedEvent.java create mode 100644 DEV/DEV3.1/TP03/Exercise1/Main.java create mode 100644 DEV/DEV3.1/TP03/Exercise2/Champ.java create mode 100644 DEV/DEV3.1/TP03/Exercise2/Database.java create mode 100644 DEV/DEV3.1/TP03/Exercise2/Main.java create mode 100644 DEV/DEV3.1/TP03/Exercise2/schema.sql create mode 100644 SCR/SCR3.1/TP02/Exercise1/answer.txt diff --git a/DEV/DEV3.1/TP01/Exercise2/Database.java b/DEV/DEV3.1/TP01/Exercise2/Database.java new file mode 100644 index 0000000..a85b021 --- /dev/null +++ b/DEV/DEV3.1/TP01/Exercise2/Database.java @@ -0,0 +1,68 @@ +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.ArrayList; + +public class Database { + + private Connection connection; + + public Database() { + try { + Class.forName("org.mariadb.jdbc.Driver"); + + this.connection = DriverManager.getConnection( + "jdbc:mariadb://dwarves.iut-fbleau.fr/baudrier", + "baudrier", + "baudrier" + ); + } catch(ClassNotFoundException exception) { + System.err.println("Class 'org.mariadb.jdbc.Driver' not found."); + } catch(SQLException exception) { + System.err.println("Error while trying to connect to the database"); + } + } + + + public Votes getVotesFromCompetitor(String country) { + try { + PreparedStatement query = this.connection.prepareStatement( + "SELECT VoterVotes.points " + + "FROM Votes " + + "JOIN Countries AS Competitors ON Votes.competitor_id = Competitors.id " + + "JOIN Countries AS Voters ON Votes.voter_id = Voters.id " + + "WHERE Competitors.name = ?;" + ); + query.setString(1, country); + + ResultSet result = query.executeQuery(); + ArrayList voteList = new ArrayList<>(); + + while(result.next()) { + voteList.add(new Vote(result.getString("name"), result.getInt("points"))); + } + + try { + query.close(); + } catch(SQLException exception) { + System.err.println("Error while trying to close the query."); + } + + return new Votes(country, voteList); + } catch(SQLException exception) { + System.err.println("Error while trying to prepare or execute the query (" + exception.getMessage() + ")."); + } + + return null; + } + + public void close() { + try { + this.connection.close(); + } catch(SQLException exception) { + System.err.println("Error while trying to close the connection."); + } + } +} diff --git a/DEV/DEV3.1/TP01/Exercise2/Main.java b/DEV/DEV3.1/TP01/Exercise2/Main.java new file mode 100644 index 0000000..c1b4223 --- /dev/null +++ b/DEV/DEV3.1/TP01/Exercise2/Main.java @@ -0,0 +1,3 @@ +public class Main { + +} diff --git a/DEV/DEV3.1/TP01/Exercise2/ScoreBoardWindow.java b/DEV/DEV3.1/TP01/Exercise2/ScoreBoardWindow.java new file mode 100644 index 0000000..f1e66f4 --- /dev/null +++ b/DEV/DEV3.1/TP01/Exercise2/ScoreBoardWindow.java @@ -0,0 +1,5 @@ +import javax.swing.JFrame; + +public class ScoreBoardWindow extends JFrame { + +} \ No newline at end of file diff --git a/DEV/DEV3.1/TP02/Part1/ClickSwapImageEvent.java b/DEV/DEV3.1/TP02/Part1/ClickSwapImageEvent.java index 09c8cb8..d35077f 100644 --- a/DEV/DEV3.1/TP02/Part1/ClickSwapImageEvent.java +++ b/DEV/DEV3.1/TP02/Part1/ClickSwapImageEvent.java @@ -10,11 +10,14 @@ public class ClickSwapImageEvent extends MouseAdapter { } public void mouseClicked(MouseEvent event) { + System.out.println(this.window.manager.cursor); if(event.getX() > this.window.getWidth() / 2) { this.window.nextImage(); + System.out.println(this.window.manager.cursor); return; } - this.window.lastImage(); + this.window.previousImage(); + System.out.println(this.window.manager.cursor); } } diff --git a/DEV/DEV3.1/TP02/Part1/ImageManager.java b/DEV/DEV3.1/TP02/Part1/ImageManager.java new file mode 100644 index 0000000..f22afc0 --- /dev/null +++ b/DEV/DEV3.1/TP02/Part1/ImageManager.java @@ -0,0 +1,49 @@ +import java.io.File; + +import javax.swing.ImageIcon; + +public class ImageManager { + + private ImageIcon[] images; + public int cursor; + + public ImageManager() { + File resourcesDirectory = new File("../resources/"); + + if(resourcesDirectory.exists() && resourcesDirectory.isDirectory()) { + File[] files = resourcesDirectory.listFiles(); + + if(files != null) { + this.images = new ImageIcon[files.length]; + this.cursor = 0; + + for(File file : files) { + this.images[this.cursor] = new ImageIcon(file.getPath()); + this.cursor++; + } + + this.cursor = 0; + } + } + } + + public ImageIcon getNextImage() { + if(this.cursor >= this.images.length - 1) { + this.cursor = 0; + } else { + this.cursor++; + } + + return this.images[this.cursor]; + } + + public ImageIcon getPreviousImage() { + if(this.cursor <= 0) { + this.cursor = this.images.length - 1; + } else { + this.cursor--; + } + + return this.images[this.cursor]; + } +} diff --git a/DEV/DEV3.1/TP02/Part1/ImageWindow.java b/DEV/DEV3.1/TP02/Part1/ImageWindow.java index 415cc91..98427bc 100644 --- a/DEV/DEV3.1/TP02/Part1/ImageWindow.java +++ b/DEV/DEV3.1/TP02/Part1/ImageWindow.java @@ -1,86 +1,45 @@ -import java.io.File; - -import javax.swing.ImageIcon; import javax.swing.JFrame; import javax.swing.JLabel; public class ImageWindow extends JFrame { - private ImageIcon[] images; - private int cursor; - + public ImageManager manager; private JLabel label; public ImageWindow() { + this.manager = new ImageManager(); + this.setSize(1024, 1024); this.setLocationRelativeTo(null); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); - File resourcesDirectory = new File("../resources/"); - - if(resourcesDirectory.exists() && resourcesDirectory.isDirectory()) { - File[] files = resourcesDirectory.listFiles(); - - if(files != null) { - this.images = new ImageIcon[files.length]; - this.cursor = 0; - - for(File file : files) { - this.images[this.cursor] = new ImageIcon(file.getPath()); - this.cursor++; - } - } - } - this.addMouseListener(new ClickSwapImageEvent(this)); - this.cursor = 0; - this.nextImage(); } public void nextImage() { - if(this.images.length == 0) return; - if(this.label != null) { this.remove(this.label); } - this.label = new JLabel(this.images[cursor]); + this.label = new JLabel(this.manager.getNextImage()); this.label.setVerticalAlignment(JLabel.CENTER); this.label.setHorizontalAlignment(JLabel.CENTER); - if(this.cursor + 1 >= this.images.length) { - this.cursor = 0; - } else { - this.cursor++; - } - - System.out.println(this.cursor); - this.add(label); this.revalidate(); } - public void lastImage() { - if(this.images.length == 0) return; - + public void previousImage() { if(this.label != null) { this.remove(this.label); } - this.label = new JLabel(this.images[cursor]); + this.label = new JLabel(this.manager.getPreviousImage()); this.label.setVerticalAlignment(JLabel.CENTER); this.label.setHorizontalAlignment(JLabel.CENTER); - if(this.cursor - 1 < 0) { - this.cursor = this.images.length - 1; - } else { - this.cursor--; - } - - System.out.println(this.cursor); - this.add(label); this.revalidate(); } diff --git a/DEV/DEV3.1/TP03/Exercise1/GridWindow.java b/DEV/DEV3.1/TP03/Exercise1/GridWindow.java new file mode 100644 index 0000000..c99869a --- /dev/null +++ b/DEV/DEV3.1/TP03/Exercise1/GridWindow.java @@ -0,0 +1,86 @@ +import java.awt.Dimension; +import java.awt.GridBagConstraints; +import java.awt.GridBagLayout; +import java.awt.Insets; + +import javax.swing.JButton; +import javax.swing.JFrame; + +public class GridWindow extends JFrame { + + public GridWindow() { + this.setSize(200, 200); + this.setLocationRelativeTo(null); + this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); + + this.getContentPane().setLayout(new GridBagLayout()); + + this.placeButtons(); + + this.addWindowListener(new GridWindowClosedEvent(this)); + } + + private void placeButtons() { + GridBagConstraints constraints = new GridBagConstraints(); + + JButton one = new JButton("1"); + constraints.gridx = 0; + constraints.gridy = 0; + constraints.gridwidth = 2; + constraints.gridheight = 1; + constraints.fill = GridBagConstraints.BOTH; + constraints.insets = new Insets(0, 0, 0, 0); + constraints.weightx = 1.0; + constraints.weighty = 1.0; + + this.getContentPane().add(one, constraints); + + JButton two = new JButton("2"); + constraints.gridx = 2; + constraints.gridy = 0; + constraints.gridwidth = 1; + constraints.gridheight = 2; + constraints.fill = GridBagConstraints.BOTH; + constraints.insets = new Insets(0, 0, 0, 0); + constraints.weightx = 1.0; + constraints.weighty = 1.0; + + this.getContentPane().add(two, constraints); + + JButton three = new JButton("3"); + constraints.gridx = 1; + constraints.gridy = 2; + constraints.gridwidth = 2; + constraints.gridheight = 1; + constraints.fill = GridBagConstraints.BOTH; + constraints.insets = new Insets(0, 0, 0, 0); + constraints.weightx = 1.0; + constraints.weighty = 1.0; + + this.getContentPane().add(three, constraints); + + JButton four = new JButton("4"); + constraints.gridx = 0; + constraints.gridy = 1; + constraints.gridwidth = 1; + constraints.gridheight = 2; + constraints.fill = GridBagConstraints.BOTH; + constraints.insets = new Insets(0, 0, 0, 0); + constraints.weightx = 1.0; + constraints.weighty = 1.0; + + this.getContentPane().add(four, constraints); + + JButton five = new JButton("5"); + constraints.gridx = 1; + constraints.gridy = 1; + constraints.gridwidth = 1; + constraints.gridheight = 1; + constraints.fill = GridBagConstraints.NONE; + constraints.insets = new Insets(0, 0, 0, 0); + constraints.weightx = 0.0; + constraints.weighty = 0.0; + + this.getContentPane().add(five, constraints); + } +} \ No newline at end of file diff --git a/DEV/DEV3.1/TP03/Exercise1/GridWindowClosedEvent.java b/DEV/DEV3.1/TP03/Exercise1/GridWindowClosedEvent.java new file mode 100644 index 0000000..7c83cbb --- /dev/null +++ b/DEV/DEV3.1/TP03/Exercise1/GridWindowClosedEvent.java @@ -0,0 +1,19 @@ +import java.awt.event.WindowAdapter; +import java.awt.event.WindowEvent; + +import javax.swing.JOptionPane; + +public class GridWindowClosedEvent extends WindowAdapter { + + private GridWindow window; + + public GridWindowClosedEvent(GridWindow window) { + this.window = window; + } + + @Override + public void windowClosing(WindowEvent event) { + int result = JOptionPane.showConfirmDialog(this.window, "Are you sure you want to close this window?", "Close current window", JOptionPane.YES_NO_OPTION); + if(result == JOptionPane.YES_OPTION) this.window.dispose(); + } +} diff --git a/DEV/DEV3.1/TP03/Exercise1/Main.java b/DEV/DEV3.1/TP03/Exercise1/Main.java new file mode 100644 index 0000000..3be08cd --- /dev/null +++ b/DEV/DEV3.1/TP03/Exercise1/Main.java @@ -0,0 +1,7 @@ +public class Main { + + public static void main(String[] args) { + GridWindow window = new GridWindow(); + window.setVisible(true); + } +} \ No newline at end of file diff --git a/DEV/DEV3.1/TP03/Exercise2/Champ.java b/DEV/DEV3.1/TP03/Exercise2/Champ.java new file mode 100644 index 0000000..b85ea17 --- /dev/null +++ b/DEV/DEV3.1/TP03/Exercise2/Champ.java @@ -0,0 +1,18 @@ +public class Champ { + + private String code; + private String[] modules; + + public Champ(String code, String[] modules) { + this.code = code; + this.modules = modules; + } + + public String getCode() { + return this.code; + } + + public String[] getModules() { + return this.modules; + } +} diff --git a/DEV/DEV3.1/TP03/Exercise2/Database.java b/DEV/DEV3.1/TP03/Exercise2/Database.java new file mode 100644 index 0000000..22abd7b --- /dev/null +++ b/DEV/DEV3.1/TP03/Exercise2/Database.java @@ -0,0 +1,44 @@ +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.ArrayList; + +public class Database { + + private Connection connection; + + public Database() { + try { + Class.forName("org.mariadb.jdbc.Driver"); + + this.connection = DriverManager.getConnection( + "jdbc:mariadb://dwarves.iut-fbleau.fr/baudrier", + "baudrier", + "baudrier" + ); + } catch(ClassNotFoundException exception) { + System.err.println("Class 'org.mariadb.jdbc.Driver' not found."); + } catch(SQLException exception) { + System.err.println("Error while trying to connect to the database"); + } + } + + public Champ[] getAllData() { + PreparedStatement query = this.connection.prepareStatement("SELECT id, code FROM Champ"); + ResultSet result = query.executeQuery(); + + while(result.next()) { + query = this.connection.prepareStatement("SELECT code FROM Modules WHERE champId = " + result.getInt("id")); + } + } + + public void close() { + try { + this.connection.close(); + } catch(SQLException exception) { + System.err.println("Error while trying to close the connection."); + } + } +} diff --git a/DEV/DEV3.1/TP03/Exercise2/Main.java b/DEV/DEV3.1/TP03/Exercise2/Main.java new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/DEV/DEV3.1/TP03/Exercise2/Main.java @@ -0,0 +1 @@ + diff --git a/DEV/DEV3.1/TP03/Exercise2/schema.sql b/DEV/DEV3.1/TP03/Exercise2/schema.sql new file mode 100644 index 0000000..3f53b8c --- /dev/null +++ b/DEV/DEV3.1/TP03/Exercise2/schema.sql @@ -0,0 +1,21 @@ +CREATE TABLE Champ ( + id INT PRIMARY KEY AUTO_INCREMENT, + code VARCHAR(20) +); + +CREATE TABLE Module ( + id INT PRIMARY KEY AUTO_INCREMENT, + champId INT REFERENCES Champ(id), + code VARCHAR(20) +); + +INSERT INTO Champ(code) VALUES ("APL"); +INSERT INTO Champ(code) VALUES ("WIM"); + +INSERT INTO Module(champId, code) VALUES (1, "M1102"); +INSERT INTO Module(champId, code) VALUES (1, "M1103"); +INSERT INTO Module(champId, code) VALUES (1, "M3103"); +INSERT INTO Module(champId, code) VALUES (2, "M1105"); +INSERT INTO Module(champId, code) VALUES (2, "M3104"); +INSERT INTO Module(champId, code) VALUES (2, "M4103C"); +INSERT INTO Module(champId, code) VALUES (2, "M4104C"); \ No newline at end of file diff --git a/SCR/SCR3.1/TP02/Exercise1/answer.txt b/SCR/SCR3.1/TP02/Exercise1/answer.txt new file mode 100644 index 0000000..2af9d8b --- /dev/null +++ b/SCR/SCR3.1/TP02/Exercise1/answer.txt @@ -0,0 +1,11 @@ +Explications techniques + + Fonctions standards (fopen, fread, fwrite) : + Ces fonctions utilisent une mémoire tampon (buffer) en interne. Même si tu demandes de lire/écrire un octet à la fois, + la bibliothèque C va lire ou écrire plusieurs octets (souvent 4096, la taille d’un bloc) d’un coup en interne, + ce qui réduit le nombre d’appels système réels et accélère le programme. + Vérifie avec strace : un seul appel read ou write pour plusieurs appels fread/fwrite d’un octet ! + + Appels systèmes (open, read, write, close) : + Ici, chaque appel à read() ou write() provoque un appel système pour chaque octet. + Le passage du mode utilisateur au noyau (syscall) est coûteux, donc le programme est beaucoup plus lent.