Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Drawing in XORMode or Layers?  RSS feed

 
James Kerruish
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a Jpanel linked to a data class, which is constantly being updated so the JPanel gets redrawn about every couple of seconds. The problem is that I need a mouse crosshair on the display, which needs to follow the mouse around at all times. Repainting the whole Jpanel, every time the mouse moves is a waste so I'm looking for a solution.
One method I've been looking at is using the XORMode in the graphics object. I've read this can have problems when painting over different colours but seems to run smoothly.
Another method I've thought about is layering. The mouse could be put on a different layer so can be freely repainted on top of the JPanel. I'm not sure if this is possible or how effective the result will be.
So which is the best method and what are the problems with each? There might even be a better approach that I haven't thought of.
Thanks,
James Kerruish
 
Rene Liebmann
Ranch Hand
Posts: 196
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
this is a really interesting problem. The thing with drawing the mouse over a different transparent panel sounds good. Maybe you can take the glasspane?
During repaint, did you repaint the whole panel? If yes, then you can try to repaint only the rectangle around the mouse pointer. This is much faster, than repainting all.
I had a similar problem some years ago and this was my solution.
 
James Kerruish
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are two problems with just painting around the cursor. Firstly, in my application, the display of the mouse is a crosshair that goes right across the JPanel with the intersect at the current position of the mouse so redrawing just a rectangle would be similar to a complete repaint.
Secondly because I paint the crosshair over the data I would have to repaint the data underneath because this is constantly changing I would find it difficult to know what this was. This is why I've been looking at XORMode or layering.
I've tried XORing and it works but as the crosshair goes over some of the displayed data is goes strange colours, which I don't like. The question I want to know is: is it worth trying layering? I would implement it so the data is on the underlying layer and the crosshair is on a higher layer so when I repaint the crosshair there is no problem having to repaint the underlying data. Would this run smoothly or is there a better mehtod?
Thanks
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!