JLabel images array

For comparison, I’ve re-factored @HFOE’s example so that Ground implements Icon and indexes the array returned by values(). As value is an implementation detail, int[][] MAP could instead be Ground[][] MAP. Update: This variation illustrates Ground[][] MAP and adds TexturePaint. import java.awt.Color; import java.awt.Component; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.GridLayout; import java.awt.TexturePaint; import java.awt.geom.Rectangle2D; import … Read more

Java in Eclipse: Where do I put files on the filesystem that I want to load using getResource? (e.g. images for an ImageIcon)

For Eclipse, typically all you need to do is set up a folder somewhere within your source code directory. For instance, if the directory containing your source is /src then you can create a /src/resources folder to place your images/files in. Then, within your class you do a getResource(“/resources/image.png”) to retrieve it. You can also … Read more

Java Swing: Displaying images from within a Jar

To create an ImageIcon from an image file within the same jars your code is loaded: new javax.swing.ImageIcon(getClass().getResource(“myimage.jpeg”)) Class.getResource returns a URL of a resource (or null!). ImageIcon has a constructors that load from a URL. To construct a URL for a resource in a jar not on your “classpath”, see the documentation for java.net.JarURLConnection.