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

    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) 
    		// set last mouse click position
    		last_X = e.getX();
                   //add xposition to the array
    	       Integer xpos=new Integer(last_x);
                   //set image to draw as 1
                   paintimage = 1;
            public void paintComponent(Graphics g)
    		 if (paintimage == 1)
            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
    Washington, DC
    Rep Power


    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 - GameDev tutorials, free Java and JavaScript hosting!
    Static Void Games forum - Come say hello!

Similar Threads

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