2012-07-14 135 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