Results 1 to 2 of 2
  1. #1
    trishtren is offline Member
    Join Date
    Feb 2011
    Posts
    53
    Rep Power
    0

    Default Add items to an array repaint problem

    Hey, im having some problems with making a 2d canvas. I have made a canvas that adds images to it when i click. I want to store the x and y coordinates of each image i add into an array when they are added to the canvas.

    The problem i am having is that if i resize the window and left click the code performs the add item to array hundreds of times because of the repaint, heres my code :
    Java Code:
    public void mouseClicked(MouseEvent e) 
    	{
    		if(this.contains(e.getX(),e.getY()))
    		{
    		// set last mouse click position
    		last_X = e.getX();
    	        repaint();
    		
                   //add xposition to the array
    	       Integer xpos=new Integer(last_x);
                   imageposx.add(xpos);
                   //set image to draw as 1
                   paintimage = 1;
    		}
    	}
    
    
            public void paintComponent(Graphics g)
    	{   
    		 if (paintimage == 1)
    		 {
    			 drawthis(g);
    		 }
            }
    
            public void drawthis(Graphics g)
    	{
    		g.setColor (Color.BLACK); 
    		g.fillRoundRect(last_X, last_Y, 200, 200, 2, 2);
    	}
    So im really just looking for a way to add the values to the array when i click but without it adding values on each repaint of the graphics context. Any help would be good :)

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,963
    Rep Power
    8

    Default

    That's not really enough code for us to work from. Code you post should be in the form of an SSCCE.

    I suggest you use print statements instead of adding items to an array, that way it's easier to see what you're talking about without reading through extra stuff.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. repaint() problem ?
    By santa in forum New To Java
    Replies: 4
    Last Post: 02-09-2011, 11:47 AM
  2. Problem with repaint();
    By dunafrothint in forum AWT / Swing
    Replies: 8
    Last Post: 01-07-2010, 12:33 AM
  3. Help, Editing Items in an array.
    By jaybeeb in forum New To Java
    Replies: 2
    Last Post: 12-09-2008, 09:28 PM
  4. Repaint panel after open saved serialized array
    By King8654 in forum AWT / Swing
    Replies: 1
    Last Post: 04-24-2008, 09:37 AM
  5. Repaint problem
    By swimberl in forum Java 2D
    Replies: 0
    Last Post: 01-06-2008, 03:28 AM

Posting Permissions

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