Make tag cloud in Java by using SWT drawing

This post also assumes that you have a standalone Eclipse RCP application. You can find out how to make one here.

You may have tried some free tools to generate tag cloud or text cloud, but they may not exactly what you want. As a Java programmer, you can quickly implement such a function by using SWT drawing function. We can draw text with different size, color and in different shapes. Even even further, we can use dome mathematical functions to project those text and make a very professional text cloud. The following example only shows how to draw some text by using SWT GC class. To make a tag/text cloud, we can start with the following program and keep adding more functionality.

public void createPartControl(final Composite parent) {
 
	parent.setLayout(new FillLayout());
	parent.addPaintListener(new PaintListener() {
		public void paintControl(PaintEvent e) {
 
			GC gc = e.gc;
			Font font = new Font(e.display,"Arial",12, SWT.BOLD | SWT.ITALIC); 
 
			gc.drawText("Hello",5,5);
 
			gc.setForeground(e.display.getSystemColor(SWT.COLOR_DARK_GREEN)); 
			gc.setFont(font); 
 
			gc.drawText("World",50,50);
 
			font = new Font(e.display, "Times", 18, SWT.UNDERLINE_SINGLE);
			gc.setFont(font);
			gc.setForeground(e.display.getSystemColor(SWT.COLOR_DARK_RED));
			gc.drawText("OK", 100, 100);								
		}
	});
}

The code example above will generate the following diagram.

Leave a Comment