1 package nl.tudelft.simulation.dsol.swing.gui.util;
2
3 import java.io.IOException;
4
5 import javax.imageio.ImageIO;
6 import javax.swing.GrayFilter;
7 import javax.swing.Icon;
8 import javax.swing.ImageIcon;
9
10 /**
11 * Icons contains static methods to load a icon from disk.
12 * <p>
13 * Copyright (c) 2020-2024 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights reserved. See
14 * for project information <a href="https://simulation.tudelft.nl/dsol/manual/" target="_blank">DSOL Manual</a>. The DSOL
15 * project is distributed under a three-clause BSD-style license, which can be found at
16 * <a href="https://https://simulation.tudelft.nl/dsol/docs/latest/license.html" target="_blank">DSOL License</a>.
17 * </p>
18 * @author <a href="https://www.tudelft.nl/averbraeck">Alexander Verbraeck</a>
19 */
20 public final class Icons
21 {
22 /** Utility class. */
23 private Icons()
24 {
25 // Utility class.
26 }
27
28 /**
29 * Attempt to load and return an icon.
30 * @param iconPath String; the path that is used to load the icon
31 * @return Icon; or null if loading failed
32 */
33 public static final Icon loadIcon(final String iconPath)
34 {
35 try
36 {
37 return new ImageIcon(ImageIO.read(Resource.getResourceAsStream(iconPath)));
38 }
39 catch (NullPointerException | IOException npe)
40 {
41 System.err.println("Could not load icon from path " + iconPath);
42 return null;
43 }
44 }
45
46 /**
47 * Attempt to load and return an icon, which will be made gray-scale.
48 * @param iconPath String; the path that is used to load the icon
49 * @return Icon; or null if loading failed
50 */
51 public static final Icon loadGrayscaleIcon(final String iconPath)
52 {
53 try
54 {
55 return new ImageIcon(GrayFilter.createDisabledImage(ImageIO.read(Resource.getResourceAsStream(iconPath))));
56 }
57 catch (NullPointerException | IOException e)
58 {
59 System.err.println("Could not load icon from path " + iconPath);
60 return null;
61 }
62 }
63
64 }