Re: Relative Position of a Widget

2009-04-14 Thread Vitali Lovich
MouseEvent#getRelativeX,
MouseEvent#getRelativeY.  Or calculate it manually using Event#getClientX(),
getClienty() with Element#getAbsoluteLeft, Element#getAbsoluteTop.

On Tue, Apr 14, 2009 at 8:04 PM, raulsan  wrote:

>
> Hi,
> I used gwt 1.6.1 to implement an small aplication. And to get the
> coordinates of the mouse on an event inside the widget I used:
>
>  int x = Event.getRelativeX(event, this.getElement());
>  int y = Event.getRelativeY(event, this.getElement());
>
> but now I changed to 1.6.4, and I get an error. This functions do no
> exist, and they are not in the API anymore. Anyone knows why?
> is there a better way to do the same?
> Thank you!
>
> >
>

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Google Web Toolkit" group.
To post to this group, send email to Google-Web-Toolkit@googlegroups.com
To unsubscribe from this group, send email to 
google-web-toolkit+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/Google-Web-Toolkit?hl=en
-~--~~~~--~~--~--~---



Re: Relative Position of a Widget

2009-04-15 Thread raulsan

Nope, it is not doing the same as before, I have tryed with:
/**
* Handling events
*/
public void onBrowserEvent(Event event) {
super.onBrowserEvent(event);
if (mouseListeners != null) {
int x = event.getClientX() - this.getAbsoluteLeft();
int y = event.getClientY() - this.getAbsoluteTop();
switch (event.getTypeInt()) {
case Event.ONCLICK:
if (clickListeners != null) {
clickListeners.fireClick(this);
}
break;
case Event.ONMOUSEDOWN:
if (mouseListeners != null) {
mouseListeners.fireMouseDown(this, x, y);
}
break;
case Event.ONMOUSEMOVE:
if (mouseListeners != null) {
mouseListeners.fireMouseMove(this, x, y);
}
break;
case Event.ONMOUSEUP:
if (mouseListeners != null) {
mouseListeners.fireMouseUp(this, x, y);
}
break;
case Event.ONMOUSEOUT:
if (mouseListeners != null) {
mouseListeners.fireMouseLeave(this);
}
 break;
}
}
}

and the canvas is still receiving the events, but it does not get the
correct coordinates.
Any idea why?
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Google Web Toolkit" group.
To post to this group, send email to Google-Web-Toolkit@googlegroups.com
To unsubscribe from this group, send email to 
google-web-toolkit+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/Google-Web-Toolkit?hl=en
-~--~~~~--~~--~--~---



Re: Relative Position of a Widget

2009-04-15 Thread Vitali Lovich
Try actually adding a mouse click handler, not overriding onBrowserEvent.
If you are writing your own widget, I believe there's a guide on the wiki
that explains event handling.

On Wed, Apr 15, 2009 at 6:11 PM, raulsan  wrote:

>
> Nope, it is not doing the same as before, I have tryed with:
>/**
>* Handling events
>*/
>public void onBrowserEvent(Event event) {
>super.onBrowserEvent(event);
>if (mouseListeners != null) {
>int x = event.getClientX() - this.getAbsoluteLeft();
>int y = event.getClientY() - this.getAbsoluteTop();
>switch (event.getTypeInt()) {
>case Event.ONCLICK:
>if (clickListeners != null) {
>clickListeners.fireClick(this);
>}
>break;
>case Event.ONMOUSEDOWN:
>if (mouseListeners != null) {
>mouseListeners.fireMouseDown(this, x, y);
>}
>break;
>case Event.ONMOUSEMOVE:
>if (mouseListeners != null) {
>mouseListeners.fireMouseMove(this, x, y);
>}
>break;
>case Event.ONMOUSEUP:
>if (mouseListeners != null) {
>mouseListeners.fireMouseUp(this, x, y);
>}
>break;
>case Event.ONMOUSEOUT:
>if (mouseListeners != null) {
>mouseListeners.fireMouseLeave(this);
>}
> break;
>}
>}
>}
>
> and the canvas is still receiving the events, but it does not get the
> correct coordinates.
> Any idea why?
> >
>

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Google Web Toolkit" group.
To post to this group, send email to Google-Web-Toolkit@googlegroups.com
To unsubscribe from this group, send email to 
google-web-toolkit+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/Google-Web-Toolkit?hl=en
-~--~~~~--~~--~--~---



Re: Relative Position of a Widget

2009-04-15 Thread raulsan

But the thing is that it used to work perfectly with gwt-1.6.1 and

 int x = Event.getRelativeX(event, this.getElement());
 int y = Event.getRelativeY(event, this.getElement());

I don't understand why I can not override onBrowserEvent, I have also
overriden an addMouseListener
and I would not like to change much code, I thought there could be a
shortcut, but maybe the best way to go is to change again to 1.6.1
Thank you for you help anyway Vitali,
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Google Web Toolkit" group.
To post to this group, send email to Google-Web-Toolkit@googlegroups.com
To unsubscribe from this group, send email to 
google-web-toolkit+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/Google-Web-Toolkit?hl=en
-~--~~~~--~~--~--~---



Re: Relative Position of a Widget

2009-04-15 Thread Vitali Lovich
Can you post your code for your class.  I can't really comment further
without understanding what it is you are trying to do.

On Wed, Apr 15, 2009 at 7:56 PM, raulsan  wrote:

>
> But the thing is that it used to work perfectly with gwt-1.6.1 and
>
>  int x = Event.getRelativeX(event, this.getElement());
>  int y = Event.getRelativeY(event, this.getElement());
>
> I don't understand why I can not override onBrowserEvent, I have also
> overriden an addMouseListener
> and I would not like to change much code, I thought there could be a
> shortcut, but maybe the best way to go is to change again to 1.6.1
> Thank you for you help anyway Vitali,
> >
>

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Google Web Toolkit" group.
To post to this group, send email to Google-Web-Toolkit@googlegroups.com
To unsubscribe from this group, send email to 
google-web-toolkit+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/Google-Web-Toolkit?hl=en
-~--~~~~--~~--~--~---



Re: Relative Position of a Widget

2009-04-15 Thread raulsan



Yes, of course"

I did not post it before because I thought it was not the right place
to do it. The class is an extended class from GWTCanvas, but I was
asking about mouse events and about the disappearing of a
function  :)  and because of that I posted here.
The class is:

/**
 *
 */
package com.mapvs.client;

import com.google.gwt.i18n.client.NumberFormat;
import com.google.gwt.user.client.Event;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.ClickListenerCollection;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.MouseListener;
import com.google.gwt.user.client.ui.MouseListenerCollection;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.widgetideas.graphics.client.Color;
import com.google.gwt.widgetideas.graphics.client.GWTCanvas;

/**
 * @author raulsan
 *
 */
@SuppressWarnings("deprecation")
public class CompassCanvas extends GWTCanvas {

private MouseListenerCollection mouseListeners;
private ClickListenerCollection clickListeners;

private Label angleLab = new Label("0");
/**
 * the size of the canvas
 */
private int x = 90;
private int y = 90;
/**
 * the size of the compass
 */
private float radius = x/2 -5;
/**
 * distance from the top of the arrow
 * to the top of the stick
 */
private int distArr = (int)radius/5;
/**
 * the size of the arrow
 */
private int arrSize = (int)radius-distArr;
/**
 * The angle to draw the arrow
 */
private double alfa = 0;
/**
 * the with of the lines
 */
private int lineW = 7;
/**
 * the colors
 */
private Color c1 = Color.YELLOW;
private Color c2 = Color.BLUE;
/**
 * Constructor
 */
public CompassCanvas() {
super(90,90);
// draw
drawCompass(alfa,c1,c1);
// the listeners
this.sinkEvents(Event.MOUSEEVENTS | Event.ONCLICK);
this.addMouseListener(new MouseListener(){
// variable
boolean dragging = false;

public void onMouseDown(Widget sender, int x, int y) {
drawCompass(x, y, c2, c2);
dragging = true;
}

public void onMouseEnter(Widget sender) {
drawCompass(alfa, c2, c1);

}

public void onMouseLeave(Widget sender) {
drawCompass(alfa, c1, c1);
dragging = false;
}

public void onMouseMove(Widget sender, int x, int y) {
if (dragging)
drawCompass(x,y,c2,c2);
else
drawCompass(alfa,c2,c1);

}

public void onMouseUp(Widget sender, int x, int y) {
drawCompass(x,y,c2,c1);
dragging = false;
}

});
}
/**
 * Handling events
 */
public void onBrowserEvent(Event event) {
event.preventDefault();
super.onBrowserEvent(event);
if (mouseListeners != null) {
int x = Event.getRelativeX(event, this.getElement());
int y = Event.getRelativeY(event, this.getElement());
switch (event.getTypeInt()) {
case Event.ONCLICK:
if (clickListeners != null) {
clickListeners.fireClick(this);
}
break;
case Event.ONMOUSEDOWN:
if (mouseListeners != null) {
mouseListeners.fireMouseDown(this, x, y);
}
break;
case Event.ONMOUSEMOVE:
if (mouseListeners != null) {
mouseListeners.fireMouseMove(this, x, y);
}
break;
case Event.ONMOUSEUP:
if (mouseListeners != null) {
mouseListeners.fireMouseUp(this, x, y);
}
break;
case Event.ONMOUSEOUT:
if (mouseListeners != null) {
mouseListeners.fireMouseLeave(this);
}
 break;
}
}
}

/**
 *
 * @param listener
 */
public void addClickListener(ClickListener listener) {
if (clickListeners == null) {
clickListeners = new ClickListenerCollection();
sinkEvents(Event.ONCLICK);
}
clickListeners.add(listener);
}
/**

Re: Relative Position of a Widget

2009-04-15 Thread Vitali Lovich
I'm having a hard time finding where exactly addMouseListener is defined.
On a side note, I approached this problem in a different way by simply
adding an event preview handler Event#addNativePreviewHandler.  Also, you
should use handlers rather than listeners (not super important, but the
listeners are deprecated).

I'd recommend looking at how some of the native GWT widgets do their event
handling to get an idea of how to write your own.

On Wed, Apr 15, 2009 at 9:20 PM, raulsan  wrote:

>
>
>
> Yes, of course"
>
> I did not post it before because I thought it was not the right place
> to do it. The class is an extended class from GWTCanvas, but I was
> asking about mouse events and about the disappearing of a
> function  :)  and because of that I posted here.
> The class is:
>
> /**
>  *
>  */
> package com.mapvs.client;
>
> import com.google.gwt.i18n.client.NumberFormat;
> import com.google.gwt.user.client.Event;
> import com.google.gwt.user.client.ui.ClickListener;
> import com.google.gwt.user.client.ui.ClickListenerCollection;
> import com.google.gwt.user.client.ui.Label;
> import com.google.gwt.user.client.ui.MouseListener;
> import com.google.gwt.user.client.ui.MouseListenerCollection;
> import com.google.gwt.user.client.ui.Widget;
> import com.google.gwt.widgetideas.graphics.client.Color;
> import com.google.gwt.widgetideas.graphics.client.GWTCanvas;
>
> /**
>  * @author raulsan
>  *
>  */
> @SuppressWarnings("deprecation")
> public class CompassCanvas extends GWTCanvas {
>
>private MouseListenerCollection mouseListeners;
>private ClickListenerCollection clickListeners;
>
>private Label angleLab = new Label("0");
>/**
> * the size of the canvas
> */
>private int x = 90;
>private int y = 90;
>/**
> * the size of the compass
> */
>private float radius = x/2 -5;
>/**
> * distance from the top of the arrow
> * to the top of the stick
> */
>private int distArr = (int)radius/5;
>/**
> * the size of the arrow
> */
>private int arrSize = (int)radius-distArr;
>/**
> * The angle to draw the arrow
> */
>private double alfa = 0;
>/**
> * the with of the lines
> */
>private int lineW = 7;
>/**
> * the colors
> */
>private Color c1 = Color.YELLOW;
>private Color c2 = Color.BLUE;
>/**
> * Constructor
> */
>public CompassCanvas() {
>super(90,90);
>// draw
>drawCompass(alfa,c1,c1);
>// the listeners
>this.sinkEvents(Event.MOUSEEVENTS | Event.ONCLICK);
>this.addMouseListener(new MouseListener(){
>// variable
>boolean dragging = false;
>
>public void onMouseDown(Widget sender, int x, int y) {
>drawCompass(x, y, c2, c2);
>dragging = true;
>}
>
>public void onMouseEnter(Widget sender) {
>drawCompass(alfa, c2, c1);
>
>}
>
>public void onMouseLeave(Widget sender) {
>drawCompass(alfa, c1, c1);
>dragging = false;
>}
>
>public void onMouseMove(Widget sender, int x, int y) {
>if (dragging)
>drawCompass(x,y,c2,c2);
>else
>drawCompass(alfa,c2,c1);
>
>}
>
>public void onMouseUp(Widget sender, int x, int y) {
>drawCompass(x,y,c2,c1);
>dragging = false;
>}
>
>});
> }
>/**
> * Handling events
> */
>public void onBrowserEvent(Event event) {
> event.preventDefault();
> super.onBrowserEvent(event);
>if (mouseListeners != null) {
> int x = Event.getRelativeX(event, this.getElement());
>int y = Event.getRelativeY(event, this.getElement());
> switch (event.getTypeInt()) {
>case Event.ONCLICK:
>if (clickListeners != null) {
>clickListeners.fireClick(this);
>}
>break;
>case Event.ONMOUSEDOWN:
>if (mouseListeners != null) {
>mouseListeners.fireMouseDown(this, x, y);
>}
>break;
>case Event.ONMOUSEMOVE:
>if (mouseListeners != null) {
>mouseListeners.fireMouseMove(this, x, y);
>}
>break;
>case Event.ONMOUSEUP:
>if (mouseListeners != null) {
> 

Re: Relative Position of a Widget

2009-04-16 Thread raulsan

Ok, I will do that
I will post again if I find a solution
Thank you

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Google Web Toolkit" group.
To post to this group, send email to Google-Web-Toolkit@googlegroups.com
To unsubscribe from this group, send email to 
google-web-toolkit+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/Google-Web-Toolkit?hl=en
-~--~~~~--~~--~--~---



Re: Relative Position of a Widget

2009-04-16 Thread raulsan

UuujUuu!it works!
it is true that mouseHanlders are more comfortable to use than
listeners.
at the end I simply used:

int x = event.getX();
int y = event.getY();

inside the handlers. And looked at the image.java to get some help.
Cheers Vitali".
I post here the code with the entire class, maybe it can be of some
help to someone:
/**
 * @author raulsan
 */
public class CompassCanvas extends GWTCanvas{
/**
 * The label than indicates the angle
 * of the compass
 */
private Label angleLab = new Label("0");
/**
 * the size of the canvas
 */
private int x = 90;
private int y = 90;
/**
 * the size of the compass
 */
private float radius = x/2 -5;
/**
 * distance from the top of the arrow
 * to the top of the stick
 */
private int distArr = (int)radius/5;
/**
 * the size of the arrow
 */
private int arrSize = (int)radius-distArr;
/**
 * The angle to draw the arrow
 */
private double alfa = 0;
/**
 * the width of the lines
 */
private int lineW = 7;
/**
 * the colors
 */
private Color c1 = Color.YELLOW;
private Color c2 = Color.BLUE;
/**
 * A variable for drag and drop
 */
private boolean dragging = false;
/**
 * Constructor
 */
public CompassCanvas() {
super(90,90);
// draw
drawCompass(alfa,c1,c1);
// the mouse handlers
this.sinkEvents(Event.MOUSEEVENTS | Event.ONCLICK);
this.addMouseMoveHandler(new MouseMoveHandler(){
@Override
public void onMouseMove(MouseMoveEvent event) {
int x = event.getX();
int y = event.getY();
   if (dragging)
drawCompass(x,y,c2,c2);
   else
drawCompass(alfa,c2,c1);
}
});
this.addMouseOutHandler(new MouseOutHandler(){
@Override
public void onMouseOut(MouseOutEvent event) {
drawCompass(alfa, c1, c1);
dragging = false;
}
});
this.addMouseOverHandler(new MouseOverHandler(){
@Override
public void onMouseOver(MouseOverEvent event) {
drawCompass(alfa, c2, c1);
}
});
this.addMouseUpHandler(new MouseUpHandler(){
@Override
public void onMouseUp(MouseUpEvent event) {
int x = event.getX();
int y = event.getY();
drawCompass(x,y,c2,c1);
   dragging = false;
}
});
this.addMouseDownHandler(new MouseDownHandler(){
@Override
public void onMouseDown(MouseDownEvent event) {
int x = event.getX();
int y = event.getY();
drawCompass(x, y, c2, c2);
dragging = true;
}
});
}
/**
 *
 */
public void drawCompass(int w,int h, Color c1, Color c2){
this.clear();
// The angle to rotate
double i = getDistance(w,h,x/2,h);
double j = getDistance(w,h,w,y/2);
alfa = correctAngle(Math.atan2(i, j),w,h,x/2,y/2);
alfa = (alfa==-0)?0:alfa;
alfa = (alfa==-180)?180:alfa;
// call to the function to draw with alfa
drawCompass(alfa, c1, c2);
}
/**
 *
 */
public void drawCompass(double alfa, Color c1, Color c2){
this.clear();
// the circumference
this.saveContext();
this.setFillStyle(Color.WHITE);
this.setStrokeStyle(c1);
this.setLineWidth(lineW);
float startAngle = 0;
float endAngle = (float)Math.PI*2;
this.beginPath();
this.arc(x/2, y/2, radius, startAngle, endAngle, true);
this.stroke();
this.fill();
this.restoreContext();
// the arrow
this.saveContext();
this.translate(x/2, y/2);
this.rotate(-Math.PI/2 + alfa);
this.setLineCap("round");
this.setLineWidth(lineW/2);
this.setStrokeStyle(c2);
this.beginPath();
this.moveTo(0, 0);
this.lineTo(arrSize, 0);
this.stroke();
this.closePath();
// the point of the arrow
this.setStrokeStyle