This week's book giveaway is in the OCAJP forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide 1Z0-808 and have Jeanne Boyarsky & Scott Selikoff on-line! See this thread for details.
I am very new to using GUI components. I am learnig under fire. Please excuse me if this is a easy problem for most of you.
I pulled this code from another forum and tested it before I changed anything. I all worked properly before I made and changes. The only thing I changed is I am using images inside the panel instead of setting the background color for each cell (inside the for loop).
I am simply trying to get the chessboard to display the image I have created. The only thing I can see when the application runs is the Frame with a grey background. I know the components are being created because I counted them after the chessboard is created.
I am using JDK 1.3 and the images are in the correct directory. I double checked this by using:
As Michael said your code seems to run okay. So I am assuming that you are having trouble with loading the images. Some general remarks: the old way to load images was with one of the 'getImage' methods from Toolkit used in conjunction with a MediaTracker. The MediaTracker api has an example that shows how to do this. The newer way with ImageIcon came along in j2se 1.2. It is convenient to use a single line of code to load an image and make an ImageIcon but one difficulty is that you get no feedback if the jvm can not find/load the image. So I made up a load method that will give some feedback. You can use this to figure out the path to your images. In your code you use a path that will work if the images are in the same folder as your class file, aka the current or "." (dot) folder. The tutorial has a page How to Use Icons that discusses things like image loading, directory structure, file paths, and the (very handy) Class method 'getResource'. To simplify putting everything together in your app you might consider loading your images first, making your 'board' JPanel with background colors next and then adding the ImageIcons to the board.
edited for text width — cw [ August 15, 2005: Message edited by: Craig Wood ]