Ok, so I have a chess program that I am writing, and I can't for the life of me figure out what is wrong with my resize code. Any help is greatly appreciated!
EDIT: See 2 comments below for a short compilable example.
Problem: Every time I resize the window smaller, certain pieces disappear, however this problem only arises when I resize the window fast. If I gradually resize it smaller, the pieces don't disappear!?!? I have attached images of the problem.
Code Structure: The resize code is spread over 3 classes. The MainGui, the ChessBoard, and the ChessPiece class. The gamePanelComponentResize method resides in the MainGui class, and is a listener for the resize component event. This method then delegates the resize order to the ChessBoard object, which in turn delegates it to each of the remaining ChessPiece objects.
Heres a simple compilable example of the problem. I apologize for the length of ChessBoard.java. Most of the code in that class is initialization code and can be ignored. The resizeBoard, setSize, and paintComponent methods are the only ones linked to the problem (that I'm aware of) in that class.
Thank you for taking the time to help!
MainGui.java
ChessBoard.java
ChessPiece.java
ChessSquare.java
board.png
Image file for the chess board.
wpawn.png
Image file for a pawn. We will pretend each piece is a pawn for the example.
Any idea's guys? I'm thinking it might be thread related, however if I spawn a new thread each time the resize event is called, won't it be flooded with duplicate threads, all doing the same thing but with different size variables? Any thoughts?
Thanks guys!
Tyler Eastman wrote:Any idea's guys? I'm thinking it might be thread related, however if I spawn a new thread each time the resize event is called, won't it be flooded with duplicate threads, all doing the same thing but with different size variables? Any thoughts?
Thanks guys!
I've been looking at this, and I'm not sure, but I don't think that it's thread related, but much of the problem is that I can't see where the problem is since it is how Swing is calling your paint methods. The error occurs when the view is shrunk quickly, and when this happens, Swing doesn't paint all the components in the container (at least I'm not seeing the output from a println I have in a paintComponent displaying from all the components when this happens). As an aside, you know that you can and should simplify your code greatly by using arrays here.
Thanks for your fast response Pete! I will condense the piece initialization and resize code down using arrays once I'm at my home computer tomorrow, I can see how not using them adds unnecessary bulk to the example.
I think I'd simply use a GridLayout, there shouldn't be painting problems at all using the 'standard' layout managers. Animations could always be drawn 'on top' (e.g. on a glasspane).
Censorship is the younger of two shameful sisters, the older one bears the name inquisition. -- Johann Nepomuk Nestroy
Thank you so much Peter, your example seems to have solved the problem completely! It will require me to restructure my animation code a little bit, but after pulling my hair out for 2 weeks on the resize code, it will be a welcome change of pace!
Thank you everyone for your help, it is very appreciated!
I just had the craziest dream. This tiny ad was in it.