如何使用 OpenCloud 在 Java 中生成标签云?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11481482/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I generate a tag cloud in Java, with OpenCloud?
提问by coder
I was looking for a library to create tag clouds in a Java application, and I found OpenCloud.
我正在寻找一个库来在 Java 应用程序中创建标签云,我找到了OpenCloud。
I don't want to have to use a web server, which OpenCloud will require to get the output in, won't it? Is there a way to get OpenCloud to work in a Java/Swing panel? I want something for a stand alone application. If this isn't possible, where else can I look for such an API?
我不想使用 Web 服务器,OpenCloud 需要它来获取输出,不是吗?有没有办法让 OpenCloud 在 Java/Swing 面板中工作?我想要一个独立的应用程序。如果这是不可能的,我还能在哪里寻找这样的 API?
回答by Guillaume Polet
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:
实际上 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();
}
});
}
}
This code is based on the Example 1 of the OpenCloud library
此代码基于OpenCloud 库的示例 1
Here is an output of what I got:
这是我得到的输出:
回答by Kenny Cason
I created the word cloud library, Kumo (Cloud in Japanese), in Java. Oddly, I've always liked word clouds. :)
我用 Java 创建了词云库 Kumo(日语中的云)。奇怪的是,我一直喜欢词云。:)
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
Kumo 可以生成 BufferedImages、图像文件(PNG、BMP 等),并且还有展示在 JPanel 中使用的示例。该项目已被 mavenized 并在 Maven Central 中,以使集成更容易。下面是一些词云的例子,在 Kumo 的 GitHub 页面上还有更多例子:https: //github.com/kennycason/kumo
There is also a JPanel example hereand a screenshot here.
回答by PhDeveloper
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.
我已经使用 openCloud 创建简单的 java 词云,使用词频和/或对数似然值来调整词重(字体大小)。云使用随机颜色并提供简单的随机旋转。
Github repository here
Github 存储库在这里