Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Chessboard-Newbie Question

Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
    Bookmark Topic Watch Topic
  • New Topic