All Downloads are FREE. Search and download functionalities are using the official Maven repository.

eu.hansolo.fx.countries.tools.CountryRegion Maven / Gradle / Ivy

Go to download

Countries is a JavaFX library containing controls and info for countries and cities

There is a newer version: 21.0.3
Show newest version
package eu.hansolo.fx.countries.tools;

import eu.hansolo.fx.countries.Country;
import javafx.scene.paint.Color;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class CountryRegion implements CRegion {
    private String                          name;
    private List                   countries;
    private Map> countryPaths;


    // ******************** Constructors **************************************
    public CountryRegion(final String name, final Country... countries) {
        this.name      = name;
        this.countries = new ArrayList(List.of(countries));
    }


    // ******************** Methods *******************************************
    @Override public String name() { return name; }

    @Override public List getCountries() { return countries; }

    @Override public void setFill(final Color fill) { countries.forEach(country -> country.setFill(fill)); }

    @Override public void setStroke(final Color stroke) { countries.forEach(country -> country.setFill(stroke)); }

    @Override public final List getRegionBounds() {
        double ulx     = 360;
        double uly     = 180;
        double lastULx;
        double lastULy;
        double lrx     = 0;
        double lry     = 0;
        double lastLRx;
        double lastLRy;
        for (Country country : getCountries()) {
            List coords = Country.getCountryBounds().get(country);
            final Point ul = coords.get(0);
            final Point lr = coords.get(1);

            lastULx = ulx;
            lastULy = uly;
            ulx = ul.getX() + 180;
            uly = (ul.getY() - 90.0) * -1;

            ulx = Math.min(ulx, lastULx);
            uly = Math.min(uly, lastULy);

            lastLRx = lrx;
            lastLRy = lry;
            lrx = lr.getX() + 180;
            lry = (lr.getY() - 90.0) * -1;

            lrx = Math.max(lrx, lastLRx);
            lry = Math.max(lry, lastLRy);
        };
        ulx = ulx - 180;
        uly = uly * -1 + 90;
        lrx = lrx - 180;
        lry = lry * -1 + 90;

        Point upperLeft  = new Point(ulx, uly);
        Point lowerRight = new Point(lrx, lry);
        return List.of(upperLeft, lowerRight);
    }

    @Override public final Map> getCountryPaths() {
        if (null == countryPaths) {
            countryPaths = new HashMap<>();
            getCountries().forEach(country -> {
                List paths = new ArrayList<>();
                country.getCopyOfPaths().forEach(countryPath -> paths.add(countryPath));
                countryPaths.put(country, paths);
            });
        }
        return countryPaths;
    }

    @Override public final List getPaths() {
        List paths = new ArrayList<>();
        getCountryPaths().values().forEach(cps -> cps.forEach(countryPath -> paths.add(countryPath)));
        return paths;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy