@Override
	public void paintIcon( Component c, Graphics g, int x, int y ) {
/*debug
		g.setColor( Color.red );
		g.drawRect( x, y, getIconWidth() - 1, getIconHeight() - 1 );
debug*/

		// scale factor
		double systemScaleFactor = UIScale.getSystemScaleFactor( (Graphics2D) g );
		float userScaleFactor = UIScale.getUserScaleFactor();
		double scaleFactor = systemScaleFactor * userScaleFactor;

		// paint input image icon if not necessary to scale
		if( scaleFactor == 1 && imageIcon != null && iconWidth == imageIcon.getIconWidth() && iconHeight == imageIcon.getIconHeight() ) {
			imageIcon.paintIcon( c, g, x, y );
			return;
		}

		// paint cached scaled icon
		if( systemScaleFactor == lastSystemScaleFactor &&
			userScaleFactor == lastUserScaleFactor &&
			lastImage != null )
		{
			paintLastImage( g, x, y );
			return;
		}

		// destination image size
		int destImageWidth = (int) Math.round( iconWidth * scaleFactor );
		int destImageHeight = (int) Math.round( iconHeight * scaleFactor );

		// get resolution variant of image if it is a multi-resolution image
		Image image = getResolutionVariant( destImageWidth, destImageHeight );

		// size of image
		int imageWidth = -1;
		int imageHeight = -1;

		if (image != null) {
			imageWidth = image.getWidth( null );
			imageHeight = image.getHeight( null );
		}

		// paint red rectangle if image has invalid size (e.g. not found)
		if( imageWidth < 0 || imageHeight < 0 ) {
			g.setColor( Color.red );
			g.fillRect( x, y, getIconWidth(), getIconHeight() );
			return;
		}

		// scale image if necessary to destination size
		if( imageWidth != destImageWidth || imageHeight != destImageHeight ) {
			// determine scaling method; default is "quality"
			Object scalingInterpolation = RenderingHints.VALUE_INTERPOLATION_BICUBIC;
			float imageScaleFactor = (float) destImageWidth / (float) imageWidth;
			if( ((int) imageScaleFactor) == imageScaleFactor &&
				imageScaleFactor > 1f &&
				imageWidth <= 16 &&
				imageHeight <= 16 )
			{
				// use "speed" scaling for small icons if the scale factor is an integer
				// to avoid blurred icons
				scalingInterpolation = RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR;
			}

			// scale image
			BufferedImage bufferedImage = image2bufferedImage( image );
			image = scaleImage( bufferedImage, destImageWidth, destImageHeight, scalingInterpolation );
		}

		// cache image
		lastSystemScaleFactor = systemScaleFactor;
		lastUserScaleFactor = userScaleFactor;
		lastImage = image;

		// paint image
		paintLastImage( g, x, y );
	}
Scales the given image icon using the system and user scale factors and
paints the icon at system scale factor 1x. This gives best scaling quality.
If the given image icon supports multiple resolutions, the best resolution
variant is used. The last scaled image is cached for faster repainting.

@author Karl Tauber
flatlaf-core/src/main/java/com/formdev/flatlaf/util/ScaledImageIcon.java