Graphics Object Won't Draw To The Correct JFrame
Hi this is my first post in a forum, sorry if I don't have enough information. I'm currently programming a chess engine and have a JFrame displaying the chess board. The board is displayed just fine as well as all the pieces. I'm having a problem when I bring up another JFrame. I do this when a pawn makes it all the way across the board, I want a new JFrame to pop up and give the user the option to choose a new piece(queen, rook, bishop, or horse) to replace their pawn. On the new JFrame, the user will see four images of the pieces, once a piece is clicked the JFrame will close and the game will resume.
Right now the new JFrame appears, but the BufferedImages I call to be drawn on it are displayed in the original JFrame. I am not sure how to specify which JFrame for the Graphics object to write to. I know how to load/display BufferedImages just fine, except I can't seem to select the right JFrame to draw to.
The code below draws a line of text and a horse piece on top of my chess board and not the new JFrame I created. It is executed when a pawn enters the back row of the board.
JFrame piece_select = new JFrame();
piece_select.setVisible(true);
piece_select.setSize(300, 300);
JPanel pieces = new JPanel();
pieces.setVisible(true);
piece_select.add(pieces);
pieces.setBackground(Color.GRAY);
g.drawString("Pick a piece below to replace your pawn.", 20, 20); //Drawn on the board, not piece_select.
g.drawImage(horse, 0, 0, null); //Same thing for this line of code.
The size of the new JFrame is correct as well as the background color I set, however the BufferedImage(horse) is drawn on the original frame and the string. I realize that the Graphics object is in no way associated with the JFrame and JPanel with this code, I just can't figure out how to make it so it is. Any ideas?
Thank you,
Nick
Re: Graphics Object Won't Draw To The Correct JFrame
I recommend posting an SSCCE that clearly shows the problem you are having (if you don't know what that is, please see the link in my signature)
1 Attachment(s)
Re: Graphics Object Won't Draw To The Correct JFrame
Here is the SSCCE part:
Attachment 1496
As you can see the new JFrame appears with the correct background and size, but the horse image and text appear on the board. If anyone thinks they can solve the problem with me shortening the code and pasting it here, I will do so. Does this image help?
Re: Graphics Object Won't Draw To The Correct JFrame
Quote:
Originally Posted by
NickNumero
Does this image help?
Sorry, but not really. I'm talking about code...reduce your code down to the smallest piece that can reproduce the problem - make sure we can compile and execute the code, then post the code. Often this process alone can result in you discovering the solution, if not, posting this makes it vastly easier for everyone to help you
Re: Graphics Object Won't Draw To The Correct JFrame
Quote:
g.drawString("Pick a piece below to replace your pawn.", 20, 20); //Drawn on the board, not piece_select.
g.drawImage(horse, 0, 0, null); //Same thing for this line of code.
g used on these two lines is the graphics object for the game board, not the "pop-up" window. To draw on the "pop-up" window you will need the graphics object of the "pop-up" window. Override the paintComponent method of the pieces JPanel in this window, and the correct graphics object will be passed to that method when "pieces" needs to be drawn
Re: Graphics Object Won't Draw To The Correct JFrame
Your advice helped me solve it. I was simplifying the code to post here and it came to me. I have to load the images into JLabels and then add the JLabels to the JPanel, which in turn is added to the new JFrame. By calling Graphics functions, I was drawing to the this.JPanel object and not the new frame's JPanel. Although it is not needed with my program, I am not sure how I would call graphics functions to draw on the new JFrame.
Re: Graphics Object Won't Draw To The Correct JFrame
Oops didn't see jps' comment. That answers my other question. Thank you guys.
Re: Graphics Object Won't Draw To The Correct JFrame
Glad you got it figured out. Please mark the thread as solved if you have no more comments on this topic. Assistance found here.