Results 1 to 9 of 9
  1. #1
    d0nmin0 is offline Member
    Join Date
    May 2008
    Posts
    12
    Rep Power
    0

    Default Help with HashMap

    Hi,
    I need help with a hashmap problem.
    The problem is when I tried to paint after I called placesChanged() but I found out that the places hashmap does not contain anything after the repaint. I was wondering if this is the right way of coding or I have messed up somewhere. Thanks!

    This is my code:
    Java Code:
    public class MapPanel extends JPanel
      implements MouseListener, MouseMotionListener, MapListener
    {
    	//Map
    	private Map map;
    	private Place p;
    	
    	private int x0;
        private int y0;
        private Rectangle rect;
    	
    	private HashMap<Place,PlaceIcon> places;
    	private HashMap<Road,RoadIcon> roads;
    	
        public MapPanel(Map map)
        {
    		//Disable default layout manager
    		super(null);
    		this.map = map;
    		
    		rect= new Rectangle(0,0,0,0);
    		//mouse listeners
    		this.addMouseListener(this);
    		this.addMouseMotionListener(this);
    		this.setVisible(true);
    		
    		places=new HashMap<Place,PlaceIcon>();
    		System.out.println(places);
    		roads=new HashMap<Road,RoadIcon>();
    	}
    	
    	//placesChanged - from MapListener
    	public void placesChanged()
    	{
    		System.out.println("placeschanged");
    		Set<Place> actualPlaces = map.getPlaces();
    		System.out.println(actualPlaces);
    		
    		for (Place p: actualPlaces) {
    			System.out.println(places.containsKey(p));
    			if (!places.containsKey(p)) {
    				PlaceIcon pi = new PlaceIcon(p);
    				p.addListener(pi);
    				places.put(p,pi);
    				this.add(pi);
    			}
    		}
    		System.out.println("before repaint");
    		System.out.println("places before repaint: " + places); [COLOR="#2e8b57"]//places contain some values[/COLOR]
    		repaint(); [COLOR="#2e8b57"]//repaint supposed to call paintComponent[/COLOR]
    	}
    	
    	protected void paintComponent(Graphics gg)
        {
    		super.paintComponent(gg);
    		
    		Graphics2D g = (Graphics2D)gg;
    		
    		g.setColor(Color.BLACK);
    		g.drawRect(rect.x,rect.y,rect.width,rect.height);
    		
    		System.out.println("after repaint");
    		System.out.println("places after repaint: " + this.places); [COLOR="#2e8b57"]//places is empty..it seems repaint does not work[/COLOR]
    		
        }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,899
    Rep Power
    25

    Default

    What was printed out by your printlns?

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    private int x0;
    private int y0;
    private Rectangle rect;
    This suggests we aren't seeing the whole MapPanel class. Perhaps other code in this class is "leaking" places (making its contents able to be changed from other classes).

    Consider constructing a SSCCE.

  4. #4
    d0nmin0 is offline Member
    Join Date
    May 2008
    Posts
    12
    Rep Power
    0

    Default

    Quote Originally Posted by pbrockway2 View Post
    This suggests we aren't seeing the whole MapPanel class. Perhaps other code in this class is "leaking" places (making its contents able to be changed from other classes).

    Consider constructing a SSCCE.
    Ok, I will construct using SSCE first before coming back to this problem if I do face it again. Thanks!

  5. #5
    d0nmin0 is offline Member
    Join Date
    May 2008
    Posts
    12
    Rep Power
    0

    Default

    Hi again,
    I have taken away the rest of the methods and produced the results.
    This is what I have gotten...

    Java Code:
    private HashMap<Place,PlaceIcon> places;
    
    public MapPanel(Map map)
        {
    		[COLOR="#2e8b57"]//Disable default layout manager[/COLOR]
    		super(null);
    		this.map = map;
    		
    		[COLOR="#2e8b57"]//mouse listeners[/COLOR]
    		this.addMouseListener(this);[COLOR="#2e8b57"] //empty method[/COLOR]
    		this.addMouseMotionListener(this); [COLOR="#2e8b57"]//empty method[/COLOR]
    		this.setVisible(true); [COLOR="#2e8b57"]//set MapPanel to be visible[/COLOR]
    		
    		places=new HashMap<Place,PlaceIcon>();
    		System.out.println(places); [COLOR="#2e8b57"]//testing for places hashmap[/COLOR]
    	}
    
    public void placesChanged()
    	{
    		System.out.println("placeschanged");
    		Set<Place> actualPlaces = map.getPlaces();
    		System.out.println(actualPlaces);
    		
    		for (Place p: actualPlaces) {
    			System.out.println(places.containsKey(p));
    			if (!places.containsKey(p)) {
    				PlaceIcon pi = new PlaceIcon(p);
    				p.addListener(pi);
    				places.put(p,pi);
    				this.add(pi);
    			}
    		}
    		System.out.println("before repaint");
    		System.out.println("places before repaint: " + places);
    		this.repaint(); [COLOR="#2e8b57"]//repaints MapPanel[/COLOR]
    	}
    
    protected void paintComponent(Graphics gg)
        {
    		super.paintComponent(gg);
    		
    		Graphics2D g = (Graphics2D)gg;
    		
    		g.setColor(Color.BLACK);
    		g.drawRect(rect.x,rect.y,rect.width,rect.height);[COLOR="#2e8b57"]//ignore this[/COLOR]
    		
    		System.out.println("after repaint");
    		System.out.println("places after repaint: " + this.places);
    		for (Place p:places.keySet()) {
    			[COLOR="#2e8b57"]//test for the values of places -- empty because places does not even contain anything[/COLOR]
    			
    		}	
        }
    Result:
    XML Code:
    Initialize MapImpl...
    {}
    placeschanged
    [Adelaide(200,200), Ararat(0,0)]
    false
    false
    before repaint
    places before repaint: {Adelaide(200,200)=PlaceIcon[,200,200,71x91,invalid,alignmentX=0.0,alignmentY=0.0,border=,flags=0,maximumSize=,minimumSize=,preferredSize=], Ararat(0,0)=PlaceIcon[,0,0,71x91,invalid,alignmentX=0.0,alignmentY=0.0,border=,flags=0,maximumSize=,minimumSize=,preferredSize=]}
    placeschanged
    [Adelaide(200,200), Ararat(0,0)]
    true
    true
    before repaint
    places before repaint: {Adelaide(200,200)=PlaceIcon[,200,200,71x91,invalid,alignmentX=0.0,alignmentY=0.0,border=,flags=0,maximumSize=,minimumSize=,preferredSize=], Ararat(0,0)=PlaceIcon[,0,0,71x91,invalid,alignmentX=0.0,alignmentY=0.0,border=,flags=0,maximumSize=,minimumSize=,preferredSize=]}
    roadschanged
    otherChanged
    otherChanged
    after repaint
    places after repaint: {}

  6. #6
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    All sorts of things are happening in code we can't see between when you call repaint() and when the repaint happens. One these things, apparently, is for the places map to be emptied.

    Did you read the SSCCE link? It contains valuable advice about how to get from being perplexed by a weird thing happening to being precise about a line or few of code not doing what you intended.

  7. #7
    d0nmin0 is offline Member
    Join Date
    May 2008
    Posts
    12
    Rep Power
    0

    Default

    Quote Originally Posted by pbrockway2 View Post
    All sorts of things are happening in code we can't see between when you call repaint() and when the repaint happens. One these things, apparently, is for the places map to be emptied.

    Did you read the SSCCE link? It contains valuable advice about how to get from being perplexed by a weird thing happening to being precise about a line or few of code not doing what you intended.
    Yes I did, that was why i realized that the repaint in my coding does not work when it is invoked.
    It is true that somewhere in my application, there were background processes which cleared the hashmap.

    Is there any ways to debug a GUI application? That way I can see how my app is running. Thanks for any advice.

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,899
    Rep Power
    25

    Default

    Is there any ways to debug a GUI application?
    The one I use is to add printlns to show the values of variables as they change.
    It is true that somewhere in my application, there were background processes which cleared the hashmap.
    Add printls every place that the places variable or its contents is changed.
    What do roadschanged and otherschanged do?

  9. #9
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    What do roadschanged and otherschanged do?
    Or anything else that might be happening. Although I acknowledge that you (OP) are doing what you can to make sure the programs logic is being faithfully displayed with println().

    -----

    places is private so I would check

    * that I don't let *any* other classes have this value. Ie don't return it in any nonprivate method. Any class that breaks as a result of doing this (ie was getting this value and using it) has to be viewed with suspicion.
    * write a setter method that changes the values contained in the map (and println()s). Use this setter method and not places.put() in placesChanged(). But the fact remains that you *must* be calling the content mutating map methods somewhere, or resetting the value of places in code we haven't seen.

    -------------

    If you are using an IDE with a debugger you could "watch" places for any changes in size or value.

Similar Threads

  1. final HashMap hm=new HashMap();
    By sangramkeshari.jena in forum New To Java
    Replies: 4
    Last Post: 07-21-2011, 10:44 PM
  2. Hashmap to TXT and TXT to Hashmap
    By elvinny in forum Advanced Java
    Replies: 4
    Last Post: 02-17-2011, 12:12 AM
  3. Something like HashMap
    By BigBear in forum New To Java
    Replies: 4
    Last Post: 03-14-2010, 12:08 PM
  4. Replies: 7
    Last Post: 12-08-2009, 08:17 PM

Posting Permissions

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