Results 1 to 19 of 19
Like Tree2Likes
  • 2 Post By Junky

Thread: How To Draw Objects Not On A JPanel

  1. #1
    anthropamorphic's Avatar
    anthropamorphic is offline Senior Member
    Join Date
    Jun 2011
    Posts
    135
    Rep Power
    0

    Default How To Draw Objects Not On A JPanel

    Hello, I was wondering how do I draw objects on a JFrame and not a JPanel, because whenever I search google it always comes up with ways to draw it on a JPanel.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Why would you want to draw directly on the JFrame and not on the JPanel? If we knew your rationale for going against standard practice, perhaps we could help you better.

    Which part of the JFrame do you plan on drawing on? The content pane? root pane? glass pane? layered pane? Do you know the reasons why the google search is giving you this answer, why it is in general a very bad idea to draw directly on the JFrame?

  3. #3
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    Why on earth do you want to paint directly on the frame?

  4. #4
    anthropamorphic's Avatar
    anthropamorphic is offline Senior Member
    Join Date
    Jun 2011
    Posts
    135
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    Why would you want to draw directly on the JFrame and not on the JPanel? If we knew your rationale for going against standard practice, perhaps we could help you better.

    Which part of the JFrame do you plan on drawing on? The content pane? root pane? glass pane? layered pane? Do you know the reasons why the google search is giving you this answer, why it is in general a very bad idea to draw directly on the JFrame?
    The reason I would want to draw directly on to the JFrame is I wanted to make a Paint program just for fun and whenever I tried using a JPanel it would always just be a dot on the screen.

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by anthropamorphic View Post
    The reason I would want to draw directly on to the JFrame is I wanted to make a Paint program just for fun and whenever I tried using a JPanel it would always just be a dot on the screen.
    Then you're doing something wrong, but the solution is not to do something even more wrong such as drawing directly on the JFrame as this won't fix your current problem and will add a heap of other new problems. No the solution is to continue drawing on the JPanel and to figure out why your program has the error and fix it.

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    Quote Originally Posted by anthropamorphic View Post
    whenever I tried using a JPanel it would always just be a dot on the screen.
    Ahh, so you prefer seeing a dot on the JFrame instead?
    Fubarable and DarrylBurke like this.

  7. #7
    anthropamorphic's Avatar
    anthropamorphic is offline Senior Member
    Join Date
    Jun 2011
    Posts
    135
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    Then you're doing something wrong, but the solution is not to do something even more wrong such as drawing directly on the JFrame as this won't fix your current problem and will add a heap of other new problems. No the solution is to continue drawing on the JPanel and to figure out why your program has the error and fix it.
    I don't use JPanels much so is there a way to resize them? I tried just doing
    pan.setSize(300, 450);
    (pan is the panel variable I used)
    and it didn't change the size

  8. #8
    anthropamorphic's Avatar
    anthropamorphic is offline Senior Member
    Join Date
    Jun 2011
    Posts
    135
    Rep Power
    0

    Default

    Quote Originally Posted by Junky View Post
    Ahh, so you prefer seeing a dot on the JFrame instead?
    no I meant the JPanel was just a dot

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by anthropamorphic View Post
    I don't use JPanels much so is there a way to resize them? I tried just doing
    pan.setSize(300, 450);
    (pan is the panel variable I used)
    and it didn't change the size
    This is likely due to your needing to understand which layout manager(s) is/are being used and how to best add the JPanel to its container so that it will be best visualized. Most layout managers don't listen to the JPanel's size but often do respect its preferredSize. You may wish to try setting the JPanel's preferredSize via setPreferredSize(new Dimension(....)), and then revalidating and repainting the container that you're adding the JPanel to.

    I strongly urge you to create a small compilable runnable program that we can run, test, modify ourselves, that requires no outside dependencies such as images on disk or database, in short, an SSCCE, and then post the code here and we can help you with it.

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Junky View Post
    Ahh, so you prefer seeing a dot on the JFrame instead?
    Cute, real cute.

  11. #11
    anthropamorphic's Avatar
    anthropamorphic is offline Senior Member
    Join Date
    Jun 2011
    Posts
    135
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    This is likely due to your needing to understand which layout manager(s) is/are being used and how to best add the JPanel to its container so that it will be best visualized. Most layout managers don't listen to the JPanel's size but often do respect its preferredSize. You may wish to try setting the JPanel's preferredSize via setPreferredSize(new Dimension(....)), and then revalidating and repainting the container that you're adding the JPanel to.

    I strongly urge you to create a small compilable runnable program that we can run, test, modify ourselves, that requires no outside dependencies such as images on disk or database, in short, an SSCCE, and then post the code here and we can help you with it.
    Thanks for the help the preferred size worked. As for creating a small compilable program I don't really have a need to do that as of right now, so thanks again for your help.

  12. #12
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Cool. Glad you've got it working, and I wish all advice were so easy to give.

  13. #13
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    Quote Originally Posted by anthropamorphic View Post
    As for creating a small compilable program I don't really have a need to do that as of right now, so thanks again for your help.
    That request was for our benefit not yours. You really have a lot to learn about how to get help. In a previous thread I requested you post your error messages and your response was "Nah! You don't need to see them". If you do not provide us will adequate and accurate information then there is not a snowballs chance in hell of anyone being able to help you.

  14. #14
    anthropamorphic's Avatar
    anthropamorphic is offline Senior Member
    Join Date
    Jun 2011
    Posts
    135
    Rep Power
    0

    Default

    Quote Originally Posted by Junky View Post
    That request was for our benefit not yours. You really have a lot to learn about how to get help. In a previous thread I requested you post your error messages and your response was "Nah! You don't need to see them". If you do not provide us will adequate and accurate information then there is not a snowballs chance in hell of anyone being able to help you.
    I did end up posting the errors the only reason I was hesitant to post it is I didn't have a way to access the errors at the moment. Anyway how would it be to your benefit posting this?

  15. #15
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    It benefits me and everyone else if you improved the quality and quantity of information in your posts. The benefit is that it doesn't take 20 posts of back and forth of extracting information from you before we get to the real problem. If you post all the information in your first post then it will be a lot easier for us to diagnose the problem and provide a solution. The follow on effect is that you get a response sooner so you can fix your problem.

  16. #16
    anthropamorphic's Avatar
    anthropamorphic is offline Senior Member
    Join Date
    Jun 2011
    Posts
    135
    Rep Power
    0

    Default

    Quote Originally Posted by Junky View Post
    It benefits me and everyone else if you improved the quality and quantity of information in your posts. The benefit is that it doesn't take 20 posts of back and forth of extracting information from you before we get to the real problem. If you post all the information in your first post then it will be a lot easier for us to diagnose the problem and provide a solution. The follow on effect is that you get a response sooner so you can fix your problem.
    Normally people don't like reading through a huge amount of code, but if you really want me to post my code next time I will do so.

  17. #17
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    Quote Originally Posted by anthropamorphic View Post
    Normally people don't like reading through a huge amount of code.
    That is why Fubar requested a SSCCE. Did you follow the link to see what that means?

  18. #18
    anthropamorphic's Avatar
    anthropamorphic is offline Senior Member
    Join Date
    Jun 2011
    Posts
    135
    Rep Power
    0

    Default

    Quote Originally Posted by Junky View Post
    That is why Fubar requested a SSCCE. Did you follow the link to see what that means?
    yah, but he had already help me with my problem

  19. #19
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

Similar Threads

  1. JPanel draw graphic on key event
    By Rolf83 in forum AWT / Swing
    Replies: 22
    Last Post: 06-04-2011, 12:26 PM
  2. Draw on JPanel?
    By PhQ in forum New To Java
    Replies: 2
    Last Post: 07-16-2010, 11:19 PM
  3. Replies: 2
    Last Post: 03-22-2009, 01:53 PM
  4. how to draw x-y graph in Jpanel.--not in APPLET.
    By vincent2001@gmail.com in forum New To Java
    Replies: 2
    Last Post: 08-24-2008, 05:01 AM
  5. Draw on JPanel, Help
    By carl in forum Java 2D
    Replies: 1
    Last Post: 07-31-2007, 06:56 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •