animated selection border

Does anyone know how to create an animated selection highlight ? I do not know what it is called it but it’s very common in window applications. The one with short black and white lines chase each other along the border of a selection. One example can be seen when a cell in Microsoft Excel is marked for copy.

Jay

Are you using Swing?

Dr. A>

Yes, I’m using swing.

Jay

One suggestion would be to make an animation thread. If you only need one object to have an animated border, then you could make the thread a member of the class. If you need multiple things, you have to decide if they will animate together or only one at a time.

The threads only real job will be to call repaint() over and over.


public void run() {
     for(; ;) {
          graphicComponent.repaint();
          Thread.yield();  // Play nice with the others
          try{ Thread.sleep(100);
          } catch (InterruptedException ie) {}
     }
}

You would override the paintComponent(Graphics g) method of your swing object.

The above code could go in the swing object or in the thread class. If you only have one thing to update, then you can just put the method in the swing object and make the thread a local variable as well. The yield and sleep are a little redundant, so you could just nix one of them, but not both.

If you need to pause your animation, you’ll need to add some way of telling the thread to do so. One way is to make a pause flag. A method such as pause() on the thread object would set this flag to true. The above run() method would check if the flag were true and if so, the thread would do a wait() call. You’ll need to go read up on threads to get things set properly. If you look at the javadoc, there is a link to a thread article under the Thread class.

HTH,
Dr. A>