/** Select or grow image when clicked. */ public void mousePressed(MouseEvent e) { Dimension size = fComponent.getSize(); if (e.getX() >= size.width - 7 && e.getY() >= size.height - 7 && getSelectionState() == 2) { // Click in selected grow-box: if (DEBUG) System.out.println("ImageView: grow!!! Size=" + fWidth + "x" + fHeight); Point loc = fComponent.getLocationOnScreen(); fGrowBase = new Point(loc.x + e.getX() - fWidth, loc.y + e.getY() - fHeight); fGrowProportionally = e.isShiftDown(); } else { // Else select image: fGrowBase = null; JTextComponent comp = (JTextComponent) fContainer; int start = fElement.getStartOffset(); int end = fElement.getEndOffset(); int mark = comp.getCaret().getMark(); int dot = comp.getCaret().getDot(); if (e.isShiftDown()) { // extend selection if shift key down: if (mark <= start) comp.moveCaretPosition(end); else comp.moveCaretPosition(start); } else { // just select image, without shift: if (mark != start) comp.setCaretPosition(start); if (dot != end) comp.moveCaretPosition(end); } } }
/** Resize image if initial click was in grow-box: */ public void mouseDragged(MouseEvent e) { if (fGrowBase != null) { Point loc = fComponent.getLocationOnScreen(); int width = Math.max(2, loc.x + e.getX() - fGrowBase.x); int height = Math.max(2, loc.y + e.getY() - fGrowBase.y); if (e.isShiftDown() && fImage != null) { // Make sure size is proportional to actual image size: float imgWidth = fImage.getWidth(this); float imgHeight = fImage.getHeight(this); if (imgWidth > 0 && imgHeight > 0) { float prop = imgHeight / imgWidth; float pwidth = height / prop; float pheight = width * prop; if (pwidth > width) width = (int) pwidth; else height = (int) pheight; } } resize(width, height); } }