Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: Graphics Object Won't Draw To The Correct JFrame

  1. #1
    Junior Member
    Join Date
    Oct 2012
    Posts
    5
    My Mood
    Sneaky
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default 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


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default 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)

  3. The Following User Says Thank You to copeg For This Useful Post:

    NickNumero (October 27th, 2012)

  4. #3
    Junior Member
    Join Date
    Oct 2012
    Posts
    5
    My Mood
    Sneaky
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Graphics Object Won't Draw To The Correct JFrame

    Here is the SSCCE part:


    Screen Shot 2012-10-27 at 9.11.56 AM.jpg


    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?

  5. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Graphics Object Won't Draw To The Correct JFrame

    Quote Originally Posted by NickNumero View Post
    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

  6. The Following User Says Thank You to copeg For This Useful Post:

    NickNumero (October 27th, 2012)

  7. #5
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Graphics Object Won't Draw To The Correct JFrame

    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
    Last edited by jps; October 27th, 2012 at 12:43 PM. Reason: reword for clarity

  8. #6
    Junior Member
    Join Date
    Oct 2012
    Posts
    5
    My Mood
    Sneaky
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default 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.

  9. #7
    Junior Member
    Join Date
    Oct 2012
    Posts
    5
    My Mood
    Sneaky
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Graphics Object Won't Draw To The Correct JFrame

    Oops didn't see jps' comment. That answers my other question. Thank you guys.

  10. #8
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default 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.

Similar Threads

  1. GUI program won't take 'char' input code, can't seem to find the correct one...
    By Eclecstatic in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 1st, 2012, 07:12 AM
  2. OpenGL Texture won't draw
    By nivangerow in forum Android Development
    Replies: 1
    Last Post: November 26th, 2011, 06:03 AM
  3. My JFrame won't repaint correctly please help me out!
    By ocolegrove in forum What's Wrong With My Code?
    Replies: 2
    Last Post: May 10th, 2011, 10:04 PM
  4. [SOLVED] Convert JFrame graphics into a jpeg file
    By Perd1t1on in forum AWT / Java Swing
    Replies: 4
    Last Post: June 22nd, 2010, 05:21 PM
  5. Won't Draw??
    By The_Mexican in forum Java Applets
    Replies: 4
    Last Post: March 13th, 2010, 06:00 PM

Tags for this Thread