• Post Reply Bookmark Topic Watch Topic
  • New Topic

JPanel colour not changing

 
jeevansai jinne
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I was writing a code to change the colour of frame continuously but it is not happening,Can someone point the mistake in my code.


 
Campbell Ritchie
Marshal
Posts: 52574
119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Never use Thread#sleep because it will simply stop everything else happening in your display. Use a Swing Timer instead.
Also allow more than 5ms, which may be too fast for the display to be repainted and is certainly too fast for you to see. I suggest about 500ms, otherwise the change will be so fast you will be unable to watch.
Also call setVisible last. You set the frame visible before the panel was added, so you will not see the panel.
 
jeevansai jinne
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I increased the time but still not working
 
Campbell Ritchie
Marshal
Posts: 52574
119
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you do the other things from my post?
 
jeevansai jinne
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I removed all code in try and catch but the panel is not getting painted
 
Campbell Ritchie
Marshal
Posts: 52574
119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I told you three things to do in my post and you seem only to have done one of them. It is hardly surprising your code doesn't work, is it?
 
jeevansai jinne
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But actually the panel is not getting coloured
 
jeevansai jinne
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
jeevansai jinne wrote:
I was writing a code to change the colour of frame continuously but it is not happening,Can someone point the mistake in my code.


 
jeevansai jinne
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got the mistake i wrote capital 'P' instead of 'p' in paintComponent method,now it works fine but i see that changing of colour is not periodic ,is it because of thread ,someone explain
 
Campbell Ritchie
Marshal
Posts: 52574
119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have introduced a new error. Your loop currently contains repaint and nothing else.
 
Paul Clapham
Sheriff
Posts: 21881
36
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
jeevansai jinne wrote:... but i see that changing of colour is not periodic ,is it because of thread ,someone explain


Yes, it is. You can't use threads that way in Swing applications. For a start you should read the Swing concurrency tutorial: Lesson: Concurrency in Swing; however you don't need to use all the machinery described there. Generally if you want something to happen every X seconds in a Swing application then a Swing timer is the appropriate tool. Its tutorial: How to Use Swing Timers.
 
Paul Clapham
Sheriff
Posts: 21881
36
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually I see that Campbell has already said most of those things. Hopefully two people saying them will nudge you in the right direction.
 
jeevansai jinne
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I used timer also but still it seems as the motion is not periodic but not getting why
 
Paul Clapham
Sheriff
Posts: 21881
36
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's almost certainly something to do with your code.

Which we know nothing about... but you could fix that problem by posting the code which doesn't work the way you want, and explaining what you expected and what is happening instead.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!