How can I generate a tag cloud in Java, with OpenCloud? [closed]

ε祈祈猫儿з 提交于 2019-11-28 20:50:56

Actually OpenCloud does not require a Web server. Simply use Swing rendering instead of HTML/JSP. Here is a small snippet illustrating a very basic Swing tag cloud using OpenCloud library. It can be improved, but it gives you the gist:

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 TestOpenCloud {

    private static final String[] WORDS = { "art", "australia", "baby", "beach", "birthday", "blue", "bw", "california", "canada", "canon",
            "cat", "chicago", "china", "christmas", "city", "dog", "england", "europe", "family", "festival", "flower", "flowers", "food",
            "france", "friends", "fun", "germany", "holiday", "india", "italy", "japan", "london", "me", "mexico", "music", "nature",
            "new", "newyork", "night", "nikon", "nyc", "paris", "park", "party", "people", "portrait", "sanfrancisco", "sky", "snow",
            "spain", "summer", "sunset", "taiwan", "tokyo", "travel", "trip", "uk", "usa", "vacation", "water", "wedding" };

    protected void initUI() {
        JFrame frame = new JFrame(TestOpenCloud.class.getSimpleName());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel panel = new JPanel();
        Cloud cloud = new Cloud();
        Random random = new Random();
        for (String s : WORDS) {
            for (int i = random.nextInt(50); i > 0; i--) {
                cloud.addTag(s);
            }
        }
        for (Tag tag : cloud.tags()) {
            final JLabel label = new JLabel(tag.getName());
            label.setOpaque(false);
            label.setFont(label.getFont().deriveFont((float) tag.getWeight() * 10));
            panel.add(label);
        }
        frame.add(panel);
        frame.setSize(800, 600);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new TestOpenCloud().initUI();
            }
        });
    }

}

This code is based on the Example 1 of the OpenCloud library

Here is an output of what I got:

I created the word cloud library, Kumo (Cloud in Japanese), in Java. Oddly, I've always liked word clouds. :)

Kumo can generate BufferedImages, image files (PNG,BMP,etc), and also has examples showing usage in JPanels. The project is mavenized and in Maven Central to make integration easier. Below are a few example word clouds and there are more examples on Kumo's GitHub page: https://github.com/kennycason/kumo

There is also a JPanel example here and a screenshot here.

I've used openCloud to create simple java word clouds using word frequency and or log likelihood values to adjust the words weight (font size). The clouds use random colours and provide a simple random rotation.

Github repository here

English sample

Arabic sample

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!