java - Issue with addMouseMotionListener getting wrong coordinates -


i borrowed class below make selection area tool project. has issue when try make selection when content not aligned @ top-left, mouse coordinates related scrollpane, draws on image - see ss better understanding: enter image description here

sscce:

import java.awt.borderlayout; import java.awt.color; import java.awt.dimension; import java.awt.graphics2d; import java.awt.point; import java.awt.rectangle; import java.awt.robot; import java.awt.toolkit; import java.awt.event.*; import java.awt.image.*;  import javax.swing.*;  /** getting rectangle of interest on screen. requires motivatedenduser api - sold separately. */ public class screencapturerectangle {      rectangle capturerect;      screencapturerectangle(final bufferedimage screen) {         final bufferedimage screencopy = new bufferedimage(screen.getwidth(), screen.getheight(), screen.gettype());         final jlabel screenlabel = new jlabel(new imageicon(screencopy));         jscrollpane screenscroll = new jscrollpane(screenlabel);          screenscroll.setpreferredsize(new dimension((int)(screen.getwidth()*2), (int)(screen.getheight()*2)));          jpanel panel = new jpanel(new borderlayout());         panel.add(screenscroll, borderlayout.center);          final jlabel selectionlabel = new jlabel("drag rectangle in screen shot!");         panel.add(selectionlabel, borderlayout.south);          repaint(screen, screencopy);         screenlabel.repaint();          screenlabel.addmousemotionlistener(new mousemotionadapter() {              point start = new point();              @override             public void mousemoved(mouseevent me) {                 start = me.getpoint();                 repaint(screen, screencopy);                 selectionlabel.settext("start point: " + start);                 screenlabel.repaint();             }              @override             public void mousedragged(mouseevent me) {                 point end = me.getpoint();                 capturerect = new rectangle(start, new dimension(end.x-start.x, end.y-start.y));                 repaint(screen, screencopy);                 screenlabel.repaint();                 selectionlabel.settext("rectangle: " + capturerect);             }         });          joptionpane.showmessagedialog(null, panel);          system.out.println("rectangle of interest: " + capturerect);     }      public void repaint(bufferedimage orig, bufferedimage copy) {         graphics2d g = copy.creategraphics();         g.drawimage(orig,0,0, null);         if (capturerect!=null) {             g.setcolor(color.red);             g.draw(capturerect);             g.setcolor(new color(255,255,255,150));             g.fill(capturerect);         }         g.dispose();     }      public static void main(string[] args) throws exception {         robot robot = new robot();         final dimension screensize = toolkit.getdefaulttoolkit().getscreensize();          final bufferedimage screen = robot.createscreencapture(new rectangle(300,0,300,300));          swingutilities.invokelater(new runnable() {             public void run() {                 new screencapturerectangle(screen);             }         });     } } 

i think have problems because attempting center image in panel.

the easiest solution make sure image painted top/left of panel:

    final jlabel screenlabel = new jlabel(new imageicon(screencopy));     screenlabel.sethorizontalalignment(jlabel.left);     screenlabel.setverticalalignment(jlabel.top); 

Comments

Popular posts from this blog

google api - Incomplete response from Gmail API threads.list -

Installing Android SQLite Asset Helper -

Qt Creator - Searching files with Locator including folder -