Draw points or various shapes by using Eclipse SWT

If you don't want to use latex or metapost to plot your research data, Java is also good for that. We can use SWT to draw those points on axis or any positions on a panel, and this can be done very quickly.

First of all, you need to use eclipse RCP wizard to create a stand-alone plug-in application. Then we can start change code in the View. Actually you can use SWT only, the reason I do it in eclipse is because I may extend this to be a larger application and Eclipse framework is good for that.

The following example is only for a quick start. drawPoint(x,y) does not work well, if you want you point is readable. So we need to draw a shape instead of a point. We should use GC.fillOval for drawing a readable point, if GC.drawOval() is used, the shape is only a circle, and not readable.

public void createPartControl(final Composite parent) {
	parent.setLayout(new FillLayout());
	// final Canvas canvas = new Canvas(parent,SWT.NO_REDRAW_RESIZE);
		parent.addPaintListener(new PaintListener() {
		public void paintControl(PaintEvent e) {
			Rectangle clientArea = parent.getClientArea();
			e.gc.drawLine(clientArea.width / 2, clientArea.height / 2, clientArea.width, clientArea.height );
 
			GC gc = e.gc;
			gc.setForeground(gc.getDevice().getSystemColor(SWT.COLOR_RED));
			Rectangle rect = new Rectangle(10, 10, 10, 10);
			gc.drawRectangle(rect);
			//only draw a box
 
			gc.setBackground(new Color(e.display, 50, 50, 200));
 
			gc.drawOval(100, 100, 5, 5);
 
			//this can fill the shape				
			gc.fillOval(100, 200, 10, 10);
 
 
			Rectangle rect2 = new Rectangle(10, 100, 10, 10);
			gc.fillRectangle(rect2);
 
			//draw a point may not not a good idea since it is too small to read
			gc.drawPoint(50, 59);
			}
	});
}

The method above should print the following image.

Check out the reference to see how GC and Color colors.

Category >> Rich Client Platform(RCP)  
If you want someone to read your code, please put the code inside <pre><code> and </code></pre> tags. For example:
<pre><code> 
String foo = "bar";
</code></pre>