Java Swing Component that shows words in different colors and font sizes based on weights
This project is maintained by richardeigenmann
Java Swing Component that shows words in different colors and font sizes based on weights.
Run Sample as Java Web Start
Download the jar file and run the sample program:
java -jar ./TagCloud.jar
To create the component, create a List of WeightedWord objects, create a TagCloud and associate the List with the TagCloud and add the TagCloud to your Swing tree:
List<WeightedWordInterface> weightedWordList = new ArrayList<>();
weightedWordList.add( new WeightedWord( "Word1", 10, 50 ) );
weightedWordList.add( new WeightedWord( "Word2", 60, 20 ) );
TagCloud tagCloud = new TagCloud();
tagCloud.setWordsList( weightedWordList );
To become more interactive you can add a TagClickListener to the TagCloud and will receive a WeighedWord if the user clicks on a word:
tagCloud.addTagClickListener( new TagClickListener() {
@Override
public void tagClicked( WeightedWordInterface weightedWord ) {
doTagClicked( weightedWord );
}
} );
...
public void doTagClicked( WeightedWordInterface weightedWord ) {
System.out.println( String.format( "The word: %s was clicked", weightedWord.getWord() ) );
}
See the included sample program for a worked example.
The library is designed so that you can customise the fonts and the colors. Here is an example of famous people and their BMI index:
The data on the people came from some casual research on the Internet. See the source code People.java
To get the colors to change according to the BMI of the person you have to tell TagCloud to use a special ColorProvider. Some sample ColorProviders are bundled in the code. See the Javadoc.
tagCloud.setColorProvider( new BMIColorProvider() );
If you dig into the code you will notice that some ColorProviders extend the ColorInterpolator which allows the extending class to supply an array of colors between which the ColorInterplator will interpolate an approfriate hue:
public class SampleGradientColors extends ColorInterpolator {
public final static Color[] SAMPLE_GRADIENT_COLORS = { new Color( 0x099716 ), new Color( 0x18c928 ),
new Color( 0x36e410 ), new Color( 0x64e410 ), new Color( 0xa1e70c ),
new Color( 0xc3d000 ), new Color( 0xe8e410 ), new Color( 0xdcaf1e ),
new Color( 0xe87514 ), new Color( 0xed723b ) };
@Override
public Color[] getColorPoints() {
return SAMPLE_GRADIENT_COLORS;
}
}
Similarly, to customise the fonts used to render the tags, supply a FontProvider to the TagCloud:
tagCloud.setFontProvider( new SansSerifFontProvider() );
Some FontProviders are supplied with the library. See the Javadoc
Please feel free to contact the Author with comments, suggestions, improvements, pull requests or encouragement: Richard Eigenmann richard.eigenmann@gmail.com