@Override protected void paintComponent(Graphics g) { // Image Dimension size = getSize(); if (img != null) { Dimension imgSize = new Dimension(img.getWidth(null), img.getHeight(null)); SwingUtil.constrainTo(imgSize, size.width, size.height); // Border int x = (size.width - imgSize.width) / 2; int y = (size.height - imgSize.height) / 2; g.drawImage(img, x, y, imgSize.width, imgSize.height, null); g.setColor(Color.black); g.drawRect(x, y, imgSize.width - 1, imgSize.height - 1); } }