76 lines
2.9 KiB
Java
76 lines
2.9 KiB
Java
import javax.swing.*;
|
|
import java.awt.*;
|
|
import java.io.*;
|
|
import java.util.*;
|
|
|
|
public class CouleursApp {
|
|
|
|
public static void main(String[] args) {
|
|
// Créer la fenêtre principale
|
|
JFrame frame = new JFrame("Choix des couleurs");
|
|
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
|
frame.setSize(400, 300);
|
|
|
|
// Panneau pour la liste et l'affichage de la couleur
|
|
JPanel panel = new JPanel(new BorderLayout());
|
|
|
|
// Panneau de couleur
|
|
JPanel colorPanel = new JPanel();
|
|
colorPanel.setPreferredSize(new Dimension(400, 200));
|
|
colorPanel.setBackground(Color.WHITE);
|
|
|
|
// Lecture des couleurs à partir du fichier rgb.txt
|
|
Map<String, Color> colorMap = new HashMap<>();
|
|
DefaultListModel<String> listModel = new DefaultListModel<>();
|
|
try (BufferedReader br = new BufferedReader(new FileReader("rgb.txt"))) {
|
|
String line;
|
|
while ((line = br.readLine()) != null) {
|
|
// Chaque ligne est sous la forme : R G B Nom
|
|
String[] parts = line.trim().split("\\s+", 4);
|
|
if (parts.length == 4) {
|
|
try {
|
|
int r = Integer.parseInt(parts[0]);
|
|
int g = Integer.parseInt(parts[1]);
|
|
int b = Integer.parseInt(parts[2]);
|
|
String name = parts[3];
|
|
colorMap.put(name, new Color(r, g, b));
|
|
listModel.addElement(name);
|
|
} catch (NumberFormatException e) {
|
|
System.err.println("Erreur de format dans la ligne : " + line);
|
|
}
|
|
}
|
|
}
|
|
} catch (FileNotFoundException e) {
|
|
System.err.println("Fichier rgb.txt introuvable !");
|
|
return;
|
|
} catch (IOException e) {
|
|
System.err.println("Erreur de lecture dans rgb.txt !");
|
|
return;
|
|
}
|
|
|
|
// Liste JList pour les noms de couleurs
|
|
JList<String> colorList = new JList<>(listModel);
|
|
colorList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
|
|
JScrollPane scrollPane = new JScrollPane(colorList);
|
|
|
|
// Ajouter un écouteur pour gérer les sélections dans la liste
|
|
colorList.addListSelectionListener(e -> {
|
|
String selectedColorName = colorList.getSelectedValue();
|
|
if (selectedColorName != null) {
|
|
Color selectedColor = colorMap.get(selectedColorName);
|
|
colorPanel.setBackground(selectedColor);
|
|
}
|
|
});
|
|
|
|
// Ajouter les composants au panneau principal
|
|
panel.add(scrollPane, BorderLayout.WEST);
|
|
panel.add(colorPanel, BorderLayout.CENTER);
|
|
|
|
// Ajouter le panneau à la fenêtre
|
|
frame.add(panel);
|
|
|
|
// Rendre la fenêtre visible
|
|
frame.setVisible(true);
|
|
}
|
|
}
|