74 lines
1.7 KiB
Java
74 lines
1.7 KiB
Java
package nt.UI.util;
|
|
|
|
import java.awt.Component;
|
|
import java.awt.Graphics;
|
|
import java.awt.Image;
|
|
import java.awt.event.ComponentEvent;
|
|
import java.awt.event.ComponentListener;
|
|
import java.net.URL;
|
|
|
|
import javax.swing.ImageIcon;
|
|
import javax.swing.JComponent;
|
|
|
|
public class ResizeableImageIcon extends ImageIcon {
|
|
|
|
private static final long serialVersionUID = 1L;
|
|
private Component component;
|
|
private boolean prepareResize = false;
|
|
|
|
public ResizeableImageIcon(URL url) {
|
|
super(url);
|
|
}
|
|
|
|
@Override
|
|
public synchronized void paintIcon(Component c, Graphics g, int x, int y) {
|
|
int width, height;
|
|
if(this.component == null) {
|
|
this.component = c;
|
|
c.addComponentListener(new IconParentListener());
|
|
width = c.getWidth()-(2*Math.abs(x));
|
|
height = c.getHeight()-(2*Math.abs(y));
|
|
resizeIcon(width, height);
|
|
}
|
|
if(prepareResize) {
|
|
width = c.getWidth()-(2*Math.abs(x));
|
|
height = c.getHeight()-(2*Math.abs(y));
|
|
resizeIcon(width, height);
|
|
prepareResize=false;
|
|
}
|
|
|
|
if(getImageObserver() == null) {
|
|
g.drawImage(getImage(), Math.abs(x), Math.abs(y), c);
|
|
} else {
|
|
g.drawImage(getImage(), Math.abs(x), Math.abs(y), getImageObserver());
|
|
}
|
|
}
|
|
|
|
protected void resizeIcon(int width, int height) {
|
|
if(this.component == null) {
|
|
return;
|
|
}
|
|
setImage(IconResizer.resizeImage(getImage(), width, height));
|
|
}
|
|
|
|
|
|
private class IconParentListener implements ComponentListener {
|
|
|
|
@Override
|
|
public void componentResized(ComponentEvent e) {
|
|
prepareResize=true;
|
|
}
|
|
|
|
@Override
|
|
public void componentMoved(ComponentEvent e) {}
|
|
|
|
@Override
|
|
public void componentShown(ComponentEvent e) {}
|
|
|
|
@Override
|
|
public void componentHidden(ComponentEvent e) {}
|
|
|
|
}
|
|
|
|
}
|