java - Drag Multiple Objects Simultaneously -
how drag multiple selected objects , move around within frame?
the current implementation whenever user click on object, pressing shift key, each new object added multishape
however,i able move single selected object instead.
my code here:
public void mouseclicked(mouseevent clickevent) { clickshape = null; int x = clickevent.getx(); // x-coordinate of point mouse // clicked int y = clickevent.gety(); // y-coordinate of point if (clickevent.isshiftdown()) { int top = shapes.size(); (int = 0; < top; i++) { shape s = (shape) shapes.get(i); if (s.containspoint(x, y)) { s.setcolor(color.red); multishape.add(s); } } repaint(); } } }
my moving object defined under abstract shape class follow:
void moveto(int x, int y) { left += x; top += y; }
mouseclick event:
public void mousepressed(mouseevent clickevent) { if (dragshape != null) { return; } int x = clickevent.getx(); // x-coordinate of point int y = clickevent.gety(); // y-coordinate of point clickshape = null; (int = shapes.size() - 1; >= 0; i--) { // check shapes // front shape s = (shape) shapes.get(i); if (s.containspoint(x, y)) { dragshape = s; prevdragx = x; prevdragy = y; clickshape = s; break; } } // continue here if (clickshape == null) { return; } else if (clickevent.ispopuptrigger()) { click.show(this, x - 10, y - 2); } else { dragshape = clickshape; prevdragx = x; prevdragy = y; } } public void mousedragged(mouseevent clickevent) { // user has moved mouse. move dragged shape same // amount. if (dragshape == null) { return; } int x = clickevent.getx(); int y = clickevent.gety(); if (dragshape != null) { dragshape.moveto(x - prevdragx, y - prevdragy); prevdragx = x; prevdragy = y; repaint(); // redraw canvas show shape in new position } } public void mousereleased(mouseevent clickevent) { // user has released mouse. move dragged shape, set // shapebeingdragged null indicate dragging over. if (dragshape == null) { return; } int x = clickevent.getx(); int y = clickevent.gety(); if (clickevent.ispopuptrigger()) { click.show(this, x - 10, y - 2); // } else { dragshape.moveto(x - prevdragx, y - prevdragy); if (dragshape.left >= getsize().width || dragshape.top >= getsize().height || dragshape.left + dragshape.width < 0 || dragshape.top + dragshape.height < 0) { // shape shapes.remove(dragshape); // remove shape list } repaint(); } dragshape = null; }
you calling moveto 1 shape, called dragshape, last shape here
for (int = shapes.size() - 1; >= 0; i--) { // check shapes // front shape s = (shape) shapes.get(i); if (s.containspoint(x, y)) { dragshape = s; prevdragx = x; prevdragy = y; clickshape = s; break; } }
instead of doing:
if (dragshape != null) { dragshape.moveto(x - prevdragx, y - prevdragy);
do:
for (shape s : multishape) s.moveto(x - prevdragx, y - prevdragy);
Comments
Post a Comment