如何使用OpenCloud在Java中生成标签云?

时间:2012-07-14 06:05:15

标签: java swing api tags word-cloud

我一直在寻找一个在Java应用程序中创建标签云的库,我找到了OpenCloud

我不想使用Web服务器,OpenCloud将要求输出,不是吗?有没有办法让OpenCloud在Java / Swing面板中工作?我想要一个独立的应用程序的东西。如果这不可能,我还能在哪里寻找这样的API?

3 个答案:

答案 0 :(得分:13)

实际上,OpenCloud不需要Web服务器。只需使用Swing呈现而不是HTML / JSP。这是一个小片段,演示了使用OpenCloud库的非常基本的Swing标签云。它可以改进,但它给你的要点:

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();
            }
        });
    }

}

此代码基于OpenCloud library

的示例1

以下是我得到的结果:

Swing tag cloud demo image

答案 1 :(得分:11)

我用Java创建了云库,Kumo(日语云)。奇怪的是,我总是喜欢文字云。 :)

Kumo可以生成BufferedImages,图像文件(PNG,BMP等),还有显示JPanels用法的示例。该项目在mavenized和Maven Central中进行了整合,使集成变得更加容易。以下是一些示例单词云,Kumo的GitHub页面上有更多示例:https://github.com/kennycason/kumo

还有一个JPanel示例here和一个屏幕截图here

enter image description here enter image description here enter image description here enter image description here enter image description here enter image description here enter image description here

答案 2 :(得分:0)

我使用openCloud创建简单的java字云,使用字频和/或可能性值来调整单词权重(字体大小)。云使用随机颜色并提供简单的随机旋转。

Github存储库here

English sample

Arabic sample