From: Jérôme Benoit Date: Fri, 18 Jan 2019 10:15:12 +0000 (+0100) Subject: * Code cleanup; X-Git-Url: https://git.piment-noir.org/?p=Persons_Comparator.git;a=commitdiff_plain;h=e16e3b1587a729dd1e2d39bd4426831136376aa7 * Code cleanup; * Add an listener on the Weigth view. Signed-off-by: Jérôme Benoit --- diff --git a/src/Country.java b/src/Country.java index 2bb786a..c663ff0 100644 --- a/src/Country.java +++ b/src/Country.java @@ -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{" + diff --git a/src/Person.java b/src/Person.java index 5d9d812..6fbdf6a 100644 --- a/src/Person.java +++ b/src/Person.java @@ -3,6 +3,7 @@ import java.util.Comparator; public class Person implements Comparator { 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 { 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 { 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 { 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 diff --git a/src/PersonLeftPanel.java b/src/PersonLeftPanel.java index d01abd0..e8cb6f7 100644 --- a/src/PersonLeftPanel.java +++ b/src/PersonLeftPanel.java @@ -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); + } } diff --git a/src/PersonRightPanel.java b/src/PersonRightPanel.java index 62067f6..f56b32d 100644 --- a/src/PersonRightPanel.java +++ b/src/PersonRightPanel.java @@ -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); } } diff --git a/src/Region.java b/src/Region.java index eac5c95..7c50c89 100644 --- a/src/Region.java +++ b/src/Region.java @@ -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 countryArrayListEurope = new ArrayList<>(); @@ -14,7 +13,6 @@ public class Region { private ArrayList countryArrayListAsia = new ArrayList<>(); private ArrayList 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 line = csvUtils.parseLine(scanner.nextLine()); - - String c3 = line.get(0); - String[] splitArray = c3.split(","); - String c2 = line.get(16); - String[] splitArray2 = c2.split(","); - + List 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 diff --git a/src/RegionView.java b/src/RegionView.java index 25b646d..7e5c69f 100644 --- a/src/RegionView.java +++ b/src/RegionView.java @@ -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 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 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 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 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 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; - } } diff --git a/src/WeightView.java b/src/WeightView.java index 2c41f5e..7e029fa 100644 --- a/src/WeightView.java +++ b/src/WeightView.java @@ -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 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 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); + } + } }