我想拖动一个形状(例如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);
}
}
答案 0 :(得分:1)
好吧,我找到了解决方案。方法:
private void formMousePressed(java.awt.event.MouseEvent evt);我用一个新的点“puntoEsquinaFigura”tu获得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();
}
}
}
在方法中,public void setLocation(Shape s,Point2D p); 我计算了Shape的新坐标。 所有这些我为Shape做的都不会在我拖动时“跳”。
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);
}
}
}