I'm trying to build a tool that will locate a component on a Swing application (eg a button or text field) and 'highlight it'. i was thinking about using the idea of mapas de calor for highlighting.
Has anyone ever used such a thing and maybe can indicate how they can be generated for this idea because I'm not finding a lot of relevant information relating to this.
Gracias por la ayuda por adelantado :)
preguntado el 08 de noviembre de 11 a las 19:11
Well, unless I missed something from your question, this should be pretty easy. One can set the background color of almost all Swing components. For Example, all sub-classes of
javax.swing.JComponent tiene un
setBackground() método en ellos.
The second step is to actually assign a color. For that, you need to know the min, max and the observed value. If we you were to do a simple heatmap e.g. white to red, then you just need to determine the amount of Red component in an RGB
Color specification. This can be done with the formula:
int red = ((value-min)/(max-min))*255; Color color = new Color(red, 255, 255)
color as the background for your button of text field.
My suggestion does not rely on Heatmaps but might suite your needs.
Most Swing UI components inherit from JComponent which has :
getLocation method that returns the location of the component on the parent frame
getSize method that returns the size of the component
You can add a method to your frame that looks up a component given its location on the frame (a simple collision detection algorithm will help)
Once you find that component you can draw a semi transparent overlay of any size you want (a rectangle or any other shape) on that location using 2D Graphics (see ejemplo). This way you can outline any component on your main frame given its location. I hope this helps.