static Icon reescala(Icon ic, int maxW, int maxH) { if (ic == null) { return null; } if (ic.getIconHeight() == maxH && ic.getIconWidth() == maxW) { return ic; } BufferedImage bi = new BufferedImage(ic.getIconHeight(), ic.getIconWidth(), BufferedImage.TYPE_INT_ARGB); Graphics g = bi.createGraphics(); ic.paintIcon(null, g, 0, 0); g.dispose(); Image bf = bi.getScaledInstance(maxW, maxH, Image.SCALE_SMOOTH); return new ImageIcon(bf); }
/** * Paints the image. * * @param g the rendering surface to use * @param a the allocated region to render into * @see View#paint */ public void paint(Graphics g, Shape a) { Color oldColor = g.getColor(); fBounds = a.getBounds(); int border = getBorder(); int x = fBounds.x + border + getSpace(X_AXIS); int y = fBounds.y + border + getSpace(Y_AXIS); int width = fWidth; int height = fHeight; int sel = getSelectionState(); // Make sure my Component is in the right place: /* if( fComponent == null ) { fComponent = new Component() { }; fComponent.addMouseListener(this); fComponent.addMouseMotionListener(this); fComponent.setCursor(Cursor.getDefaultCursor()); // use arrow cursor fContainer.add(fComponent); } fComponent.setBounds(x,y,width,height); */ // If no pixels yet, draw gray outline and icon: if (!hasPixels(this)) { g.setColor(Color.lightGray); g.drawRect(x, y, width - 1, height - 1); g.setColor(oldColor); loadIcons(); Icon icon = fImage == null ? sMissingImageIcon : sPendingImageIcon; if (icon != null) icon.paintIcon(getContainer(), g, x, y); } // Draw image: if (fImage != null) { g.drawImage(fImage, x, y, width, height, this); // Use the following instead of g.drawImage when // BufferedImageGraphics2D.setXORMode is fixed (4158822). // Use Xor mode when selected/highlighted. // ! Could darken image instead, but it would be more expensive. /* if( sel > 0 ) g.setXORMode(Color.white); g.drawImage(fImage,x, y, width,height,this); if( sel > 0 ) g.setPaintMode(); */ } // If selected exactly, we need a black border & grow-box: Color bc = getBorderColor(); if (sel == 2) { // Make sure there's room for a border: int delta = 2 - border; if (delta > 0) { x += delta; y += delta; width -= delta << 1; height -= delta << 1; border = 2; } bc = null; g.setColor(Color.black); // Draw grow box: g.fillRect(x + width - 5, y + height - 5, 5, 5); } // Draw border: if (border > 0) { if (bc != null) g.setColor(bc); // Draw a thick rectangle: for (int i = 1; i <= border; i++) g.drawRect(x - i, y - i, width - 1 + i + i, height - 1 + i + i); g.setColor(oldColor); } }