Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
The moose likes Swing / AWT / SWT and the fly likes Chessboard-Newbie Question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Chessboard-Newbie Question" Watch "Chessboard-Newbie Question" New topic

Chessboard-Newbie Question

Russell Ray
Ranch Hand

Joined: Apr 25, 2005
Posts: 116
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:

I am at a lost why I can not see the images.

Thank you for any suggestions.

Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
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 ]
It is sorta covered in the JavaRanch Style Guide.
subject: Chessboard-Newbie Question
It's not a secret anymore!