* Code cleanup;
authorJérôme Benoit <jerome.benoit@piment-noir.org>
Fri, 18 Jan 2019 10:15:12 +0000 (11:15 +0100)
committerJérôme Benoit <jerome.benoit@piment-noir.org>
Fri, 18 Jan 2019 10:15:12 +0000 (11:15 +0100)
* Add an listener on the Weigth view.

Signed-off-by: Jérôme Benoit <jerome.benoit@piment-noir.org>
src/Country.java
src/Person.java
src/PersonLeftPanel.java
src/PersonRightPanel.java
src/Region.java
src/RegionView.java
src/WeightView.java

index 2bb786a2c33d92fb2d64eb9fe6302bc1472062f4..c663ff0af66b22c2c8c9bdd22845f51cbb434d00 100644 (file)
@@ -1,9 +1,11 @@
+import javax.swing.*;
+
 public class Country {
     private String name;
     private String region;
     private double lat;
     private double lng;
-
+    private ImageIcon flag;
 
     public Country(String name, String region, double lat, double lng) {
         setName(name);
@@ -32,6 +34,21 @@ public class Country {
         this.lng = lng;
     }
 
+    public int distanceTo(Country country) {
+        if ((this.lat == country.lat) && (this.lng == country.lng)) {
+            return 0;
+        } else {
+            double theta = this.lng - country.lng;
+            Double dist = Math.sin(Math.toRadians(this.lat)) * Math.sin(Math.toRadians(country.lat)) + Math.cos(Math.toRadians(this.lat)) * Math.cos(Math.toRadians(country.lat)) * Math.cos(Math.toRadians(theta));
+            dist = Math.acos(dist);
+            dist = Math.toDegrees(dist);
+            dist = dist * 60 * 1.1515;
+            // Kilometers
+            dist = dist * 1.609344;
+            return dist.intValue();
+        }
+    }
+
     @Override
     public String toString() {
         return "Country{" +
index 5d9d812924ec1daf2c66351c4f90272a397e7bce..6fbdf6ac55a644af4baf0043d23f89c40b240d75 100644 (file)
@@ -3,6 +3,7 @@ import java.util.Comparator;
 public class Person implements Comparator<Person> {
     private Firstname firstname;
     private Origin origin;
+    private Country country;
     private Size size;
     private Weight weight;
     private Eye eye;
@@ -42,6 +43,14 @@ public class Person implements Comparator<Person> {
         return origin;
     }
 
+    public void setCountry(Country country) {
+        this.country = country;
+    }
+
+    public Country getCountry() {
+        return country;
+    }
+
     public void setPersonSize(Size size) {
         this.size = size;
     }
@@ -79,6 +88,7 @@ public class Person implements Comparator<Person> {
         return "Person{" +
                 "firstname=" + firstname.getFirstname() +
                 ", origin=" + origin.getCountry() +
+                ", country=" + country +
                 ", size=" + size.getSize() +
                 ", weight=" + weight.getWeight() +
                 ", eye=" + eye.getStrColor() + "(" + this.getEye().getColor().getRed() + "," + this.getEye().getColor().getGreen() + "," + this.getEye().getColor().getBlue() + ")" +
@@ -88,8 +98,8 @@ public class Person implements Comparator<Person> {
 
     public Integer distanceTo(Person person) {
         return this.getFirstname().distanceTo(person.getFirstname()) + this.getOrigin().distanceTo(person.getOrigin())
-                + this.getPersonSize().distanceTo(person.getPersonSize()) + this.getWeight().distanceTo(person.getWeight())
-                + this.getEye().distanceTo(person.getEye());
+                + this.getCountry().distanceTo(person.getCountry()) + this.getPersonSize().distanceTo(person.getPersonSize())
+                + this.getWeight().distanceTo(person.getWeight()) + this.getEye().distanceTo(person.getEye());
     }
 
     @Override
index d01abd000b1ef5ee56f946d2d034e2f9cc6f53dd..e8cb6f71d5a8dc5a8896c9f10887beb57dccacb4 100644 (file)
@@ -14,7 +14,7 @@ public class PersonLeftPanel extends JPanel {
         try {
             this.personImage = ImageIO.read(new File("data/personImage.png"));
         } catch (IOException e) {
-            System.out.println(e.getStackTrace());
+            e.printStackTrace();
         }
     }
 
@@ -45,4 +45,10 @@ public class PersonLeftPanel extends JPanel {
         g.fillOval(50, 50, 6, 3);
         g.fillOval(62, 50, 6, 3);
     }
+
+    public void drawWeight(int weight) {
+        Graphics g = this.getGraphics();
+        g.setColor(Color.black);
+        g.fillOval(44, 76, 30, (weight * 20) / 600);
+    }
 }
index 62067f6689bc2aa4d47e0e0a4c692b34fb9159ac..f56b32db4dd1cbeff9cf50dbd365cdcad9b7ce11 100644 (file)
@@ -15,7 +15,7 @@ public class PersonRightPanel extends JPanel {
         try {
             this.personImage = ImageIO.read(new File("data/personImage.png"));
         } catch (IOException e) {
-            System.out.println(e.getStackTrace());
+            e.printStackTrace();
         }
     }
 
@@ -44,8 +44,12 @@ public class PersonRightPanel extends JPanel {
         int imageWidth = 120;
         int imageHeight = 180;
         g2d.drawImage(this.personImage.getScaledInstance(imageWidth / 2, imageHeight / 2, Image.SCALE_SMOOTH), imageWidth / 4, imageHeight / 4, this);
+        // Eyes
         g2d.setColor(personObj.getEye().getColor());
         g2d.fillOval(50, 50, 6, 3);
         g2d.fillOval(62, 50, 6, 3);
+        // Weigth
+        g2d.setColor(Color.black);
+        g2d.fillOval(44, 76, 30, (personObj.getWeight().getWeight() * 20) / 600);
     }
 }
index eac5c9561839d29a74ce91efd0768c24b236616a..7c50c89a3184138ed043acb7cd13ccbc81de8328 100644 (file)
@@ -5,7 +5,6 @@ import java.util.List;
 import java.util.Scanner;
 
 public class Region {
-
     private String csvFile = "data/countries.csv";
     private String[] continents = {"Africa", "Americas", "Asia", "Europe", "Oceania"};
     private ArrayList<Country> countryArrayListEurope = new ArrayList<>();
@@ -14,7 +13,6 @@ public class Region {
     private ArrayList<Country> countryArrayListAsia = new ArrayList<>();
     private ArrayList<Country> countryArrayListOceania = new ArrayList<>();
 
-
     public Region() {
         this.loadCSVCountries();
     }
@@ -44,7 +42,6 @@ public class Region {
     }
 
     public void loadCSVCountries() {
-        CSVUtils csvUtils = new CSVUtils();
         Scanner scanner = null;
         try {
             scanner = new Scanner(new File(csvFile));
@@ -52,32 +49,27 @@ public class Region {
             e.printStackTrace();
         }
         while (scanner.hasNext()) {
-
-            List<String> line = csvUtils.parseLine(scanner.nextLine());
-
-            String c3 = line.get(0);
-            String[] splitArray = c3.split(",");
-            String c2 = line.get(16);
-            String[] splitArray2 = c2.split(",");
-
+            List<String> line = CSVUtils.parseLine(scanner.nextLine());
+            String countryList = line.get(0);
+            String[] countryArray = countryList.split(",");
+            String latLng = line.get(16);
+            String[] latLngArray = latLng.split(",");
             if (line.get(12).equals("Europe")) {
-                Country c = new Country(splitArray[1], line.get(12), Double.parseDouble(splitArray2[0]), Double.parseDouble(splitArray2[1]));
+                Country c = new Country(countryArray[1], line.get(12), Double.parseDouble(latLngArray[0]), Double.parseDouble(latLngArray[1]));
                 countryArrayListEurope.add(c);
             } else if (line.get(12).equals("Africa")) {
-                Country c = new Country(splitArray[1], line.get(12), Double.parseDouble(splitArray2[0]), Double.parseDouble(splitArray2[1]));
+                Country c = new Country(countryArray[1], line.get(12), Double.parseDouble(latLngArray[0]), Double.parseDouble(latLngArray[1]));
                 countryArrayListAfrica.add(c);
             } else if (line.get(12).equals("Americas")) {
-                Country c = new Country(splitArray[1], line.get(12), Double.parseDouble(splitArray2[0]), Double.parseDouble(splitArray2[1]));
+                Country c = new Country(countryArray[1], line.get(12), Double.parseDouble(latLngArray[0]), Double.parseDouble(latLngArray[1]));
                 countryArrayListAmericas.add(c);
             } else if (line.get(12).equals("Asia")) {
-                Country c = new Country(splitArray[1], line.get(12), Double.parseDouble(splitArray2[0]), Double.parseDouble(splitArray2[1]));
+                Country c = new Country(countryArray[1], line.get(12), Double.parseDouble(latLngArray[0]), Double.parseDouble(latLngArray[1]));
                 countryArrayListAsia.add(c);
             } else if (line.get(12).equals("Oceania")) {
-                Country c = new Country(splitArray[1], line.get(12), Double.parseDouble(splitArray2[0]), Double.parseDouble(splitArray2[1]));
+                Country c = new Country(countryArray[1], line.get(12), Double.parseDouble(latLngArray[0]), Double.parseDouble(latLngArray[1]));
                 countryArrayListOceania.add(c);
             }
-
-
         }
         scanner.close();
         /*Utils.displayArrayList(countryArrayListOceania);
@@ -85,7 +77,5 @@ public class Region {
         Utils.displayArrayList(countryArrayListAmericas);
         Utils.displayArrayList(countryArrayListAsia);
         Utils.displayArrayList(countryArrayListEurope);*/
-
     }
-
 }
\ No newline at end of file
index 25b646dc19c1fd9508ccce3dbf723e6b98a5522d..7e5c69fc76248d9d8d37028e44d8b18f96bc716a 100644 (file)
@@ -4,7 +4,6 @@ import java.util.ListIterator;
 
 public class RegionView extends JPanel {
     private Region regionObj;
-    //private JLabel label;
     private JTree tree;
 
     RegionView(Region regionObj) {
@@ -13,15 +12,12 @@ public class RegionView extends JPanel {
 
         for (String continent : regionObj.getContinents()) {
             DefaultMutableTreeNode topContinent = new DefaultMutableTreeNode(continent);
-            System.out.println(continent.equals("Europe"));
-
             if (continent.equals("Europe")) {
                 ListIterator<Country> iter = regionObj.getCountryArrayListEurope().listIterator();
                 while (iter.hasNext()) {
                     Country countryCursor = iter.next();
                     topContinent.add(new DefaultMutableTreeNode(countryCursor.getName()));
                     top.add(topContinent);
-
                 }
             } else if (continent.equals("Africa")) {
                 ListIterator<Country> iter = regionObj.getCountryArrayListAfrica().listIterator();
@@ -29,7 +25,6 @@ public class RegionView extends JPanel {
                     Country countryCursor = iter.next();
                     topContinent.add(new DefaultMutableTreeNode(countryCursor.getName()));
                     top.add(topContinent);
-
                 }
             } else if (continent.equals("Americas")) {
                 ListIterator<Country> iter = regionObj.getCountryArrayListAmericas().listIterator();
@@ -37,7 +32,6 @@ public class RegionView extends JPanel {
                     Country countryCursor = iter.next();
                     topContinent.add(new DefaultMutableTreeNode(countryCursor.getName()));
                     top.add(topContinent);
-
                 }
             } else if (continent.equals("Asia")) {
                 ListIterator<Country> iter = regionObj.getCountryArrayListAsia().listIterator();
@@ -45,7 +39,6 @@ public class RegionView extends JPanel {
                     Country countryCursor = iter.next();
                     topContinent.add(new DefaultMutableTreeNode(countryCursor.getName()));
                     top.add(topContinent);
-
                 }
             } else if (continent.equals("Oceania")) {
                 ListIterator<Country> iter = regionObj.getCountryArrayListOceania().listIterator();
@@ -53,17 +46,14 @@ public class RegionView extends JPanel {
                     Country countryCursor = iter.next();
                     topContinent.add(new DefaultMutableTreeNode(countryCursor.getName()));
                     top.add(topContinent);
-
                 }
             }
         }
         tree = new JTree(top);
         add(tree);
-
     }
 
     public void setRegionObj(Region regionObj) {
         this.regionObj = regionObj;
-
     }
 }
index 2c41f5ec889c2a73430fb3e1f90a391923a390b5..7e029fa1799330896cd685bfec04b3d68d806c42 100644 (file)
@@ -1,6 +1,8 @@
 import javax.swing.*;
+import java.awt.event.ItemEvent;
+import java.awt.event.ItemListener;
 
-public class WeightView extends JPanel {
+public class WeightView extends JPanel implements ItemListener {
     private Weight weightObj;
     private JLabel label;
     private JComboBox<Integer> comboBox;
@@ -14,6 +16,7 @@ public class WeightView extends JPanel {
             this.comboBox.setSelectedItem(this.getWeightObj().getWeight());
         else
             this.comboBox.setSelectedItem(this.getWeightObj().getDefaultWeight());
+        this.comboBox.addItemListener(this);
         add(label);
         add(comboBox);
     }
@@ -29,4 +32,13 @@ public class WeightView extends JPanel {
     public JComboBox<Integer> getComboBox() {
         return comboBox;
     }
+
+    @Override
+    public void itemStateChanged(ItemEvent event) {
+        if (event.getStateChange() == ItemEvent.SELECTED) {
+            Object item = event.getItem();
+            PersonLeftPanel personLeftPanel = MainWindowsView.getInstance().getCurrentPersonView().getPersonPanel().getLeftPanel();
+            personLeftPanel.drawWeight((int) item);
+        }
+    }
 }