Merge remote-tracking branch 'origin/master'
authorSylvain Papa <sylvain.papa@yahoo.fr>
Thu, 17 Jan 2019 15:04:51 +0000 (16:04 +0100)
committerSylvain Papa <sylvain.papa@yahoo.fr>
Thu, 17 Jan 2019 15:04:51 +0000 (16:04 +0100)
src/CSVUtils.java
src/Eye.java
src/EyeView.java
src/Main.java
src/MainWindowsView.java
src/PersonLeftPanel.java [new file with mode: 0644]
src/PersonPanel.java
src/PersonRightPanel.java [new file with mode: 0644]
src/PersonView.java

index 08a1ef91b18171f58fa1181aad2e12f520fe5fee..d586b005d369824b7e849e79346b80f421b15cca 100644 (file)
@@ -6,20 +6,20 @@ public class CSVUtils {
     private static final char DEFAULT_SEPARATOR = ',';
     private static final char DEFAULT_QUOTE = '"';
 
-    public static List<String> parseLine(String cvsLine) {
-        return parseLine(cvsLine, DEFAULT_SEPARATOR, DEFAULT_QUOTE);
+    public static List<String> parseLine(String csvLine) {
+        return parseLine(csvLine, DEFAULT_SEPARATOR, DEFAULT_QUOTE);
     }
 
-    public static List<String> parseLine(String cvsLine, char separators) {
-        return parseLine(cvsLine, separators, DEFAULT_QUOTE);
+    public static List<String> parseLine(String csvLine, char separators) {
+        return parseLine(csvLine, separators, DEFAULT_QUOTE);
     }
 
-    public static List<String> parseLine(String cvsLine, char separators, char customQuote) {
+    public static List<String> parseLine(String csvLine, char separators, char customQuote) {
 
         List<String> result = new ArrayList<>();
 
         //if empty, return!
-        if (cvsLine == null && cvsLine.isEmpty()) {
+        if (csvLine == null && csvLine.isEmpty()) {
             return result;
         }
 
@@ -36,7 +36,7 @@ public class CSVUtils {
         boolean startCollectChar = false;
         boolean doubleQuotesInColumn = false;
 
-        char[] chars = cvsLine.toCharArray();
+        char[] chars = csvLine.toCharArray();
 
         for (char ch : chars) {
 
index 822e6f83dd3fc7aabc5bcf5d864671c828c6f999..5177333340ccaae5405fce6264d927319ef3aa16 100644 (file)
@@ -7,6 +7,7 @@ public class Eye {
     private String[] colorsList = {"black", "blue", "brown", "green"};
 
     Eye() {
+        //this.color = Color.white;
         sortColorList();
     }
 
index 2f892e4b99e47239078137243c3554caaa181363..48ad0b5636d36d28b8f1cbaa9565d19e7b0d905d 100644 (file)
@@ -1,7 +1,10 @@
 import javax.swing.*;
+import javax.swing.event.ListSelectionEvent;
+import javax.swing.event.ListSelectionListener;
+import java.awt.*;
 import java.util.Arrays;
 
-public class EyeView extends JPanel {
+public class EyeView extends JPanel implements ListSelectionListener {
     private Eye eyeObj;
     private JLabel label;
     private JList colorsList;
@@ -17,6 +20,8 @@ public class EyeView extends JPanel {
         this.colorsList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
         this.colorsList.setLayoutOrientation(JList.VERTICAL);
         this.colorsList.setSelectedIndex(Arrays.asList(this.getEyeObj().getColorsList()).indexOf(this.getEyeObj().getStrColor()));
+        this.colorsList.setCellRenderer(new EyeCellRenderer());
+        this.colorsList.addListSelectionListener(this);
         add(label);
         add(colorsList);
     }
@@ -38,4 +43,24 @@ public class EyeView extends JPanel {
     public JList getColorsList() {
         return colorsList;
     }
+
+    @Override
+    public void valueChanged(ListSelectionEvent listSelectionEvent) {
+        Eye currentEye = new Eye((String) getColorsList().getSelectedValue());
+        PersonLeftPanel personLeftPanel = MainWindowsView.getInstance().getCurrentPersonView().getPersonPanel().getLeftPanel();
+        personLeftPanel.drawEyes(currentEye.getColor());
+    }
+
+    private static class EyeCellRenderer extends DefaultListCellRenderer {
+        public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
+            Component c = super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
+            Eye currentEye = new Eye((String) list.getModel().getElementAt(index));
+            c.setBackground(currentEye.getColor());
+            c.setForeground(Color.WHITE);
+            if (isSelected) {
+                setBackground(getBackground().darker());
+            }
+            return c;
+        }
+    }
 }
index a3bc099b9f6dac2aaf98b69ee1b4952140f45dc2..17cf896c521dc308a5b1fb595f539beb48bb3963 100644 (file)
@@ -80,7 +80,8 @@ public class Main {
         //creating and showing this application's GUI.
         javax.swing.SwingUtilities.invokeLater(new Runnable() {
             public void run() {
-                MainWindowsView mainWindows = new MainWindowsView(programName, emptyPersonView);
+                MainWindowsView mainWindows = MainWindowsView.getInstance();
+                mainWindows.setMainWindowsView(programName, emptyPersonView);
                 mainWindows.showGUI();
             }
         });
index df6839f9813916af37709317683a550835ca2046..703792124cb31db55c285d7cd3ff79d65e9c00ec 100644 (file)
@@ -7,12 +7,18 @@ import java.awt.event.KeyEvent;
 import java.util.ArrayList;
 
 public class MainWindowsView extends JFrame {
+    private static MainWindowsView ourInstance = new MainWindowsView();
+    private PersonView currentPersonView;
 
-    MainWindowsView(String title, PersonView view) {
+    private MainWindowsView() {
+    }
+
+    public void setMainWindowsView(String title, PersonView personView) {
+        this.currentPersonView = personView;
         Container panel = getContentPane();
         //Create and set up the window.
         setTitle(title);
-        setSize(view.getDimension());
+        setSize(getCurrentPersonView().getDimension());
         setLocationRelativeTo(null);
         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 
@@ -29,11 +35,19 @@ public class MainWindowsView extends JFrame {
             panel.add(component);
         }
 
-        panel.add(view.getNorthPanel(), BorderLayout.NORTH);
-        panel.add(view.getSouthPanel(), BorderLayout.SOUTH);
-        panel.add(view.getEastPanel(), BorderLayout.EAST);
-        panel.add(view.getWestPanel(), BorderLayout.WEST);
-        panel.add(view.getPersonPanel(), BorderLayout.CENTER);
+        panel.add(getCurrentPersonView().getNorthPanel(), BorderLayout.NORTH);
+        panel.add(getCurrentPersonView().getSouthPanel(), BorderLayout.SOUTH);
+        panel.add(getCurrentPersonView().getEastPanel(), BorderLayout.EAST);
+        panel.add(getCurrentPersonView().getWestPanel(), BorderLayout.WEST);
+        panel.add(getCurrentPersonView().getPersonPanel(), BorderLayout.CENTER);
+    }
+
+    public static MainWindowsView getInstance() {
+        return ourInstance;
+    }
+
+    public PersonView getCurrentPersonView() {
+        return currentPersonView;
     }
 
     /**
@@ -85,7 +99,7 @@ public class MainWindowsView extends JFrame {
         System.out.println("Created GUI on EDT? " +
                 SwingUtilities.isEventDispatchThread());
         //Display the window.
-        //this.pack();
+        this.pack();
         this.setVisible(true);
     }
 }
\ No newline at end of file
diff --git a/src/PersonLeftPanel.java b/src/PersonLeftPanel.java
new file mode 100644 (file)
index 0000000..d01abd0
--- /dev/null
@@ -0,0 +1,48 @@
+import javax.imageio.ImageIO;
+import javax.swing.*;
+import java.awt.*;
+import java.io.File;
+import java.io.IOException;
+
+public class PersonLeftPanel extends JPanel {
+    private JLabel leftLabel = new JLabel();
+    private Image personImage;
+
+    PersonLeftPanel() {
+        leftLabel.setText("Current person");
+        add(leftLabel);
+        try {
+            this.personImage = ImageIO.read(new File("data/personImage.png"));
+        } catch (IOException e) {
+            System.out.println(e.getStackTrace());
+        }
+    }
+
+    private void draw(Graphics g) {
+        // Draw
+        Graphics2D g2d = (Graphics2D) g;
+        int imageWidth = 120;
+        int imageHeight = 180;
+        g2d.drawImage(this.personImage.getScaledInstance(imageWidth / 2, imageHeight / 2, Image.SCALE_SMOOTH), imageWidth / 4, imageHeight / 4, this);
+    }
+
+    @Override
+    public void paintComponent(Graphics g) {
+        super.paintComponent(g);
+        draw(g);
+    }
+
+    //FIXME: redraw on resizing
+    /*@Override
+    public void paint(Graphics g) {
+        super.paint(g);
+        draw(g);
+    }*/
+
+    public void drawEyes(Color color) {
+        Graphics g = this.getGraphics();
+        g.setColor(color);
+        g.fillOval(50, 50, 6, 3);
+        g.fillOval(62, 50, 6, 3);
+    }
+}
index 7c8e2410c386211e68e3495c8995ead56ae3fc11..4f17945fed81377abe86f13555e6194dbd441e01 100644 (file)
@@ -1,59 +1,24 @@
-import javax.imageio.ImageIO;
 import javax.swing.*;
-import java.awt.*;
-import java.awt.geom.Ellipse2D;
-import java.awt.geom.Point2D;
-import java.io.File;
-import java.io.IOException;
 
 public class PersonPanel extends JPanel {
-    private boolean debug = true;
-    private String titleText = new String();
-    private JLabel personLabel = new JLabel();
-    private String contentText = new String();
-    private Image personImage;
+    private PersonLeftPanel leftPanel = new PersonLeftPanel();
+    private PersonRightPanel rightPanel = new PersonRightPanel();
 
-    public PersonPanel(String title) {
-        setTitleText(title);
-        personLabel.setText(this.getTitleText());
-        add(personLabel);
-        try {
-            this.personImage = ImageIO.read(new File("data/personImage.png"));
-        } catch (IOException e) {
-            System.out.println(e.getStackTrace());
-        }
+    public PersonPanel() {
+        setLayout(new BoxLayout(this, BoxLayout.LINE_AXIS));
+        add(leftPanel);
+        add(rightPanel);
     }
 
-    public void setTitleText(String titleText) {
-        this.titleText = titleText;
+    public void setRightContentText(String rightContentText) {
+        this.rightPanel.setContentText(rightContentText);
     }
 
-    public String getTitleText() {
-        return titleText;
+    public PersonLeftPanel getLeftPanel() {
+        return leftPanel;
     }
 
-    public void setContentText(String contentText) {
-        this.contentText = contentText;
-    }
-
-    public String getContentText() {
-        return contentText;
-    }
-
-    public void paintComponent(Graphics g) {
-        super.paintComponent(g);
-
-        // Draw
-        g.setColor(Color.black);
-        if (debug)
-            // Below the JLabel
-            g.drawString(this.getContentText(), 5, 35);
-        Graphics2D g2d = (Graphics2D) g;
-        g2d.drawImage(this.personImage.getScaledInstance(getWidth() / 2, getHeight() / 2, Image.SCALE_SMOOTH), getWidth() / 4, getHeight() / 4, this);
-    }
-
-    private static Ellipse2D getCircleByCenter(Point2D center, double radius) {
-        Ellipse2D.Double myCircle = new Ellipse2D.Double(center.getX() - radius, center.getY() - radius, 2 * radius, 2 * radius);
-        return myCircle;
+    public PersonRightPanel getRightPanel() {
+        return rightPanel;
     }
 }
diff --git a/src/PersonRightPanel.java b/src/PersonRightPanel.java
new file mode 100644 (file)
index 0000000..62067f6
--- /dev/null
@@ -0,0 +1,51 @@
+import javax.imageio.ImageIO;
+import javax.swing.*;
+import java.awt.*;
+import java.io.File;
+import java.io.IOException;
+
+public class PersonRightPanel extends JPanel {
+    private JLabel rightLabel = new JLabel();
+    private String contentText = new String();
+    private Image personImage;
+
+    PersonRightPanel() {
+        rightLabel.setText("Closest person found");
+        add(rightLabel);
+        try {
+            this.personImage = ImageIO.read(new File("data/personImage.png"));
+        } catch (IOException e) {
+            System.out.println(e.getStackTrace());
+        }
+    }
+
+    public void setContentText(String contentText) {
+        this.contentText = contentText;
+    }
+
+    public String getContentText() {
+        return contentText;
+    }
+
+    //FIXME: redraw on resizing
+    /*@Override
+    public void paint(Graphics g) {
+        super.paint(g);
+        drawPerson();
+    }*/
+
+    public void drawPerson(Person personObj) {
+        Graphics g = this.getGraphics();
+        g.clearRect(0, 25, getWidth(), getHeight());
+
+        // Draw
+        g.drawString(getContentText(), 12, 35);
+        Graphics2D g2d = (Graphics2D) g;
+        int imageWidth = 120;
+        int imageHeight = 180;
+        g2d.drawImage(this.personImage.getScaledInstance(imageWidth / 2, imageHeight / 2, Image.SCALE_SMOOTH), imageWidth / 4, imageHeight / 4, this);
+        g2d.setColor(personObj.getEye().getColor());
+        g2d.fillOval(50, 50, 6, 3);
+        g2d.fillOval(62, 50, 6, 3);
+    }
+}
index 6823579d2bc162a43ea1fc6d1b377a1e43c73fe0..1998cece9e3afdbc071b6de389bba316eb06e397 100644 (file)
@@ -5,8 +5,8 @@ import java.awt.event.ActionListener;
 import java.util.ArrayList;
 
 public class PersonView extends JComponent implements ActionListener {
-    private int width = 800;
-    private int height = 500;
+    private int width = 600;
+    private int height = 600;
     private Person personObj;
     private ArrayList<Person> personArrayList;
     private FirstnameView firstnameView;
@@ -18,12 +18,12 @@ public class PersonView extends JComponent implements ActionListener {
     private JPanel southPanel = new JPanel();
     private JPanel eastPanel = new JPanel();
     private JPanel westPanel = new JPanel();
-    private PersonPanel personPanel = new PersonPanel("Person comparison");
+    private PersonPanel personPanel = new PersonPanel();
     private JButton compareButton = new JButton("Compare");
 
     PersonView(Person personObj, ArrayList<Person> personArrayList) {
         setPersonObj(personObj);
-        setFirstnameView(new FirstnameView(15, this.personObj.getFirstname()));
+        setFirstnameView(new FirstnameView(14, this.personObj.getFirstname()));
         setOriginView(new OriginView(this.personObj.getOrigin()));
         setSizeView(new SizeView(this.personObj.getPersonSize()));
         setWeightView(new WeightView(this.personObj.getWeight()));
@@ -35,12 +35,12 @@ public class PersonView extends JComponent implements ActionListener {
         northPanel.add(firstnameView);
         southPanel.setBorder(BorderFactory.createRaisedSoftBevelBorder());
         southPanel.add(compareButton);
-        eastPanel.setLayout(new BoxLayout(this.eastPanel, BoxLayout.Y_AXIS));
+        eastPanel.setLayout(new BoxLayout(this.eastPanel, BoxLayout.PAGE_AXIS));
         eastPanel.setBorder(BorderFactory.createRaisedSoftBevelBorder());
         eastPanel.add(sizeView);
         eastPanel.add(weightView);
         eastPanel.add(eyeView);
-        westPanel.setLayout(new BoxLayout(this.westPanel, BoxLayout.Y_AXIS));
+        westPanel.setLayout(new BoxLayout(this.westPanel, BoxLayout.PAGE_AXIS));
         westPanel.setBorder(BorderFactory.createRaisedSoftBevelBorder());
         westPanel.add(originView);
     }
@@ -182,7 +182,8 @@ public class PersonView extends JComponent implements ActionListener {
             personArrayList.sort(getPersonObj());
             //Utils.displayArrayList(personArrayList);
             Person closestPerson = personArrayList.get(personArrayList.indexOf(this.getPersonObj()) + 1);
-            personPanel.setContentText(closestPerson.toString());
+            personPanel.setRightContentText(closestPerson.getFirstname().getFirstname() + " at distance " + closestPerson.getDistanceFromReference());
+            personPanel.getRightPanel().drawPerson(closestPerson);
         }
     }
 }