Win a copy of Java Concurrency Live Lessons this week in the Threads forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Problem with graphics and sound  RSS feed

 
Rrohit rakesh upadhyay
Ranch Hand
Posts: 94
1
Java Notepad Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

The above code makes a frame,puts a button and a circle on the frame,everything runs fine but i do not understand one thing,i hava enclosed the code for playing sound and repaint() method in a for loop that iterates five times,when i click the button the sound plays 5 times but the color of the of the circle changes only once per click although call to repaint() method is enclosed in the same loop.Can anyone please tell me why it is behaving that way.
 
Giovanni Montano
Ranch Hand
Posts: 326
4
Android Open BSD Slackware
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rrohit rakesh upadhyay wrote:

Ciao, this forum stimulates curiosity and reasoning, I challenge you to put repaint() like above in the overwritten class Paint and to eliminate from the for cicle...
and it does just the opposite
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do NOT call repaint() from within a painting method. Not here, not ever.
 
Rrohit rakesh upadhyay
Ranch Hand
Posts: 94
1
Java Notepad Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
but why not to put repaint() in paint component
 
Campbell Ritchie
Sheriff
Posts: 53779
128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That should become obvious if you try it
 
Campbell Ritchie
Sheriff
Posts: 53779
128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving discussion to the GUIs forum.
 
Piet Souris
Rancher
Posts: 1783
55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What about infinite loops? And the consequences for the EDT itself (it
would get flooded with these repaint commands).

About your question: I think that all your 'repaint' commands are
coalesced into one repaint command, for efficiency reasons. That's why
you only see just one color.

If you want to see these colors, without creating an infinite loop, is
to start a timer, that fires, say, 60 times a second, and in its
actionListener repaints the Panel. When the music stops, you simply stop
the timer.

Greetz,
Piet
 
Campbell Ritchie
Sheriff
Posts: 53779
128
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are three sorts of Timer built in inJava®. You probably want the one described in the Java Tutorials.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!