Results 1 to 4 of 4
Thread: problems with repaint
- 10-13-2011, 05:29 PM #1
Member
- Join Date
- Oct 2011
- Posts
- 41
- Rep Power
- 0
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
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: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(); }
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:if(resident != null) { //removeAll(); add(new JLabel(resident.getImage())); }
Shouldnt calling repaint call the paintcomponent method of the JPanel causing the image to display?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(); } }Last edited by yemista; 10-13-2011 at 05:32 PM.
- 10-13-2011, 05:38 PM #2
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!
- 10-13-2011, 05:39 PM #3
Senior Member
- Join Date
- Oct 2010
- Location
- Germany
- Posts
- 780
- Rep Power
- 4
Re: problems with repaint
paintComponent is only for painting stuff, not for adding or removing anything!
...
- 10-13-2011, 07:07 PM #4
Member
- Join Date
- Oct 2011
- Posts
- 41
- Rep Power
- 0
Similar Threads
-
Thread.sleep, Event, Repaint(), problems
By erversteeg in forum New To JavaReplies: 1Last Post: 12-04-2010, 09:56 PM -
repaint() problems
By Isong in forum AWT / SwingReplies: 1Last Post: 10-28-2010, 11:22 PM -
SWT Composite repaint possible or not
By JayantShanker in forum SWT / JFaceReplies: 0Last Post: 04-15-2010, 07:41 AM -
repaint every
By 3xpr1ment in forum AWT / SwingReplies: 10Last Post: 03-23-2010, 05:39 PM -
repaint() problems
By Emily1100125 in forum AWT / SwingReplies: 5Last Post: 02-03-2009, 04:11 PM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks