Download this file

IconManager.java    142 lines (133 with data), 4.9 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
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package cologne.eck.all_peas.gui;
import java.awt.Image;
import java.awt.Toolkit;
import java.io.File;
import javax.swing.ImageIcon;
import cologne.eck.tools.UnexpectedValueException;
public class IconManager {
/**
* Load an ImageIcon for javax swing components
* from an image file. The image file can be inside
* the jar archive or in the file system, but must
* be in the directory resources.
*
* @param imageFileName the file name of the image file
* in the directory resources
* ( png, gif,jpg, bmp)
* @param description the description of the icon
* @param accessibleName name of the icon, e.g. for icon-oly
* buttons
* @param accessibleDescription the description of the icon,
* describing the purpose of the object
*
* @return the ImageIcon loaded from the file
*/
public static ImageIcon loadIcon(String imageFileName, String description,
String accessibleName, String accessibleDescription) {
ImageIcon icon = null;
// first try to load from jar archive
try {
icon = new ImageIcon(
IconManager.class.getClassLoader().getResource("resources/"+ imageFileName));//,
// description);
} catch (Exception e) {
// try to load from file system:
icon = new ImageIcon("resources" + File.separator + imageFileName);
// check success:
if (icon.getImageLoadStatus() == 4){// MediaTracker.ERRORED
icon = new ImageIcon(Toolkit.getDefaultToolkit().createImage("resources/" + imageFileName));
// check success:
if (icon.getImageLoadStatus() == 4){// MediaTracker.ERRORED
// try to load from jar
java.net.URL imgURL = IconManager.class.getResource("/resources/"+ imageFileName);
if (imgURL != null) {
icon = new ImageIcon(imgURL);
} else {
icon = null;
}
}
}
}
if (icon == null) {
new UnexpectedValueException("icon", "ImageIcon", "is null").printDescription();
return null;
} else if (icon.getImageLoadStatus() == 4){// MediaTracker.ERRORED
new UnexpectedValueException("icon", "ImageIcon", "is not loaded").printDescription();
return null;
} else {
icon.setDescription(description);
}
if (accessibleName != null) {
icon.getAccessibleContext().setAccessibleDescription(accessibleName);
}
if (accessibleDescription != null) {
icon.getAccessibleContext().setAccessibleDescription(accessibleDescription);
}
return icon;
}
/**
* Load a scaled ImageIcon for javax swing components
* from an image file. The image file can be inside
* the jar archive or in the file system, but must
* be in the directory resources.
*
* @param imageFileName the file name of the image file
* in the directory resources
* ( png, gif,jpg, bmp)
* @param description the description of the icon
* @param width the intended width if the icon
* @param height the intended height of the icon
* @param accessibleName name of the icon, e.g. for icon-oly
* buttons
* @param accessibleDescription the description of the icon,
* describing the purpose of the object
*
* @return the ImageIcon loaded from the file
*/
public static ImageIcon loadScaledIcon(String imageFileName, String description,
int width, int height,
String accessibleName, String accessibleDescription) {
ImageIcon icon = null;
// first try to load from jar archive
try {
icon = new ImageIcon(
IconManager.class.getClassLoader().getResource("resources/"+ imageFileName));//,
// description);
} catch (Exception e) {
// try to load from file system:
icon = new ImageIcon("resources" + File.separator + imageFileName);
// check success:
if (icon.getImageLoadStatus() == 4){// MediaTracker.ERRORED
icon = new ImageIcon(Toolkit.getDefaultToolkit().createImage("resources/" + imageFileName));
// check success:
if (icon.getImageLoadStatus() == 4){// MediaTracker.ERRORED
// try to load from jar
java.net.URL imgURL = IconManager.class.getResource("/resources/"+ imageFileName);
if (imgURL != null) {
icon = new ImageIcon(imgURL);
} else {
icon = null;
}
}
}
}
if (icon == null) {
new UnexpectedValueException("icon", "ImageIcon", "is null").printDescription();
return null;
} else if (icon.getImageLoadStatus() == 4){// MediaTracker.ERRORED
new UnexpectedValueException("icon", "ImageIcon", "is not loaded").printDescription();
return null;
} else {
icon.setDescription(description);
}
Image image = icon.getImage().getScaledInstance(width,height,Image.SCALE_DEFAULT);
icon = new ImageIcon(image);
if (accessibleName != null) {
icon.getAccessibleContext().setAccessibleDescription(accessibleName);
}
if (accessibleDescription != null) {
icon.getAccessibleContext().setAccessibleDescription(accessibleDescription);
}
return icon;
}
}