swing - Java - get click off the limits of JPanel -
i have following doubt: possible "mouse left click event" off limits of component mouselistener? or should try approach?
my problem following. creating wysiwyg panel suitable project. panel sibling panel displays images loaded according user selection. need get, instance, background color of image. when color clicked change bgcolor of wysiwyg panel. using robot
class color of pixel, works if image , color selector in same panel, won't be.
update: code mean. mainframe has 2 independent jframes. need rgb color of images on imageloader click on mousecolorpane. on case, robot can black border of jlabel.
import java.awt.*; import java.awt.event.mouseevent; import java.awt.event.mouselistener; import java.awt.image.bufferedimage; import java.io.file; import java.io.ioexception; import javax.imageio.imageio; import javax.swing.*; public class whatsmycolor { public static void main(string[] args) throws ioexception { new whatsmycolor(); } public whatsmycolor() { eventqueue.invokelater(new runnable() { @override public void run() { try { jframe frame = new jframe(); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.setlayout(new borderlayout()); final mousecolorpane mcp = new mousecolorpane(); frame.add(mcp,borderlayout.center); imageloader il = new imageloader(); frame.add(il,borderlayout.north); frame.setsize(800, 400); frame.setlocationrelativeto(null); frame.setvisible(true); } catch (exception exp) { exp.printstacktrace(); } } }); } public class imageloader extends jpanel { imageloader(){ bufferedimage img = null; try {img = imageio.read(new file("src/monkey-icon128.png"));} catch (ioexception e1) {e1.printstacktrace();} add(new jlabel (new imageicon (img))); bufferedimage img2 = null; try {img2 = imageio.read(new file("src/monkey-icon128.png"));} catch (ioexception e1) {e1.printstacktrace();} add(new jlabel (new imageicon (img2))); } } public class mousecolorpane extends jpanel { private robot robot; private jlabel color; public mousecolorpane() throws awtexception { setlayout(new gridbaglayout()); color = new jlabel(); color.setborder(borderfactory.createlineborder(color.black)); color.setpreferredsize(new dimension(100,100)); color.setfocusable(false); color.setopaque(true); color.setfocustraversalpolicyprovider(false); color.addmouselistener(new mouselistener() { @override public void mousereleased(mouseevent arg0) {} @override public void mousepressed(mouseevent arg0) {} @override public void mouseexited(mouseevent arg0) {} @override public void mouseentered(mouseevent arg0) {} @override public void mouseclicked(mouseevent arg0) { // todo auto-generated method stub pointerinfo pi; pi = mouseinfo.getpointerinfo(); updatecolor(pi.getlocation()); } }); add(color); robot = new robot(); setvisible(true); } protected void updatecolor(point p) { color pixelcolor = robot.getpixelcolor(p.x, p.y); color.setbackground(pixelcolor); } } }
you may idea of what's possible zoom
, uses robot
capture patch of desktop , getrgb()
determine color. click , drag capture; mouse on see tooltip containing rgb components of pixel.
Comments
Post a Comment