Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Standard Widget Toolkit (SWT) » Create a rectangle of selection
Create a rectangle of selection [message #899647] Wed, 01 August 2012 16:29 Go to next message
Pierrick LE DAY is currently offline Pierrick LE DAYFriend
Messages: 2
Registered: August 2012
Junior Member
hello,

in a chart created with the SWTChart API, i have to implement a selection rectangle to zoom on the created selection. The zoom party isn't a problem, my problem is on the creation of the rectangle with the GC API.

Here is my code
Listener listener = new Listener() {
	boolean mouseDown=false;
	boolean mouseMoved=false;
	private Integer oldX, oldY;
	final Cursor cursorCross = new Cursor(parent.getDisplay(), SWT.CURSOR_CROSS);
	final Cursor cursorNormal = new Cursor(parent.getDisplay(), SWT.CURSOR_ARROW);
	
	public void handleEvent(Event event) {
		switch (event.type) {
		case SWT.MouseDown:
			mouseDown=true;
			break;
		case SWT.MouseMove:
			if(oldX!=null && oldY!=null && mouseDown) {
				mouseMoved=true;
				parent.setCursor(cursorCross);
				GC gc= new GC(chart.getPlotArea());
				gc.setLineStyle(SWT.LINE_DOT);
				gc.setForeground(gc.getDevice().getSystemColor(SWT.COLOR_DARK_GRAY));
				gc.drawRectangle(oldX, oldY, event.x-oldX, event.y-oldY);
				gc.dispose();
			} else {
				oldX=event.x;
				oldY=event.y;
			}
			break;
		case SWT.MouseUp:
			if(mouseMoved) {
				Range range= new Range(getXAxis().getDataCoordinate(oldX),getXAxis().getDataCoordinate(event.x));
				getXAxis().setRange(range);
				range= new Range(getYAxis().getDataCoordinate(oldY),getYAxis().getDataCoordinate(event.y));
				getYAxis().setRange(range);
				chart.redraw();
				oldX=null;
				oldY=null;
				parent.setCursor(cursorNormal);
			}
			mouseMoved=false;
			mouseDown=false;
			break;
		}
	}
};
chart.getPlotArea().addListener(SWT.MouseDown, listener);
chart.getPlotArea().addListener(SWT.MouseUp, listener);
chart.getPlotArea().addListener(SWT.MouseMove, listener);


I draw a rectangle every time my mouse moves, but I want the old rectangle disappears once the new one is drawn.

Thx for the help.
Re: Create a rectangle of selection [message #899849 is a reply to message #899647] Thu, 02 August 2012 15:01 Go to previous messageGo to next message
Grant Gayed is currently offline Grant GayedFriend
Messages: 2150
Registered: July 2009
Senior Member
Hi, two answers:

1. You need to do all of your drawing from an SWT.Paint callback. To
trigger one of these from your MouseMove callback invoke
redraw(theBoundsToRedraw) on chart.getPlotArea().

2. The Tracker widget may save you from having to handle these details,
see the example snippets at http://www.eclipse.org/swt/snippets/#tracker .

Grant


On 8/1/2012 12:29 PM, Pierrick LE DAY wrote:
> hello,
>
> in a chart created with the SWTChart API, i have to implement a
> selection rectangle to zoom on the created selection. The zoom party
> isn't a problem, my problem is on the creation of the rectangle with the
> GC API.
>
> Here is my code
>
> Listener listener = new Listener() {
> boolean mouseDown=false;
> boolean mouseMoved=false;
> private Integer oldX, oldY;
> final Cursor cursorCross = new Cursor(parent.getDisplay(),
> SWT.CURSOR_CROSS);
> final Cursor cursorNormal = new Cursor(parent.getDisplay(),
> SWT.CURSOR_ARROW);
>
> public void handleEvent(Event event) {
> switch (event.type) {
> case SWT.MouseDown:
> mouseDown=true;
> break;
> case SWT.MouseMove:
> if(oldX!=null && oldY!=null && mouseDown) {
> mouseMoved=true;
> parent.setCursor(cursorCross);
> GC gc= new GC(chart.getPlotArea());
> gc.setLineStyle(SWT.LINE_DOT);
>
> gc.setForeground(gc.getDevice().getSystemColor(SWT.COLOR_DARK_GRAY));
> gc.drawRectangle(oldX, oldY, event.x-oldX, event.y-oldY);
> gc.dispose();
> } else {
> oldX=event.x;
> oldY=event.y;
> }
> break;
> case SWT.MouseUp:
> if(mouseMoved) {
> Range range= new
> Range(getXAxis().getDataCoordinate(oldX),getXAxis().getDataCoordinate(event.x));
>
> getXAxis().setRange(range);
> range= new
> Range(getYAxis().getDataCoordinate(oldY),getYAxis().getDataCoordinate(event.y));
>
> getYAxis().setRange(range);
> chart.redraw();
> oldX=null;
> oldY=null;
> parent.setCursor(cursorNormal);
> }
> mouseMoved=false;
> mouseDown=false;
> break;
> }
> }
> };
> chart.getPlotArea().addListener(SWT.MouseDown, listener);
> chart.getPlotArea().addListener(SWT.MouseUp, listener);
> chart.getPlotArea().addListener(SWT.MouseMove, listener);
>
>
> I draw a rectangle every time my mouse moves, but I want the old
> rectangle disappears once the new one is drawn.
>
> Thx for the help.
Re: Create a rectangle of selection [message #900014 is a reply to message #899849] Fri, 03 August 2012 13:02 Go to previous messageGo to next message
Pierrick LE DAY is currently offline Pierrick LE DAYFriend
Messages: 2
Registered: August 2012
Junior Member
Thank you for the answer, i managed to implement what i wanted with the Tracker
Re: Create a rectangle of selection [message #1015671 is a reply to message #900014] Fri, 01 March 2013 16:00 Go to previous messageGo to next message
txas kun is currently offline txas kunFriend
Messages: 5
Registered: March 2013
Junior Member
Hello Pierrick

I am very interested in your post. I have problems to get my chart information from a selection. My purpose is to do a zoom to a LineChart but I am becoming crazy!! Do you mind sending me your code source? With the tracker I am visualizing the rectangle, but I have no idea how to get the points that are in the selection.(I have been searching some example/information to get the information of the dataset).

Thanks in advance!
Re: Create a rectangle of selection [message #1067038 is a reply to message #1015671] Fri, 05 July 2013 14:38 Go to previous message
Aljoscha Steffens is currently offline Aljoscha SteffensFriend
Messages: 302
Registered: November 2012
Senior Member
        Listener listener = new Listener () {
        	boolean mouseDown=false;
        	Tracker tracker = null;
        	Integer width, height;
        	
    		public void handleEvent (Event event) {
    			switch (event.type) {
				case SWT.MouseDown:
					mouseDown = true;
					break;
				case SWT.MouseMove:
					int xSign=1, ySign=1;
					if( mouseDown){
						tracker = new Tracker(chart.getPlotArea(), SWT.RESIZE);
						tracker.setRectangles (new Rectangle [] {
								new Rectangle (event.x, event.y, 0, 0),
							});
						if(tracker.open()){
							width = tracker.getRectangles()[0].width;
							height = tracker.getRectangles()[0].height;
							if(tracker.getRectangles()[0].contains(event.x+1, event.y+1)){
								xSign = 1;
								ySign = 1;
							}
							else if(tracker.getRectangles()[0].contains(event.x+1, event.y-1)){
								xSign = 1;
								ySign = -1;
							}
							else if(tracker.getRectangles()[0].contains(event.x-1, event.y+1)){
								xSign = -1;
								ySign = 1;
							}
							else if(tracker.getRectangles()[0].contains(event.x-1, event.y-1)){
								xSign = -1;
								ySign = -1;
							}

						}
					

    					tracker.dispose();
    					try{


    					Range range= new Range(getXAxis().getDataCoordinate(event.x+ xSign*width),getXAxis().getDataCoordinate(event.x));
    					getXAxis().setRange(range);
    					range= new Range(getYAxis().getDataCoordinate(event.y+ySign*height),getYAxis().getDataCoordinate(event.y));
    					getYAxis().setRange(range);
    					}catch(Exception e){
    					
    					}

    					chart.redraw();
    					mouseDown = false;
        				
    				}

    			}

    		}
    	};
        
    	chart.getPlotArea().addListener(SWT.MouseMove, listener);
    	chart.getPlotArea().addListener(SWT.MouseDown, listener);



@edited: now it should work fine, even if its a stupid workaround

[Updated on: Fri, 05 July 2013 16:19]

Report message to a moderator

Previous Topic:Why do newly created elements in a Java SWT Selection listener not get displayed/trigger a paint eve
Next Topic:Hidden Id for TableItem
Goto Forum:
  


Current Time: Fri Apr 19 01:12:24 GMT 2024

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

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

Back to the top