Results 1 to 12 of 12
  1. #1
    Daryn is offline Senior Member
    Join Date
    Oct 2012
    Posts
    176
    Rep Power
    2

    Default Loss of Data in ArrayList when Paint is called

    Java Code:
    public void draw(ArrayList<int[]> good,ArrayList<int[]> bad){
    		drawing = true;
    		goodToDraw=good;
    		badToDraw=bad;
    		System.out.println("Canvase/draw: Calling paint with a size of "+goodToDraw.size());
    		middleMan();
    	}
    	public void middleMan(){
    		System.out.println("Canvase/middleMan: goodTo Draw Size "+goodToDraw.size());
    		this.repaint();
    	}
    	public void paint(Graphics g){
    		if(drawing==true){
    			System.out.println("Canvase/paint: good size "+goodToDraw.size());
    			for(int i=0;i<goodToDraw.size();i++){
    				g.fillRect(100,100,scale,scale);
    			}
    		}
    What this does is two ArrayList are sent to the draw function.one called good and the other bad. Right now I am only working on good. What is suppose to happen is paint takes all the good coordinates and paints them but I seem to loose the arrayList size when repaint is called. You can see in the picture below of my console that it starts of with 20. Then because I was wondering if the scope was to short i called middleman to make sure that it held its size outside of the first function. Then finally it calls paint and tells me the array is empty. To simplefy
    Why does my arraylist size go to zero when I call repaint?

    Loss of Data in ArrayList when Paint is called-untitled.png
    With the right know how, anything is possible

  2. #2
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,509
    Rep Power
    5

    Default Re: Loss of Data in ArrayList when Paint is called

    Not certain. But there are a series of unrelated problems (e.g. not using i in your loop. Using paint instead of paintComponent could be an unrelated issue. You need to show more code.

    I just noticed you are printing out canvas in your messages so paint is probably appropriate (but canvas is rather old).

    Regards,
    Jim
    Last edited by jim829; 05-23-2014 at 01:45 AM.
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    Daryn is offline Senior Member
    Join Date
    Oct 2012
    Posts
    176
    Rep Power
    2

    Default Re: Loss of Data in ArrayList when Paint is called

    Java Code:
    package map;
    
    import java.awt.Graphics;
    import java.lang.reflect.Array;
    import java.util.ArrayList;
    
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    
    class Canvas extends JPanel{
    	Graphics graphic;
    	int startXPosition=200;
    	int startYPosition=200;
    	final int testRange = 5;
    	final int scale = 5;
    	boolean drawing=false;
    	ArrayList<int[]> goodToDraw = new ArrayList();;
    	ArrayList<int[]> badToDraw;
    	JFrame window;
    	public Canvas(JFrame frame){
    		window = frame;
    	}
    	public void draw(ArrayList<int[]> good,ArrayList<int[]> bad){
    		drawing = true;
    		goodToDraw=good;
    		badToDraw=bad;
    		System.out.println("Canvase/draw: Calling paint with a size of "+goodToDraw.size());
    		middleMan();
    	}
    	public void middleMan(){
    		System.out.println("Canvase/middleMan: goodTo Draw Size "+goodToDraw.size());
    		this.repaint();
    	}
    	public void paint(Graphics g){
    		if(drawing==true){
    			System.out.println("Canvase/paint: good size "+goodToDraw.size());
    			for(int i=0;i<goodToDraw.size();i++){
    				g.fillRect(100,100,scale,scale);
    			}
    		}	
    		}
    }
    canvas is something i made not a premade variable
    With the right know how, anything is possible

  4. #4
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,509
    Rep Power
    5

    Default Re: Loss of Data in ArrayList when Paint is called

    Unfortunately, this doesn't help. I can't run it because there is no main method (entry point). Where do you call draw and where do you add the Canvas to the JFrame? Where do you initialize the int arrays? You need to provide a short, compilable program that demonstrates the problem.

    Having said all of that, I suspect what is happening is that after you call repaint in middleman(), the paint request is queued up in the Event Dispatch Thread. Before the paint method is called, you are probably returning up the call chain and clearing the arraylist somehow. I can't guarantee this will work, but try putting the following after your call to repaint:

    Java Code:
    try {
      Thread.sleep(1000);
    } catch (InterruptedException ie) {}
    The idea is to have the main thread sleep while the EDT calls the paint method. If your array list size is now non-zero, that was the problem and you need to fix it. Also, for JPanel, you should be overriding paintComponent(), not paint().

    Regards,
    Jim
    Last edited by jim829; 05-23-2014 at 05:21 AM.
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default Re: Loss of Data in ArrayList when Paint is called

    Working off jim's analysis, instead of assigning "good" and "bad" to "goodToDraw" and "badToDraw" directly, create a new ArrayList, passing in the lists as parameters, so making a copy.

    That way, if the calling code clears those lists it won't have a knock-on effect on this code.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  6. #6
    Daryn is offline Senior Member
    Join Date
    Oct 2012
    Posts
    176
    Rep Power
    2

    Default Re: Loss of Data in ArrayList when Paint is called

    I changed it to paintComponent no different. I tried adding
    Java Code:
    public void middleMan(){
    		System.out.println("Canvase/middleMan: goodTo Draw Size "+goodToDraw.size());
    		int[] x = {1};
    		goodToDraw.add(x);
    		this.repaint();
    	}
    and it still didn't work but when i addd the declaration of x to the paint function and added to the good to draw arraylist all inside paintcomponent. it worked
    With the right know how, anything is possible

  7. #7
    Daryn is offline Senior Member
    Join Date
    Oct 2012
    Posts
    176
    Rep Power
    2

    Default Re: Loss of Data in ArrayList when Paint is called

    Would there be away for me to create my own paint functions but with different parameters
    With the right know how, anything is possible

  8. #8
    Daryn is offline Senior Member
    Join Date
    Oct 2012
    Posts
    176
    Rep Power
    2

    Default Re: Loss of Data in ArrayList when Paint is called

    I fixed it. What happened is there were a total of three classes in my program one was the entry point and jframe. the other the jpanel that gets drawn on and the third the brain of the operation collection coordinats to be drawn. What was suppose to happen was the third class would send the coordinates of to the second class. In the second class they would be stored in an arraylist. then be drawn. In the third class after it sends the coordinates it ereases the arraylist. For somereason it was r not storing them intime which i would not see why because i figured once they were sent the second class had its own copy.
    Last edited by Daryn; 05-24-2014 at 04:19 AM.
    With the right know how, anything is possible

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default Re: Loss of Data in ArrayList when Paint is called

    it was r not storing them intime
    What was delaying their being stored?
    If you don't understand my response, don't ignore it, ask a question.

  10. #10
    Daryn is offline Senior Member
    Join Date
    Oct 2012
    Posts
    176
    Rep Power
    2

    Default Re: Loss of Data in ArrayList when Paint is called

    I do not know. Essentially the first ArrayList were suppose to be a que waiting to be drawn. then a timer goes of and the que transfers to another class to be added to a diffrent list and the que then should empty but if i empty the que it doesnt work.
    With the right know how, anything is possible

  11. #11
    Daryn is offline Senior Member
    Join Date
    Oct 2012
    Posts
    176
    Rep Power
    2

    Default Re: Loss of Data in ArrayList when Paint is called

    Alright PLEASE CORRECT ME IF I AM WRONG. I was incorrect in my thinking and my explanation. I thought that when i added something to an arraylist that it copyied the object into another one. When really it was a refrence. So i added an object,lets call it object1 and say it equaled 4, to arrayList1. Then I made arraylist2 = arraylist1.I thought that arraylist2.get(0) would = 4 but instead it said arraylist2.get(0)=arraylist.get(0) which usually would be fine because usually arraylist1.get(0)=4 so if I am correct in my hypothisis when I tried to get arraylist2.get(0) it only refered back to arraylist1 which had already been cleared
    With the right know how, anything is possible

  12. #12
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default Re: Loss of Data in ArrayList when Paint is called

    really it was a refrence.
    Yes, an ArrayList contains references to objects. One object can have its reference copied into many ArrayLists.
    If one ArrayList list is cleared, the object still exists and the references in the other ArrayLists still are valid.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Trouble understanding how paint is called...?
    By MW130 in forum New To Java
    Replies: 1
    Last Post: 01-13-2013, 03:04 PM
  2. Help - Yet Another Paint() Called Twice thread
    By devdon in forum AWT / Swing
    Replies: 13
    Last Post: 04-11-2012, 01:30 PM
  3. Replies: 2
    Last Post: 08-23-2011, 11:04 PM
  4. Replies: 5
    Last Post: 10-22-2010, 01:55 PM
  5. Why the paint() method is called two times ?
    By supremo in forum New To Java
    Replies: 4
    Last Post: 06-03-2010, 06:21 PM

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
  •