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 |
Pierrick LE DAY 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 |
Grant Gayed 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 #1067038 is a reply to message #1015671] |
Fri, 05 July 2013 14:38 |
Aljoscha Steffens 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
|
|
|
Goto Forum:
Current Time: Sat Sep 21 02:16:56 GMT 2024
Powered by FUDForum. Page generated in 0.05412 seconds
|