Bogdan Muresan
Bogdan Muresan

Reputation: 35

How to move a shape in java and calculate the coordinates when dragged?

I want to drag a shape (for example a line2d,rectangle2D or Ellipse2D) What I do is get the pressed point and the dragged point from the respective point(the point i get from event returned point). However the method setFrame(p.x,p.y,height,width) move sharply the left shape's corner to the p.x , p.y coordinates(which are dragged returned point ) and after that, drag correctly .

In other words ,when i drag the shape ,I want the cursor and the shape move where I will drag the cursor and not sharply go to the left shape corner and then move the shape.

How Can I calculate those coordinates correctly?

my listener events are:

private void formMouseDragged(java.awt.event.MouseEvent evt) {                                  
    if(getEditar()){//estamos en modo edicion
       // pFinal=evt.getPoint();
        Shape s=getShape();
        //MiLinea2D linea=(MiLinea2D)s;
        //linea.setPointP2(evt.getPoint());
        setLocation(s, evt.getPoint());
        this.repaint();
    }else{
        pFinal=evt.getPoint();
        updateShape();
        this.repaint();
    }
}                                 
/**
 * Evento que gestiona la liberación del ratón
 * @param evt 
 */
private void formMouseReleased(java.awt.event.MouseEvent evt) {                                   
    if(getEditar()){

    }else{
        pInicial=null;
        pFinal=null;
    }
}                                  
/**
 * Evento que gestion presionar el ratón
 * @param evt 
 */
private void formMousePressed(java.awt.event.MouseEvent evt) {                                  
    if(getEditar()){
       pInicial=evt.getPoint();
       Shape s= getSlectedShape(evt.getPoint());
       setShape(s);
       //if(getShape() instanceof MiLinea2D)
       //     System.out.println("es una lina la seleccionada");
      // else System.out.println("no es una linea");
    }else{
        pInicial=evt.getPoint();
        createShape();

    }
} 

my createShape and updateShape methods:

void createShape(){
    Shape s = null;
    switch(formaActiva){
        //dibujar punto
        case 0:
            //vamos a crear un rectangulo
          s=new Rectangle2D.Double(pInicial.x,pInicial.y,0,0);
        break;
        //dibujar linea
        case 1: 
            s=new MiLinea2D();
        break;
        //dibujar rectangulo
        case 2:
            s=new Rectangle2D.Double(pInicial.x,pInicial.y,0,0);
        break;
        //dibujar elipse
        case 3:
           s=new Ellipse2D.Double(pInicial.x,pInicial.y,0,0);
        break;
    }
    if(s!=null)
        vShape.add(s); 
}//fin createShape()

/**
 * Metodo updateShape que modifica las formas
 */
void updateShape(){
    //Shape s=null; 
    switch(formaActiva){
        //actualizar punto
        case 0:
            //no hay que actualizar el punto
        break;
        //actualizar linea
        case 1 :
            MiLinea2D linea;
            linea=(MiLinea2D)vShape.get(vShape.size()-1);
            linea.setLine((double)pInicial.x,(double)pInicial.y,    (double)pFinal.x,(double)pFinal.y);
        break;
        //actualizar el rectangulo
        case 2:
            Rectangle2D rec;
            rec=(Rectangle2D.Double)vShape.get(vShape.size()-1);
            rec.setFrameFromDiagonal(pInicial, pFinal); 
        break;
        //actualizar elipse
        case 3:
            Ellipse2D elipse;
            elipse=(Ellipse2D)vShape.get(vShape.size()-1);
            elipse.setFrameFromDiagonal(pInicial, pFinal);
        break;

    }//fin switch
}//fin metodo updateShape   

and my setLocation(Shape s,Point2D p) is:

public void setLocation(Shape s,Point2D p)
{
    if(s instanceof Rectangle2D)
    {
        Rectangle2D rec=(Rectangle2D.Double)s;
        //rec.setLocation((Point) p);
        double altura,anchura;
        altura=rec.getHeight();
        anchura=rec.getWidth();
        rec.setFrame(p.getX(),p.getY(), anchura, altura);
    }

    if(s instanceof Line2D)
    {
        MiLinea2D linea=(MiLinea2D)s;
        linea.setLocation(p);
    }
    if(s instanceof Ellipse2D)
    {
        Ellipse2D elipse=(Ellipse2D.Double)s;
        double altura,anchura;
        altura=elipse.getHeight();
        anchura=elipse.getWidth();
        elipse.setFrame(p.getX(),p.getY(), anchura, altura);

    }

}                             

Upvotes: 0

Views: 1335

Answers (1)

Bogdan Muresan
Bogdan Muresan

Reputation: 35

Well ,I found the solution.In the method :
private void formMousePressed(java.awt.event.MouseEvent evt); I use a new Point "puntoEsquinaFigura" tu get the left corner coordinates of the Shape.

 private void formMousePressed(java.awt.event.MouseEvent evt) {                                  
    if(getEditar()){
       pInicial=evt.getPoint();
       Shape s= getSlectedShape(evt.getPoint());
       puntoEsquinaFigura=new Point();
       try{
       puntoEsquinaFigura.x=s.getBounds().x;
       puntoEsquinaFigura.y=s.getBounds().y;
       }catch(Exception e){
           puntoEsquinaFigura=null;
       }
       setShape(s);
    }else{
        if(getFormaActiva()!=0)
        {
            pInicial=evt.getPoint();
            createShape();
        }


    }
}

In the method, public void setLocation(Shape s,Point2D p); I calculate the new coordinates of the Shape. All this I do it for Shape don't "jump" when i dragged it.

MiPunto2D is a class that extend of java.awt.Rectangle.

public void setLocation(Shape s,Point2D p)
{
    if(puntoEsquinaFigura!=null){
        //calculamos el desplazamiento
        int deltax=(int) (p.getX()-pInicial.x);
        int deltay=(int) (p.getY()-pInicial.y);

        if(s instanceof MiPunto2D)
        {
            Rectangle rec=(Rectangle)s;
            rec.setLocation(deltax+puntoEsquinaFigura.x, deltay+puntoEsquinaFigura.y);
        }
        if(s instanceof Rectangle2D)
        {
            Rectangle rec=(Rectangle)s;
            rec.setLocation(deltax+puntoEsquinaFigura.x, deltay+puntoEsquinaFigura.y);

        }
        if(s instanceof MiLinea2D)
        {
            MiLinea2D linea=(MiLinea2D)s;
            linea.setLocation(new Point(deltax+puntoEsquinaFigura.x, deltay+puntoEsquinaFigura.y));
        }
        if(s instanceof Ellipse2D)
        {

            Ellipse2D elipse=(Ellipse2D.Double)s;
            double altura=elipse.getHeight();
            double anchura=elipse.getWidth();
            elipse.setFrame(deltax+puntoEsquinaFigura.x, deltay+puntoEsquinaFigura.y, anchura,altura);

        }
    }
}

Upvotes: 1

Related Questions