Question

I'm making a little chess game and it has the classes whitePawnPiece that extends whitePiece that extends chessMain that extends chessGUI.

chessGUI is the JFrame, and whitePawnPiece is where I want to do whitePawnIcon.paintIcon (...,...,xPos, yPos).

Well, first I don't understand very well the first 2 parameters, but from what I read the first can be null and the second should be the JFrame.

My question is: how do I use this second parameter? I'm using NetBeans and it accepts the first parameter as null, but the second, whatever I put, it gives an error "Cannot find symbol" or something.

PS: I was doing the GUI using NetBeans IDE but after reading the code I thought it was easier and better to program the GUI myself, so this is my first try at programming the GUI itself :)

And a bonus question: When I was using the NetBeans IDE I had some problems in overlaying images until I set the layout to Absolute (I was using labels to work with the icons). Will I have the same problem now or not? Should I use the default layout or there is a better one for a chess program that has lots of overlay (chessboard, pieces, selection/possible moves markers)?

Was it helpful?

Solution

This is a little confusing, but...

Icon declares paintIcon as taking four parameters...

  • Component
  • Graphics
  • int x
  • int y

The is a reference to some component, presumably the one from which the Graphics context is associated with, as it's likely the the Component will be used as an ImageObsever to monitor changes to the icon when it needs to be updated and may provide other useful information

The Graphics context is actual place (the graphics output) where the image is to be painted.

Now, having said all that, in order to paint the icon successfully, you will need some kind of reference of some Graphics context. This can be obtained by extending a component (like JPanel), overriding it's paintComponent method. This will give you access to a Graphics context which can used to paint stuff in this paint cycle. Never maintain a reference to a Graphics context you did not create yourself.

Then you would call paintIcon passing a reference to the component and the Graphics context.

Take a look at Performing Custom Painting, Painting in AWT and Swing and 2D Graphics

Having said all that, I think you will the use of BufferedImages (or Image) simpler then Icon. Take a look at Reading/Loading an Image for more details

As to your second question. No, you won't have the same issues, you'll have entirly brand new ones

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top