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(c2);
this.setFillStyle(c2);
this.beginPath();
this.moveTo(arrSize+distArr-(lineW/2), 0);
this.lineTo(arrSize-distArr, distArr);
this.lineTo(arrSize, 0);
this.lineTo(arrSize-distArr, -distArr);
this.lineTo(arrSize+distArr-(lineW/2), 0);
this.stroke();
this.fill();
this.closePath();
this.restoreContext();
// The angle
angleLab.setText(NumberFormat.getFormat("#,##0").format(alfa*360/
(2*Math.PI)));
}
/**
* Gives the distance between 2 points (x,y) and (i,j)
* @return double
*/
public double getDistance(int x, int y,int i,int j){
return Math.sqrt(Math.pow(x-i, 2)+Math.pow(y-j, 2));
}
/**
* Gives the right angle inside the compass
* @return
*/
public double correctAngle(double alfa,int w,int h,int x,int y){
double beta = 0;
boolean a = w>x;
boolean b = h<y;
if (a&&b){
//1st quadrant
beta = alfa;
}else if (!a&&b){
//2nd quadrant
beta = -alfa;
}else if (!a&&!b){
//3er quadrant
beta = -Math.PI+alfa;
}else if (a&&!b){
//4th quadrant
beta = Math.PI-alfa;
}
return beta;
}
/**
* The Mouse Handlers
*/
public HandlerRegistration addMouseMoveHandler(MouseMoveHandler
handler) {
return addDomHandler(handler, MouseMoveEvent.getType());
}
public HandlerRegistration addMouseOutHandler(MouseOutHandler
handler) {
return addDomHandler(handler, MouseOutEvent.getType());
}
public HandlerRegistration addMouseOverHandler(MouseOverHandler
handler) {
return addDomHandler(handler, MouseOverEvent.getType());
}
public HandlerRegistration addMouseUpHandler(MouseUpHandler
handler) {
return addDomHandler(handler, MouseUpEvent.getType());
}
public HandlerRegistration addMouseDownHandler(MouseDownHandler
handler) {
return addDomHandler(handler, MouseDownEvent.getType());
}
/**
* @return the angleLab
*/
public Label getAngleLab() {
return angleLab;
}
/**
* @return the alfa
*/
public double getAlfa() {
return alfa;
}
}
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Google Web Toolkit" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/Google-Web-Toolkit?hl=en
-~----------~----~----~----~------~----~------~--~---