gpt4 book ai didi

java - 如何在java中移动形状并计算拖动时的坐标?

转载 作者:行者123 更新时间:2023-12-01 11:38:27 25 4
gpt4 key购买 nike

我想要拖动一个形状(例如 line2d、rectangle2D 或 Ellipse2D)我所做的是从相应的点(我从事件返回点获得的点)获取按下的点和拖动的点。然而方法 setFrame(p.x,p.y,height,width) 将左侧形状的角急剧移动到 p.x , p.y 坐标(拖动返回点),然后正确拖动。

换句话说,当我拖动形状时,我希望光标和形状移动到我将拖动光标的位置,而不是急剧转到左形状角,然后移动形状。

如何正确计算这些坐标?

我的监听器事件是:

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();

}
}

我的 createShape 和 updateShape 方法:

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

我的 setLocation(Shape s,Point2D p) 是:

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);

}

}

最佳答案

嗯,我找到了解决方案。在方法中:
私有(private)无效 formMousePressed(java.awt.event.MouseEvent evt);我使用新的点“puntoEsquinaFigura”来获取形状的左角坐标。

 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();
}


}
}

该方法中,public void setLocation(Shape s,Point2D p);我计算形状的新坐标。我做这一切是为了形状在拖动它时不会“跳跃”。

MiPunto2D 是 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);

}
}
}

关于java - 如何在java中移动形状并计算拖动时的坐标?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29759363/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com