Sunday, 15 March 2015

java - How to determine what user action triggered a JDialog resize event? -



java - How to determine what user action triggered a JDialog resize event? -

i have added componentadapter componentlistener jdialog, , want implement componentresized(componentevent e). goal maintain jdialog's ratio, , calculation of how resize according user's action, know if user dragged window's side edge, window's top/bottom edge, or 1 of window's corners.

is there way retrieve info componentevent?

i don't know reply original question (is ther way tell source of componentevent), store previous dimensions, , calculate new size. if helps anyone, code using. enjoy:

public class resizableimageview extends jdialog { bufferedimage img; dimension originalimgsize; double imageratio; jpanel jp; jlabel label; dimension previoussize; public resizableimageview(bufferedimage img) { ... setresizable(true); this.img = img; originalimgsize = new dimension(img.getwidth(), img.getheight()); imageratio = (double)img.getwidth() / (double)img.getheight(); label = new jlabel(new imageicon(img)); label.setminimumsize(originalimgsize); jp = new jpanel(); jp.setborder(new emptyborder(0, 0, 0, 0)); jp.add(label); setcontentpane(jp); pack(); previoussize = getsize(); addcomponentlistener(new componentadapter() { private boolean allreadyresized = true; private void resize(image img) { imageicon ico = new imageicon(img); label = new jlabel(ico); label.setborder(new emptyborder(0, 0, 0, 0)); label.setpreferredsize(new dimension(img.getwidth(null), img.getheight(null))); jp = new jpanel(); jp.setborder(new emptyborder(0, 0, 0, 0)); jp.add(label); setcontentpane(jp); allreadyresized = true; pack(); previoussize = getsize(); } @override public void componentresized(componentevent e) { if (allreadyresized) { allreadyresized = false; return; } double xchange = (double) math.abs(getsize().width - previoussize.width); double ychange = (double) math.abs(getsize().height - previoussize.height); if (xchange == 0 && ychange == 0) return; int height = getsize().height - getinsets().bottom - getinsets().top; int width = getsize().width - getinsets().left - getinsets().right; if (height > originalimgsize.height || width > originalimgsize.width) { resize(img); return; } if (ychange == 0 || xchange/ychange > imageratio) { resize(img.getscaledinstance(width, -1, image.scale_smooth)); return; } else { resize(img.getscaledinstance(-1, height, image.scale_smooth)); return; } } }); } }

java resize awt eventlistener

No comments:

Post a Comment