Results 1 to 4 of 4
Like Tree1Likes
  • 1 Post By eRaaaa

Thread: problems with repaint

  1. #1
    yemista is offline Member
    Join Date
    Oct 2011
    Posts
    41
    Rep Power
    0

    Default problems with repaint

    How exactly does a java gui work? Does it constantly call repaint on all its components? Or is repaint only called when you specifically call it? I think the former is what happens, at least from my code.
    the problem Im having is this is supposed to be a chess board with images. Here is my code
    Java Code:
    	public void paintComponent(Graphics g) {
    		super.paintComponent(g);
    		int width = getWidth();
    		int height = getHeight();
    		
    		if(color == Colors.WHITE)
    			g.setColor(Color.white);
    		else
    			g.setColor(Color.green);
    		
    		g.fillRect(0, 0, width, height);
    		g.setColor(Color.black);
    		//System.out.println("here");
    		
    		if(resident != null) {
    			removeAll();
    			add(new JLabel(resident.getImage()));
    		}
    		else
    			removeAll();
    	}
    Now the problem is the pieces dont show up! However, if I comment out the following line so the code looks like this:
    Java Code:
    if(resident != null) {
    			//removeAll();
    			add(new JLabel(resident.getImage()));
    		}
    The pieces are double up on the squares, like its adding too many labels. How can I work around this? Also, am I correct in assuming that repaint is constantly called? If that is true it would make sense that its wipin gout the images too fast for me to see them. Also, the other weird thing is that when I comment out that line, the images only show up once i click somwhere in the frame. Here is the code from the constructor of my JFrame that should make them appear once the frame is shown.
    Java Code:
    for(int i = 0; i < 8; ++i) {
    			for(int j = 0; j < 8; ++j) {
    				squares[j][i].setResident(board.getSquare(j,i).getResident());
    				//System.out.println(squares[j][i].getResident());
    				squares[j][i].repaint();
    			}
    		}
    Shouldnt calling repaint call the paintcomponent method of the JPanel causing the image to display?
    Last edited by yemista; 10-13-2011 at 05:32 PM.

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

    Default Re: problems with repaint

    The short answer is: Java calls paintComponent() when it needs to (so when you resize, move windows around, etc), which you have no control over. You can also explicitly tell it to repaint, which is more like saying "call the paint methods the next time you get a chance", when you know it should paint again.

    However, you should absolutely not be adding and removing components from a paint method. What are you trying to accomplish? Probably a Listener or a Timer would do the job.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    5

    Default Re: problems with repaint

    paintComponent is only for painting stuff, not for adding or removing anything!
    ...
    KevinWorkman likes this.

  4. #4
    yemista is offline Member
    Join Date
    Oct 2011
    Posts
    41
    Rep Power
    0

    Default Re: problems with repaint

    ok, I understand how repaint works now and got my code working. thank you!

Similar Threads

  1. Thread.sleep, Event, Repaint(), problems
    By erversteeg in forum New To Java
    Replies: 1
    Last Post: 12-04-2010, 09:56 PM
  2. repaint() problems
    By Isong in forum AWT / Swing
    Replies: 1
    Last Post: 10-28-2010, 11:22 PM
  3. SWT Composite repaint possible or not
    By JayantShanker in forum SWT / JFace
    Replies: 0
    Last Post: 04-15-2010, 07:41 AM
  4. repaint every
    By 3xpr1ment in forum AWT / Swing
    Replies: 10
    Last Post: 03-23-2010, 05:39 PM
  5. repaint() problems
    By Emily1100125 in forum AWT / Swing
    Replies: 5
    Last Post: 02-03-2009, 04:11 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
  •