making a chess board gui

hi, I am new to gui with java and i am having trouble making a GUI that represents a chess board. I can get a grid just fine, but when it comes to the event handler, I am having trouble. I want it to work so that when you click on one square, the contents are stored in the event handler, and then when you click on another square, the content moves to that square. currently, all that happens is that when i double click a white square, it turns green! here is all my source code for this project

Re: making a chess board gui

ok, I made the changes. Here are the new bugs that I cant seem to fix. The first one is, even though I have 2 for loops each iterating 4 times, only 12 squares show up!. The second weird bug, is I can get the text to move around ok, but it wont dissappear from the white squares, only from the green. Code is the same for both so I dont see why this is happening. Also, I tried to make SquarePanel one class with a constructor of SquarePanel(Color c), but the squares ended up having no color. I passed it parameters of Color.black and Color.white, but thats really a minor problem

Re: making a chess board gui

Think through your logic in your nested for loop as it's wrong, and in particular your if (j != 0) bit since you end up trying to add the same JPanel twice to the grid, and that will never work. Instead you have to add a new JPanel to the grid each time. Work it out first on paper as if you were drawing white and black squares on paper starting with a 2 by 2 grid, and see what happens.

Re: making a chess board gui

Another problem with your graphics is that usually when you override a graphics method such as paintComponent, you'll want to call the super's method somewhere in this code, often as the first call in the method, so that house-keeping graphics can be done, such as erasing old images to make way for the new. So for instance, you'll probably want to change every paintComponent method to something like so:

Re: making a chess board gui

Re: making a chess board gui

ok, Ive condensed the code very much, however, not all the panels are displaying on top of each other. it was working fine earlier, and im not sure what exactly i changed to cause it to do this. sorry if this is a simple question, but i am new to gui programming, and tired because ive been working on this all day. I got the logic mostly done for the chess portion of the code, now its just a matter of linking the two.

Re: making a chess board gui

Originally Posted by yemista

ok, Ive condensed the code very much, however, not all the panels are displaying on top of each other. it was working fine earlier, and im not sure what exactly i changed to cause it to do this. sorry if this is a simple question, but i am new to gui programming, and tired because ive been working on this all day.

You state "sorry if this is a simple question" but neglect to ask a question, so there's limited help we can give. If you don't know where the problem is then debug your code with println statements to find out. Please come back if you've found your problem, can describe it, and have an answerable question to ask.

Re: making a chess board gui

Sorry about that. I dont know how to debug gui code with println statements because in order to do that you have to understand how the components interact with each other. Currently, the problem is that instead of displaying the panels in a grid pattern, they are all stacked in the upper left corner. Depending on where in the frame i click, that determines which one is on top of the stack. I have attached an image of how it looks. Its supposed to look like a grid of 64 green squares each with their coordinates printed on them

Your getX() and getY() methods are overriding similar methods of JPanel and this is messing up the placement of your JPanels as Swing thinks that they should be placed where getX() and getY() tells them to place it.

Solution: rename those methods so they don't mess up the super methods. Perhaps you could call them