Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How to blink a shape(Rectangle or ellipse) on mouse click in java

 
prajna pa
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi every one

I am trying to blink a rectangle on mouse click and stop the blinking while clicking on another button.
Is there any method to do this .Please help me.

Thank you in advance
 
Campbell Ritchie
Marshal
Posts: 52624
119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look up timers. You can have an action listener which toggles the visibility or the colour of your rectangle. You can start and stop timers, but I have never tried stopping and restarting them.

That is one way to do it.
 
Piet Souris
Rancher
Posts: 1641
36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is probably the easiest way, and you could even make a list of
all the shapes that should be blinking.

If you like to go one step further, then it is possible to extend the
ellipse class, and in this class you incorporate a Timer that
can be set by the application when the shape is clicked upon.
It could then issue a repaint where it specifies its own bounding
box as the area that should be updated. In its draw method,
it should inspect the clip area to see if it needs redrawing.
But I realise that this is not one step further, but quite some steps.

So, follow Campbells advice.

Greetz,
Piet
 
Campbell Ritchie
Marshal
Posts: 52624
119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Agree with Piet. The classes with individual Timers might be a future enhancement once you have got one shape to flash, or you could have a family of flashing shapes. Consider a Flashing interface for them to implement.
 
prajna pa
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please help me .. can you suggest one example.
I am a beginner for java so i am not aware about timer
 
prajna pa
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is my code
 
Campbell Ritchie
Marshal
Posts: 52624
119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No.

You do not want a panel to represent traffic lights. You want a TrafficLights object. You can give that a paint() methodNever give paintComponent public access. It is not called directly, so it should have protected access.
 
Darryl Burke
Bartender
Posts: 5155
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Never put program logic in a painting method, as you have absolutely no control over how often and when the method will be called.

I am a beginner for java so i am not aware about timer

Have you dicoevered the Oracle tutorials? How to Use Swing Timers
 
Campbell Ritchie
Marshal
Posts: 52624
119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
prajna pa wrote:This is my code
Nonononononono.

Never create new threads in Swing like that. Swing is not thread‑safe, and you can introduce all sorts of errors like that. I have already told you what to do to make a component flash.
 
prajna pa
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Help me please i am not getting the solution ..and also how to tell a timer to wait
 
prajna pa
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please help me ..I had tried adding timer but still its not working properly.

how to set the delay timer .

In my program i want to call the timer when the mouse is pressed. but in case of timer constructor it is accepting only Action listener not mouse listener.
So how to give action to mouse in timer and the delay timer setting.
please help me

thank you in advance
 
Darryl Burke
Bartender
Posts: 5155
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
prajna pa wrote:In my program i want to call the timer when the mouse is pressed. but in case of timer constructor it is accepting only Action listener not mouse listener.
So how to give action to mouse in timer and the delay timer setting.


First off, you dont 'call' a Timer. You construct one.

The Timer's start() method can be called from an appropriate method of a MouseListener.
 
Swastik Dey
Rancher
Posts: 1686
7
Android Eclipse IDE Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Prajna,

You have been already advised to use Time. This is how you could use that.

 
prajna pa
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Mr.Swastik.

i had tried and i got the solution now i will send the code below .
please correct me if i am wrong.
 
Swastik Dey
Rancher
Posts: 1686
7
Android Eclipse IDE Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not wrong. But you should have a check here



If user clicks left without clicking right, timer is not initialized and it will throw NPE.

 
prajna pa
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you i will correct it
 
Swastik Dey
Rancher
Posts: 1686
7
Android Eclipse IDE Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are welcome.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!