package jetbrains.mps.editor.contextActionsTool.pluginSolution.plugin;
/*Generated by MPS */
import javax.swing.Icon;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.Graphics2D;
public class ScaledIcon implements Icon {
public static Icon scaleIfNeeded(int width, int height, Icon unscaled) {
if (width == unscaled.getIconWidth() && height == unscaled.getIconHeight()) {
return unscaled;
} else {
return new ScaledIcon(width, height, unscaled);
}
}
public static Icon scaleIfNeeded(int height, Icon unscaled) {
if (height == unscaled.getIconHeight()) {
return unscaled;
} else {
return new ScaledIcon(((double) height) / unscaled.getIconHeight(), unscaled);
}
}
private double myScale;
private Icon myUnscaled;
public ScaledIcon(int width, int height, Icon unscaled) {
myUnscaled = unscaled;
double scaleX = ((double) width) / myUnscaled.getIconWidth();
double scaleY = ((double) height) / myUnscaled.getIconHeight();
myScale = Math.min(scaleX, scaleY);
}
public ScaledIcon(double scale, Icon unscaled) {
myUnscaled = unscaled;
myScale = scale;
}
@Override
public int getIconHeight() {
return ((int) Math.round(Math.ceil(myUnscaled.getIconHeight() * myScale)));
}
@Override
public int getIconWidth() {
return ((int) Math.round(Math.ceil(myUnscaled.getIconWidth() * myScale)));
}
@Override
public void paintIcon(Component component, Graphics g_, int x, int y) {
Graphics2D g = ((Graphics2D) g_.create());
try {
g.translate(x, y);
g.scale(myScale, myScale);
myUnscaled.paintIcon(component, g, 0, 0);
} finally {
g.dispose();
}
}
}