Page 1 of 2 12 LastLast
Results 1 to 20 of 36
Like Tree1Likes

Thread: How to draw map in java

  1. #1
    ark
    ark is offline Senior Member
    Join Date
    Apr 2015
    Posts
    288
    Rep Power
    5

    Default How to draw map in java

    I need to draw the map of roads and cities. But it has some difficulties, using the collection.
    For example I define city in such way.
    Java Code:
    import java.awt.*;
    import java.io.*;
    import java.util.*;
    public class City extends Point{
    int num;
    String name;
    double x;
    double y;
    public City (int num, String name, double x, double y)
    {
    this.num=num;
    this.name=name;
    this.x=x;
    this.y=y;
    }
    } 
    //Then I read the data about cities from the file (in another Java file-class)
    Vector m = new Vector();
    ....
    public void mista ()throws IOException { 
    try {   String fname = "D:\\ucities.txt";
            FileReader fr = new FileReader(fname);
            BufferedReader in = new BufferedReader(fr);
            int i=1;
            String s;
            while ((s = in.readLine())!=null) {
            String[] arr=s.split(" ");
            m.add(new City(i++,arr[0],Double.parseDouble(arr[1]),Double.parseDouble(arr[2])));
            }
            }
    So my object city do not have the names. But I want to create the
    Road class in such way:
    Java Code:
     public class Road implements Comparator {
    int nom;
    City name1;
    City name2;
    public Road (int nom, City name1, City name2)
    {
    this.nom=nom;
    this.name1=name1;
    this.name2=name2;
    }
    }
    Then I want to create the Arraylist(Vector) of edges in such way
    Java Code:
     List<Road> systema=Arraylist<Road>();
    int i=0;
    systema.add(new(i++,name1,name2);
    ...systema.add(new(i++,name-n,name-k)
    ;--So I need to do it maulaly so I want
    to shape the wholelist not in cycle nut in this way entering the names of cities (but it is the fileds of objects, so I do not know how to do it).

  2. #2
    ark
    ark is offline Senior Member
    Join Date
    Apr 2015
    Posts
    288
    Rep Power
    5

    Default Re: How to draw map in java

    Cities are wriiten in file in such way -- Kiev 49.25 28.5
    --and form the unnamed objects in vector.
    So the only way I see to create the Map<String,City> mapp=new Map<String,City>;
    mapp.put(CitiesnameArray[i],m.get(i+1));
    -->So it would relates in such way -- Kiev ==number, Kiev 49.25 28.5.
    So I could create the Arraylist fo roads (edges) in such way--
    Java Code:
    List<Road> systema=Arraylist<Road>();
    int i=0;
    systema.add(new(i++,mapp(get(name1),mapp(get(name2));//--is it possible such construction? To conenct 
    //the roads and cities names(and its objects)?

  3. #3
    ark
    ark is offline Senior Member
    Join Date
    Apr 2015
    Posts
    288
    Rep Power
    5

    Default Re: How to draw map in java

    Java Code:
    import java.io.*;
    import java.util.*;
    public class Mistax {
    Vector m = new Vector();
    String[] NameArray; 
    public void mista ()throws IOException { 
    try {   String fname = "D:\\ucities.txt";
            FileReader fr = new FileReader(fname);
            BufferedReader in = new BufferedReader(fr);
            int i=1;
            String s;
            while ((s = in.readLine())!=null) {
            String[] arr=s.split(" ");
            m.add(new City(i++,arr[0],Double.parseDouble(arr[1]),Double.parseDouble(arr[2])));
            }
            }        
            catch (IOException e) {e.printStackTrace();}
            for (int j=0; j<m.size();j++)
            {NameArray[j]=m.get(j+1).name;} //Here is compiler error--cannot find symbol--symbol  : variable name--
    //location: class java.lang.Object--{NameArray[j]=m.get(j+1).name;}--Why in Object as the name is the second field in City???
    
            }
            public static void main (String[] args){
    Mistax g=new Mistax();
    try { g.mista();}
    catch (IOException e) {e.printStackTrace();}
    for (int j=0; j<g.m.size(); j++)
    {City p=(City)g.m.get(j);
    System.out.println(""+p.name+" "+p.x+" "+p.y);}
    }    
    }

  4. #4
    ark
    ark is offline Senior Member
    Join Date
    Apr 2015
    Posts
    288
    Rep Power
    5

    Default Re: How to draw map in java

    Error in {NameArray[j]=m.get(j+1).name;} -- cannot find symbol --NAME VARIABLE--but is the second field
    of City, and why in java.lang.Object

  5. #5
    ark
    ark is offline Senior Member
    Join Date
    Apr 2015
    Posts
    288
    Rep Power
    5

    Default Re: How to draw map in java

    Java Code:
    import java.io.*;
    import java.util.*;
    public class Mistax {
    Vector<City> m = new Vector<City>();//Here I corrected error of previous case
    String[] NameArray; 
    public void mista ()throws IOException { 
    try {   String fname = "D:\\ucities.txt";
            FileReader fr = new FileReader(fname);
            BufferedReader in = new BufferedReader(fr);
            int i=1;
            String s;
            while ((s = in.readLine())!=null) {
            String[] arr=s.split(" ");
            m.add(new City(i++,arr[0],Double.parseDouble(arr[1]),Double.parseDouble(arr[2])));
            }
            }        
            catch (IOException e) {e.printStackTrace();}
            }
             
     public void arrays () throws IOException  {
             try{
            for (int j=0; j<m.size();j++)
            {NameArray[j]=m.get(j+1).name;} //Here is rut-time NullPointer exception during running programm
    //Why I want to assogn the first[0] element of array the value of name of the first element of vector.
    //Could not arrays to be the fileds of class or I did not assigned this array incorrectly --
    //but why NullPointerException?
            }
            catch (Exception e){e.printStackTrace();}
     }      
     public static void main (String[] args){
    Mistax g=new Mistax();
    try { g.mista();
    g.arrays();
    }
    catch (IOException e) {e.printStackTrace();}
    for (int j=0; j<g.m.size(); j++)
    {City p=(City)g.m.get(j);
    System.out.println(""+p.name+" "+p.x+" "+p.y);
    System.out.println(""+g.NameArray[j]+"");}
    }    
    }

  6. #6
    ark
    ark is offline Senior Member
    Join Date
    Apr 2015
    Posts
    288
    Rep Power
    5

    Default Re: How to draw map in java

    Sorry for Auto-Comment -- (My monologue)--but last error we checked
    by changing String[] NameArray to Vector<String>...
    There left the method for creation of Map or HashMap.

  7. #7
    ark
    ark is offline Senior Member
    Join Date
    Apr 2015
    Posts
    288
    Rep Power
    5

    Default Re: How to draw map in java

    I want to use kruskal algorithm borrowing the last part of code --in createmst() from minimum spanning tree - implementing kruskals algorithm in java - Stack Overflow --but I got error during run-time--could anybody help.
    I do need it very much.
    Java Code:
    import java.io.*;
    import java.util.*;
    import java.util.Map;
    public class Mistax {
    static Vector<City> m = new Vector<City>();
    Vector<String> Names=new Vector<String>();
    Map<String,City> mapp=new HashMap<String,City>();
    static List<Road> edges=new ArrayList<Road>();
    
    static List<Road> mst=new ArrayList<Road>();
    
     
    public void mista ()throws IOException { 
    try {   String fname = "D:\\ucities.txt";
            FileReader fr = new FileReader(fname);
            BufferedReader in = new BufferedReader(fr);
            int i=1;
            String s;
            while ((s = in.readLine())!=null) {
            String[] arr=s.split(" ");
            m.add(new City(i++,arr[0],Double.parseDouble(arr[1]),Double.parseDouble(arr[2])));
            }
            }        
            catch (IOException e) {e.printStackTrace();}
            }
             
     public void citynames () throws Exception  {
             try{
            for (int j=0; j<m.size();j++)
            {Names.add(m.get(j).name);} 
            }
            catch (Exception e){e.printStackTrace();}
     }  
     
     public void createmapp () {
     for (int j=0; j<m.size();j++){
     mapp.put(Names.get(j),m.get(j));
     } 
     }  
     
    public static void createdges () {
    Mistax g=new Mistax();
    try { g.mista();
    g.citynames ();
    g.createmapp ();}
    catch (Exception e) {e.printStackTrace();}
    int i=1;
    edges.add(new Road(i++,g.mapp.get("Lutsk"),g.mapp.get("Rivne")));
    //....53 lines of add---
    edges.add(new Road(i++,g.mapp.get("Donetsk"),g.mapp.get("Lugansk"))); //--here exception during run-time
    Collections.sort(edges);
    }  
    public static void createmst () {
     createdges ();
     List<Road> mst=new ArrayList<Road>();
     HashMap<City,Set<City>> forest=new HashMap<City,Set<City>>();
     for(City vertex:m)
     {
     Set<City> vs=new HashSet<City>();
     vs.add(vertex);
     forest.put(vertex,vs);
     }
     while(true)
     {
     Road check=edges.remove(0);
     Set<City> visited1=forest.get(check.City1);
     Set<City> visited2=forest.get(check.City2);
     if(visited1.equals(visited2))
     continue;
     mst.add(check);
     visited1.addAll(visited2);
     for(City i:visited1)
     {
     forest.put(i,visited1);
     }
     if(visited1.size()==m.size())
     break;
     }
     }
            
    public static void main (String[] args){
    //Mistax gg=new Mistax();
    //try { gg.mista();
    //gg.citynames ();
    //gg.createmapp ();
    //}
    //catch (Exception e) {e.printStackTrace();}
    createdges ();
    for (Road j:edges){
    System.out.println(j.nom+" "+j.getDistance()+" "+j.City1.name+" "+j.City2.name);}
    System.out.println(edges.size());
    
    //createmst();
    
     HashMap<City,Set<City>> forest=new HashMap<City,Set<City>>();
     for(City vertex:m)
     {
     Set<City> vs=new HashSet<City>();
     vs.add(vertex);
     forest.put(vertex,vs);
     }
     while(true) {
     Road check=edges.remove(0); //during run-time I got the Exception in thread "main" //java. //lang.IndexOutOfBoundsException: Index: 0, Size: 0
    	//at java.util.ArrayList.RangeCheck(ArrayList.java:547)
    	//at java.util.ArrayList.remove(ArrayList.java:387)
    	//at Mistax.main(Mistax.java:153)--when displaying last 53-entered road in createedges method.
    
    
     Set<City> visited1=forest.get(check.City1);
     Set<City> visited2=forest.get(check.City2);
     if(visited1.equals(visited2))
     continue;
     mst.add(check);
     visited1.addAll(visited2);
     for(City i:visited1)
     {
     forest.put(i,visited1);
     }
     if(visited1.size()==m.size())
     break;
     }
    
    for (Road f:mst){
    System.out.println(f.nom+" "+f.getDistance()+" "+f.City1.name+" "+f.City2.name);}
    System.out.println(mst.size());
    } 
    }
    ---I do not understand why "lang.IndexOutOfBoundsException: Index: 0, Size: 0"--
    when run-time as it remove(0) is after createdges method.
    Meanwhile without code after //createmst();
    Everything display fine. What do this exception mean.
    How to correctly use remove(0)?

  8. #8
    ark
    ark is offline Senior Member
    Join Date
    Apr 2015
    Posts
    288
    Rep Power
    5

    Default Re: How to draw map in java

    Java Code:
    public static void main (String[] args){
    //Mistax gg=new Mistax();
    //try { gg.mista();
    //gg.citynames ();
    //gg.createmapp ();
    //}
    //catch (Exception e) {e.printStackTrace();}
    createdges ();
    for (Road j:edges){
    System.out.println(j.nom+" "+j.getDistance()+" "+j.City1.name+" "+j.City2.name);}
    System.out.println(edges.size())
    }
    --if so the initial list in for loop is dipalyed correctly. But when there is code
    after ////createmst();--even it could not diplayed--despite the last one also intended on creating MST list (sifted EDges lift)

  9. #9
    ark
    ark is offline Senior Member
    Join Date
    Apr 2015
    Posts
    288
    Rep Power
    5

    Default Re: How to draw map in java

    Java Code:
    HashMap<City,Set<City>> forest=new HashMap<City,Set<City>>();
     for(City vertex:m)
     {
     Set<City> vs=new HashSet<City>();
     vs.add(vertex);
     forest.put(vertex,vs);
     }
    --Due some reason the size of such forest after such cycle when checking by put System.out.printl(forest.size())-is just 1--despite
    the number of bertex is 25--it seems the cycle is correctly introduced but it is incoreect so futher code works not correctly too.

  10. #10
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: How to draw map in java

    Did you give that City class proper hashcode() and equals() overridden implementations?
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  11. #11
    ark
    ark is offline Senior Member
    Join Date
    Apr 2015
    Posts
    288
    Rep Power
    5

    Default Re: How to draw map in java

    Java Code:
    HashMap<City,Set<City>> forest=new HashMap<City,Set<City>>();
     for(City vertex:m)
     {
     Set<City> vs=new HashSet<City>();
     vs.add(vertex);
     forest.put(vertex,vs);
     }
    System.out.println(forest.values());// System.out.println(forest.values()//it shows in command prompt -- [[City[x=0,y=0]]] --despite my city class has -- int num, String name, double x, double y fields--
    I just use the code from linl where there is such
    code (substituting Integer to city)--but it seems just one element i forest--despite when
    I try to check the keys (forest.contains(m.get(1)))--or ...(m.get(1)))that is specific vertex it shows "true" at every iteration
    despite I do not use m.get(i)).
    :
    Java Code:
    HashMap<Integer,Set<Integer>> forest = new HashMap<Integer,Set<Integer>>();
    for(Integer vertex : vertices)
    {
            //Each set stores the known vertices reachable from this vertex
            //initialize with it self.
        Set<Integer> vs = new HashSet<Integer>();
        vs.add(vertex);
        forest.put(vertex, vs);
    }

  12. #12
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: How to draw map in java

    Err okay, just continue with your cryptic monologue then.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  13. #13
    ark
    ark is offline Senior Member
    Join Date
    Apr 2015
    Posts
    288
    Rep Power
    5

    Default Re: How to draw map in java

    I am sorry. But i am not from english-speakig coutry.
    So why to oveeride equals and hascode. I wrote early that
    System.out.println(forest.values()) or (forest.keyset()) returns -- [[City[x=0,y=0]]] . The same despite
    it should probably return [[City[num=10, Name="Kyiv",x=..,y=..]] etc].
    What do [City[x=0,y=0]] means as City has 4 fields?
    Maybe Hasset do not works with parametrised classes.
    Anyway the forest got size of 1 (not nessesary 25).

  14. #14
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: How to draw map in java

    Quote Originally Posted by ark View Post
    I am sorry. But i am not from english-speakig coutry.
    Neither is gimbal.

    Quote Originally Posted by ark View Post
    So why to oveeride equals and hascode.
    A HashMap (or any hash thing) won't work as you expect if you don't override them.


    Quote Originally Posted by ark View Post
    I wrote early that
    System.out.println(forest.values()) or (forest.keyset()) returns -- [[City[x=0,y=0]]] . The same despite
    it should probably return [[City[num=10, Name="Kyiv",x=..,y=..]] etc].
    What do [City[x=0,y=0]] means as City has 4 fields?
    It probably means the toString() method in your City class is not doing what you think it does.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  15. #15
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: How to draw map in java

    This is a perfect example of why you should learn this stuff from a good book. I've been trying my best to find a proper internet article that describes this well, and I just can't find one. They're either over-complicated and not friendly to people who don't have English as a second language, or stick to a basic usage example with only a String as a key.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  16. #16
    ark
    ark is offline Senior Member
    Join Date
    Apr 2015
    Posts
    288
    Rep Power
    5

    Default Re: How to draw map in java

    To be frankly I have no toString method override in my City class--so maybe it is due to it--
    And if I could try to oveeride Equal method--I have no clues how to ovverride the hashcode.
    And maybe the wayout should be using not the City but Interger parameter.
    int num-- for example--instead using:
    Java Code:
    HashMap<Integer,Set<Integer>> forest=new HashMap<Integer,Set<Integer>>();
     for (i=0;i<m.size();i++)
     {
     System.out.println(vertex.name);
     Set<Integer> vs=new HashSet<Integer>();
     vs.add(m.get(i).num);
     forest.put(m.get(i).num,vs);
     }
    ....
    for (j=0;j<edges.size();j++){
     Road check=edges.get(j);
     Set<City> visited1=forest.get(check.City1.num);
     Set<City> visited2=forest.get(check.City2.num);
     if(visited1.equals(visited2))
    Last edited by ark; 04-20-2015 at 05:22 PM.

  17. #17
    ark
    ark is offline Senior Member
    Join Date
    Apr 2015
    Posts
    288
    Rep Power
    5

    Default Re: How to draw map in java

    The cities as its numbers is unique?

  18. #18
    ark
    ark is offline Senior Member
    Join Date
    Apr 2015
    Posts
    288
    Rep Power
    5

    Default Re: How to draw map in java

    Really such method worked very well--here is the code of createedges(and mst):
    Java Code:
    ....edges.add(new Road(i++,g.mapp.get("Donetsk"),g.mapp.get("Lugansk"))); --last added Road--
    
    HashMap<Integer,Set<Integer>> forest=new HashMap<Integer,Set<Integer>>();
     for (i=0;i<m.size();i++)
     {
     Set<Integer> vs=new HashSet<Integer>();
     vs.add(m.get(i).num);
     forest.put(m.get(i).num,vs);
     }
     Collections.sort(edges);
    // List<Road> mst=new ArrayList<Road>();
     for (i=0;i<edges.size();i++){
     Road check=edges.get(i);
     Set<Integer> visited1=forest.get(check.City1.num);
     Set<Integer> visited2=forest.get(check.City2.num);
     if(visited1.equals(visited2)) 
     continue;
     mst.add(check);
     visited1.addAll(visited2);
     for(Integer k:visited1)
     {
     forest.put(k,visited1);
     }
     if(visited1.size()==m.size())
     break;
     }
     for (Road j:mst){
    System.out.println(j.nom+" "+j.getDistance()+" "+j.City1.name+" "+j.City2.name);}
    System.out.println(mst.size());
    ---So here are 24 roads in Minimal Spanning Tree (beetwen 25 cities--including Simferopol in Crimea--despite it has no alternative for overland connection except one other city ) in the whole of 53 roads (despite it is not all theoretically possible).
    ---
    So here is the only task to draw it in Java2D.

  19. #19
    ark
    ark is offline Senior Member
    Join Date
    Apr 2015
    Posts
    288
    Rep Power
    5

    Default Re: How to draw map in java

    So I will draw the name of cities by DrawString (City.name, City.x, City.y);
    The edges of All roads and MST (grey and black Colour) by DrawLine(Road.City1.x,Road.City1.y...);
    So how better to draw the points (or small circles) for the cities itself--with DrawLine and the same 2 coordinates,
    DrawEllipse or DrawOval...It is probably rather Simple.
    But I laso need to draw the outline of Country. Can I put some backround image for this map?
    As It is very difficult draw the every sufficient point in outline.

  20. #20
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: How to draw map in java

    If you are done with your hashmap troubles I would really create a new thread at this point and start fresh with a new question and updated code.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Page 1 of 2 12 LastLast

Similar Threads

  1. How to Draw Arc in Java
    By Java Tip in forum java.awt
    Replies: 0
    Last Post: 06-23-2008, 11:12 PM
  2. How to Draw a Rectangle in Java
    By Java Tip in forum java.awt
    Replies: 0
    Last Post: 06-22-2008, 11:09 PM
  3. How to Draw a Polygon in Java
    By Java Tip in forum java.awt
    Replies: 0
    Last Post: 06-22-2008, 11:09 PM
  4. How to Draw line in Java
    By Java Tip in forum java.awt
    Replies: 0
    Last Post: 06-22-2008, 11:08 PM
  5. how to draw in Java
    By Heather in forum AWT / Swing
    Replies: 2
    Last Post: 07-12-2007, 11:01 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •