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);
    }
    /**
     *
     * @param listener
     */
    public void addMouseListener(MouseListener listener) {
        if (mouseListeners == null) {
          mouseListeners = new MouseListenerCollection();
          sinkEvents(Event.MOUSEEVENTS);
        }
        mouseListeners.add(listener);
     }

      public void removeClickListener(ClickListener listener) {
        if (clickListeners != null) {
          clickListeners.remove(listener);
        }
      }

      public void removeMouseListener(MouseListener listener) {
        if (mouseListeners != null) {
          mouseListeners.remove(listener);
        }
      }

    /**
     *
     */
    public void drawCompass(int w,int h, Color c1, Color c2){
                this.clear();
                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 corner 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
     */
    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
     */
    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;
    }
}

--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to