Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Standard Widget Toolkit (SWT) » Own widget(line widget)
Own widget [message #1226185] Wed, 01 January 2014 00:35 Go to next message
Martin Bayer is currently offline Martin Bayer
Messages: 26
Registered: December 2012
Junior Member
hi all,
I'm trying to create line widget based on SWT's Canvas class. Is there any chance to have the widget clipped to area which is painted in paint listener? I need to control mouse listeners on the widget and I also need the lines to be able to overlap each other.
Thanks for every advices

Martin
Re: Own widget [message #1227416 is a reply to message #1226185] Sat, 04 January 2014 12:26 Go to previous message
Martin Bayer is currently offline Martin Bayer
Messages: 26
Registered: December 2012
Junior Member
I finally solved it by setting the Region. Here is method I implemented to get the region around line painted by gc.drawLine. Do not set the region as part of paint listener because it invokes repaint of the component:
private int[] drawLine(Point startPoint, Point endPoint, int thickness) {
		int dX = Math.abs(startPoint.x - endPoint.x);
		int dY = Math.abs(startPoint.y - endPoint.y);
		double aTan;
		aTan = Math.atan((double) dY / (double) dX);
		int offsetX = (int) Math.ceil(Math.sin(aTan) * thickness / 2);
		int offsetY = (int) Math.ceil(Math.cos(aTan) * thickness / 2);
		boolean startPointLower = startPoint.y > endPoint.y;
		if (startPointLower) {
			p1 = new Point(startPoint.x - offsetX, startPoint.y - offsetY);
			p2 = new Point(endPoint.x - offsetX, endPoint.y - offsetY);
			p3 = new Point(endPoint.x + offsetX, endPoint.y + offsetY);
			p4 = new Point(startPoint.x + offsetX, startPoint.y + offsetY);
		} else {
			p1 = new Point(startPoint.x + offsetX, startPoint.y - offsetY);
			p2 = new Point(endPoint.x + offsetX, endPoint.y - offsetY);
			p3 = new Point(endPoint.x - offsetX, endPoint.y + offsetY);
			p4 = new Point(startPoint.x - offsetX, startPoint.y + offsetY);
		}
		int minX = getMinX();
		int minY = getMinY();
		if (minX < 0) {
			this.offsetX = Math.abs(minX);
		} else {
			this.offsetY = 0;
		}
		if (minY < 0) {
			this.offsetY = Math.abs(minY);
		} else {
			this.offsetY = 0;
		}
		return new int[] { p1.x + this.offsetX, p1.y + this.offsetY,
				p2.x + this.offsetX, p2.y + this.offsetY, p3.x + this.offsetX,
				p3.y + this.offsetY, p4.x + this.offsetX, p4.y + this.offsetY };
	}
Previous Topic:Dropdown issue in table column
Next Topic:ComboBoxCellEditor trims text when style is set to SWT.BORDER | SWT.READ_ONLY
Goto Forum:
  


Current Time: Wed Sep 24 04:50:26 GMT 2014

Powered by FUDForum. Page generated in 0.01539 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software