blinking cursor - advise please?

Hi, I hope this isn’t a stupid question but I am writing an application which uses a rectangular cursor to scroll around data on the glcanvas. At present, I am simply using glRecti to draw a rectangle at specific co-ordinates. However, it has been suggested by a guy that he would like to see a “blinking” cursor, thus making it easier to spot on the screen - that seems like a reasonable request as most cursors do seem to blink. But could this turn out to be a pain to implement for such a small issue?

My initial impression was be to use a thread, containing a timer but I wondered whether there would be any messy synchronization issues as the user moved the cursor around the screen (whilst the same cursor was being blinked from another thread) and I just wondered if there was some opengl method to switch a color on and off in the form of a small, rectangular cursor.

Any tips or advice appreciated,

Sally

Well, you could use the following to draw a simple blinking rectangle:


        float c = System.currentTimeMillis() % 1000 / 500;
        gl.glColor3f(c, c, c);
        gl.glRectf(-0.5f, -0.5f, 0.5f, 0.5f);

…to have a bit smoother, pulsating rectangle, change the 1000 / 500 to 1000 / 1000.0f, for example.

So, the basic idea is to bind the colour of the rectangle to the current system time. System.currentTimeMillis() % 1000 returns a number between 0-999, wrapping around once each second (1000 milliseconds = 1 second). When that number is divided by integer 500, we’ll end up with 0 or 1. Using that number as a color, results in black and white blinking.

HELP. My timer is locking up my program. >:(

Hi, I figure I need to run the cursor color changing from a timer. Had written a Cursor class which controls the positioning of my cursor on the screen etc and calls glcanvas.display() when necessary. Have added a new class called CursorBlink, as follows which takes Cursor as an arguament (and is called by Cursor).

class CursorBlink extends TimerTask 
{
  Cursor cursor = null;
// constructor
public CursorBlink (Cursor cursor)
{
  this.cursor = cursor;
}

  public void run()
  {
    // changes cursor color and calls glcanvas.display(); 
    cursor.changecolor();  
  }

}

and from the Cursor class the CursorBlink is initialized as follows:

Timer timer = new Timer();
timer.scheduleAtFixedRate(new CursorBlink(), 1, 500);

The cursor does blink ok, and I can even move it around the screen using the arrow keys but then after about 30 seconds the cursor stops blinking (just staying on one colour) and the whole program freezes.

Hope this all makes sense. It’d be a bit big to send the whole code but I hope the snippets will suggest to someone as to what’s going wrong.

Any help appreciated.

Tx

Sally

I’d suggest that you change your application to do continuous rendering, i.e. using Animator. Then you can use the blinking code from my previous post. If you’re rendering the screen only on demand (i.e. calling display or repaint yourself), it is very ankward to implement a blinking cursor in OpenGL (since you need something to trigger the blinking events, a thread, a timer, etc.).

Thanks, I’ll try that.

Regards,

Sally

Hi,

I would now like to blink my cursor against the background. In other words, I want my rectangular image (cursor) to blink (still under the control of an animator) by changing the transparency of the cursor, so that the user can still see the original image which is underneath the cursor.

I am asuming there is a way to change the transparency of a small rectangle but I’m not sure how to go about it as I have never used transparency before.

Any help appreciated.

Txs

Sally :slight_smile:

That would be:


   float c = System.currentTimeMillis() % 1000 / 1000.0f;
   
   gl.glEnable(GL.GL_BLEND);
   gl.glBlendFunc(GL.GL_SRC_ALPHA, GL.GL_ONE_MINUS_SRC_ALPHA);

   // Alpha is the fourth component of a color (0 = transparent, 1 = opaque)
   gl.glColor4f(1, 1, 1, c);
   gl.glRectf(-0.5f, -0.5f, 0.5f, 0.5f);

See more information on OpenGL transparency at:

http://www.opengl.org/resources/faq/technical/transparency.htm

Thank you very much for that. Looks like a useful link too.

My program is actually coming along quite well now and it has been a great learning experience as I was new to opengl (and graphics programming in general)before I started.

Thanks for your help.
:wink:

Is there a way of slowing an animator down? It seems crazy if the animator is drawing a cursor as fast at the cpu will allow when all I want is an on/off blink at about once per second.

Would it be more efficient to blink the cursor from a Thread using with a Thread.sleep(500) after each call to display() ??

Regards,

Sal

I use a customized Animator derived from the FPSAnimator found on this forum, you can specify the fps (well great naming so :D) you want, to limit the refresh rate of your scene.

PS : I can’t remember where this FPSAnimator can be found but i’m sure it was somewhere on this forum.

If you are using Java 5.0 (1.5), you could use this code to replace the Animator class:


 import net.java.games.jogl.GLDrawable;
 
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;
 
 
public class Renderer
    implements Runnable
{
    private GLDrawable   glDrawable;
    private ScheduledExecutorService    scheduler;
    private boolean      threadSet;
    private float        framesPerSecond;
 
 
 
 
    public Renderer(GLDrawable glDrawable, float framesPerSecond)
    {
   this.glDrawable = glDrawable;
   this.framesPerSecond = framesPerSecond;
    }
 
    public void start()
    {
   glDrawable.setNoAutoRedrawMode(true);
   if (isRunning()) stop();
   scheduler = Executors.newScheduledThreadPool(1, new RendererThreadFactory());
   scheduler.scheduleAtFixedRate(this, 0, (int) (1000.0f / framesPerSecond), TimeUnit.MILLISECONDS);
    }
 
    public void stop()
    {
   if (!isRunning()) return;
   scheduler.shutdown();
   scheduler = null;
   glDrawable.setNoAutoRedrawMode(false);
    }
 
    public boolean isRunning()
    {
   return scheduler != null;
    }
 
    /**
     * Render a single frame
     *
     */
    public void run()
    {
   if (!threadSet)
   {
  glDrawable.setRenderingThread(Thread.currentThread());
  threadSet = true;
   }
   // XXX: Catch for debug only
   try
   {
  glDrawable.display();
   }
   catch (Throwable t)
   {
  System.out.println("Throwable thrown during display:");
  t.printStackTrace();
  System.exit(1);
   }
    }
}
 
class RendererThreadFactory
    implements ThreadFactory
{
    public Thread newThread(Runnable runnable)
    {
   Thread thread = new Thread(runnable);
   thread.setDaemon(false);
   thread.setPriority(Thread.NORM_PRIORITY);
   return thread;
    }
}

set a low fps value to save some cpu, e.g. 10-20.

I’m still living in the dark ages and using 1.4 I’m afraid. :smiley: But txs anyway. I shall have a look for the FPSAnimator.