APL/DEV 3.2/TP06/Couleurs/Couleurs.java
2022-11-17 16:20:15 +01:00

62 lines
1.9 KiB
Java

import java.io.BufferedInputStream;
import java.awt.event.*;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.StringReader;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.awt.*;
import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
public class Couleurs {
public static void main(String[] args) {
Map<String, Color> colorDict = new HashMap<String,Color>();
try (BufferedReader fs = new BufferedReader(new FileReader("rgb.txt"))) {
String line;
while ((line = fs.readLine()) != null) {
String[] elements = line.trim().split("[\\s\t]+");
Color c = new Color(Integer.parseInt(elements[0]), Integer.parseInt(elements[1]), Integer.parseInt(elements[2]));
String name = elements[3];
colorDict.put(name, c);
}
} catch (Exception e ) {
System.out.println(e);
}
JFrame f = new JFrame();
f.setLayout(new GridLayout(1, 2));
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(400, 250);
String[] nameArray = new String[colorDict.size()];
colorDict.keySet().toArray(nameArray);
JList<String> list = new JList<String>(nameArray);
JScrollPane jpane = new JScrollPane(list);
jpane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
f.add(jpane);
JPanel p = new JPanel();
f.add(p);
list.addListSelectionListener(new ListSelectionListener() {
@Override
public void valueChanged(ListSelectionEvent e) {
p.setBackground(colorDict.get(list.getSelectedValue()));
}
});
f.setVisible(true);
}
}