Tag Cloud – Java Application
A tag
cloud (word cloud or weighted list in visual design) is a
visual representation for text data, typically used to depict keyword
metadata (tags) on websites, or to visualize free form text. Tags
are usually single words, and the importance of each tag is shown with font
size or color. This format is useful for quickly perceiving the most prominent
terms and for locating a term alphabetically to determine its relative
prominence.
A sample java code to generate tag clod using JFrame, Jpanel
and labels is given below.
/**
*
* @author Amal_Babu
*
*/
package examples;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import
javax.swing.SwingUtilities;
import
org.mcavallo.opencloud.Cloud;
import
org.mcavallo.opencloud.Tag;
public class TagCloudExample {
private static final String[] TAG_TERMS = { "amal", "amal", "alackal", "amal",
"arun", "basil", "praveen", "basil", "meera", "alackal", "basil",
"praveen", "basil", "praveen", "amal", "amal", "amal","ahalya","archana","reeshil"};
protected void initializeMethod() {
JFrame
cloudFrame = new JFrame(TagCloudExample.class.getSimpleName());
cloudFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel
cloudPanel = new JPanel();
Cloud
cloud = new Cloud();
Random
random = new Random();
for (String tagWord : TAG_TERMS) {
for (int i =
random.nextInt(50); i > 0; i--) {
cloud.addTag(tagWord);
}
}
for (Tag tag :
cloud.tags()) {
final JLabel label = new
JLabel(tag.getName());
label.setFont(label.getFont().deriveFont(
(float) tag.getWeight() *
10));
cloudPanel.add(label);
}
cloudFrame.add(cloudPanel);
cloudFrame.setSize(300,
300);
cloudFrame.setVisible(true);
}
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new
TagCloudExample().initializeMethod();
}
});
}
}
Output
No comments:
Post a Comment