-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathResizableIcon.java
More file actions
97 lines (85 loc) · 2.33 KB
/
ResizableIcon.java
File metadata and controls
97 lines (85 loc) · 2.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/*
* Open Source Physics software is free software as described near the bottom of this code file.
*
* For additional information and documentation on Open Source Physics please see:
* <http://www.opensourcephysics.org/>
*/
package org.opensourcephysics.display;
import java.awt.AlphaComposite;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.net.URL;
import javax.swing.Icon;
import javax.swing.ImageIcon;
/**
* An <CODE>Icon</CODE> that can be resized.
*
* @author Doug Brown
*/
public class ResizableIcon implements Icon {
protected int baseWidth, baseHeight, w, h;
protected BufferedImage baseImage;
protected Icon icon;
/**
* Creates a <CODE>ResizableIcon</CODE> from the specified URL.
*
* @param location the URL for the image
*/
public ResizableIcon(URL location) {
this(new ImageIcon(location));
}
/**
* Creates a <CODE>ResizableIcon</CODE> from the specified Icon.
*
* @param the icon to resize
*/
public ResizableIcon(Icon icon) {
// prevent nesting resizable icons
while (icon instanceof ResizableIcon) {
icon = ((ResizableIcon)icon).icon;
}
this.icon = icon;
baseWidth = w = icon.getIconWidth();
baseHeight = h = icon.getIconHeight();
}
public synchronized void paintIcon(Component c, Graphics g, int x, int y) {
if (icon == null) {
return;
}
if (baseImage==null || baseImage.getWidth()!=baseWidth || baseImage.getHeight()!=baseHeight) {
baseImage = new BufferedImage(baseWidth, baseHeight, BufferedImage.TYPE_INT_ARGB);
}
Graphics2D g2 = baseImage.createGraphics();
g2.setComposite(AlphaComposite.Clear);
g2.fillRect(0, 0, baseWidth, baseHeight);
g2.setComposite(AlphaComposite.SrcOver);
icon.paintIcon(c, g2, 0, 0);
g.drawImage(baseImage, x, y, w, h, c);
}
public int getIconWidth() {
return w;
}
public int getIconHeight() {
return h;
}
/**
* Gets the base icon which is resized by this ResizableIcon.
*
* @return the base icon
*/
public Icon getBaseIcon() {
return icon;
}
/**
* Magnifies the icon by a specified integer factor.
*
* @param factor the factor
*/
public void resize(int factor) {
int n = Math.max(factor, 1);
w = n*baseWidth;
h = n*baseHeight;
}
}