How to use BuffereImage for a Chess Game
I am trying to create a chess game for java using images my classmate and I are working on. We are using a created image to serve as the bo开发者_JS百科ard and pieces to place about that the user can drag and place. Unfortunately we do not currently know how to import images from a file directory into the program itself. Not only that, but we are completely clueless as to why this would occur, as the Java API guide does not clearly explain this process.
The portion of the JDK API that provides image input is the package javax.imageio. The easiest way to load an image (into a BufferedImage object) is:
BufferedImage img = ImageIO.read( new File(pathname) );
Don't forget to catch the appropriate exceptions.
Read the section from the Swing tutorial on How to Use Icons for working examples of different ways to read images and display them on a label. Its easier to drag a label around the screen then just and image.
精彩评论