java - How do I get images to move without them first jumping to position of the mouse cursor when using mouseDragged? -
i have program able move images around on screen clicking , dragging. problem is, when click image , start drag around on panel, image first jumps position of mouse cursor. how prevent this, image moved no matter click on image? can mouse cursor either jump under mouse arrow when starting drag, or on mouse arrow when starting drag, or set middle of image drag.
i moving image object of image class in class following function:
moveimage(selected,x,y);
where x , y coordinates mousedragged method. image class:
import javax.imageio.imageio; import java.awt.*; import java.awt.image.bufferedimage; import java.io.file; import java.io.ioexception; class image { private int x; private int y; private bufferedimage image; private stringbuffer filepath; private boolean isturned; public image(int x, int y, string filepath) throws ioexception { this.filepath = new stringbuffer(filepath); this.x = x; this.y = y; this.image = imageio.read(new file(string.valueof(filepath))); } public void draw(graphics g){ g.drawimage(image, x, y, null); } public void undraw (graphics g, color c ){ g.setcolor(c); g.fillrect(x,y, image.getwidth(), image.getheight()); } public boolean containsxy (int x, int y){ if ( (this.x <= x ) && (x <= (this.x+this.image.getwidth())) && (this.y <= y ) && (y <= (this.y+this.image.getheight())) ){ return true; } return false; } public void move (graphics g, int x, int y) { system.out.println("int x = " + x + " int y = " + y); system.out.println("this x = " + this.x + " y = " + this.y); system.out.println("width: " + this.image.getwidth() + " height: " + this.image.getheight()); undraw(g, color.white); /* this.x = x - ((x+image.getwidth())-x); this.y = y -((y+image.getheight())-y); this.x = x - (x-this.x); this.y = y - (y-this.y); this.x = x - (this.x+image.getwidth()-x); this.y = y - (this.y+image.getheight()-y); this.x = this.x + (x-image.getwidth()); this.y = this.y + (y-image.getheight()); this.x = x - (image.getwidth()); this.y = y - (image.getheight());*/ this.x = x - (image.getwidth()/2); this.y = y - (image.getheight()/2); draw(g); } public void turn(graphics g) throws ioexception { if (isturned) { filepath.replace(filepath.length()-9, filepath.length(), ".gif" ); undraw(g, color.white); image = imageio.read(new file(string.valueof(filepath))); draw(g); isturned = false; } else { filepath.replace(filepath.length()-4, filepath.length(), " back.gif" ); undraw(g, color.white); image = imageio.read(new file(string.valueof(filepath))); draw(g); isturned = true; } } }
as can see, inside move method, have tried alot of different things on manipulation incoming mouse event dragged coordinates. right in code using
this.x = x - (image.getwidth()/2); this.y = y - (image.getheight()/2);
which makes mouse cursor lock on middle of image when dragging around best can do. when want move image , click in corner of image, "pops" middle of image , locks there while dragging image around. not want this. want mouse cursor stay on location on image clicked when started drag, not redraw image somewhere else , start drag.
this class call image object, calling inside class:s method moveimage:
import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.io.file; import java.io.ioexception; import java.util.arraylist; import java.util.collection; class paintsurface extends jlabel implements mouselistener, mousemotionlistener { private int x, y; private jbutton browse; private collection<image> images = new arraylist<image>(); private final jfilechooser fc = new jfilechooser(); private image selected; public paintsurface(jbutton b){ browse = b; addmouselistener(this); addmousemotionlistener(this); browse.addactionlistener(new actionlistener() { @override public void actionperformed(actionevent e) { int x = fc.showopendialog(browse); if (x == jfilechooser.approve_option){ try { buttonpressed(fc); } catch (ioexception e1) { e1.printstacktrace(); } } else if (x ==jfilechooser.cancel_option){ system.out.println("no file selected."); } } }); } public void paintcomponent (graphics g){ super.paintcomponent(g); (image i: images){ i.draw(g); } } public void addimage(image i){ images.add(i); graphics g = getgraphics(); i.draw(g); } public void buttonpressed(jfilechooser fc) throws ioexception { file selectedfile = fc.getselectedfile(); string filepath = string.valueof(selectedfile.getabsolutepath()); image = new image(x, y, filepath ); selected = i; addimage(i); repaint(); } public image findimage(int x, int y){ image[] imagearray = images.toarray(new image[images.size()]); (int = imagearray.length - 1; >= 0; i--){ if (imagearray[i].containsxy(x, y)){ return imagearray[i]; } } return null; } public void moveimage (image i, int x, int y) { // i.move(getgraphics(), x, y); } public boolean removeimage(image i){ graphics g = getgraphics(); i.undraw(g, color.white); return images.remove(i); } @override public void mouseclicked(mouseevent e) { x = e.getx(); y = e.gety(); system.out.println("mouseclick"); selected = findimage(x,y); if (selected != null) { graphics g = getgraphics(); try { selected.turn(g); repaint(); } catch (ioexception e1) { e1.printstacktrace(); } selected = null; } } @override public void mousepressed(mouseevent e) { image = findimage(e.getx(), e.gety()); if (i == null) { system.out.println("null mousepress"); return; } else { system.out.println("not null mousepress"); if (i == selected){ return; } else { removeimage(i); addimage(i); selected = i; } } } @override public void mousedragged(mouseevent e) { int x = e.getx(); int y = e.gety(); if (selected != null) { moveimage(selected,x,y); repaint(); } } @override public void mousereleased(mouseevent e) { selected = null; } @override public void mouseentered(mouseevent e) { } @override public void mouseexited(mouseevent e) { } @override public void mousemoved(mouseevent e) { } }
can experience in help? pulling hair since week now...
you need track change clicked instead of moving new cursor position. in mousepressed
, store original press location:
// lastx, lasty object variables lastx = e.getx(); lasty = e.gety();
in mousedragged
, calculate delta
public void mousedragged(mouseevent e) { int x = i.getx() + e.getx() - lastx; int y = i.gety() + e.gety() - lasty; if (selected != null) { moveimage(selected,x,y); repaint(); } lastx = e.getx(); lasty = e.gety(); }
Comments
Post a Comment