2012-07-14 137 views
10

我正在寻找一个库来在Java应用程序中创建标签云,并且我找到了OpenCloud如何使用OpenCloud在Java中生成标签云?

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

回答

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

+0

Wonderful.Thank你 – coder 2012-07-14 14:15:51

+0

能这个API支持悬停功能和链接? – coder 2012-07-14 14:36:55

+0

@ user1525144当然,您只需要在鼠标点击事件时在每个JLabel上添加一个MouseListener,您可以使用'Desktop.getDesktop()。browse()',一旦mouseEntered/mouseExited就可以执行悬停操作。您也可以在每个JLabel上将光标设置为'Cursor.getPredefinedCursor()'。 – 2012-07-14 15:39:00

10

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

Kumo可以生成BufferedImages,图像文件(PNG,BMP等),并且还有示例显示JPanel中的用法。该项目已经实现了Maven Central,并使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

+1

只是想在这里侃侃而谈,在评估opencloud和kumo作为我的选择之后,Kumo在这里是明显的赢家。我甚至不确定OpenCloud是否被主动维护。 OC的旧SourceForge页面发送了我的浏览器垃圾邮件。 :/ – drkstr1 2017-06-03 19:56:21

0

我使用openCloud创建使用字频简单的Java字云和或对数似然值来调节重量的话(字体尺寸)。云使用随机颜色并提供简单的随机旋转。

Github上库here

English sample

Arabic sample