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 5 of 5

Thread: Problem with JPanel

  1. #1
    Junior Member
    Join Date
    Mar 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Problem with JPanel

    hi all. i am a new user in this site and newbie in java world. i have a problem with my swing application. thats why i am here to find out the problem with you. i have a JPanel on which i draw an image. to do that at first i take the Graphics object of the JPanel and then draw image on this graphics object.

    my code is like belo

    Graphics g = jPanel.getGraphics();
    g.drawImage(parameters);

    the code works fine. but if i resize the JPanel or minimize the window, all drawn image get vanished. please someone tell me how to solve this issue. thank you.


  2. #2
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 287 Times in 225 Posts
    Blog Entries
    4

    Default Re: Problem with JPanel

    Hello fahien,

    Welcome to the Java Programming Forums.

    Please post all of your code for us to look at.. Do you want your JPanel to be resizable?
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

  3. #3
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,612
    My Mood
    Hungover
    Thanks
    145
    Thanked 658 Times in 563 Posts

    Default Re: Problem with JPanel

    Quote Originally Posted by fahien View Post
    hi all. i am a new user in this site and newbie in java world. i have a problem with my swing application. thats why i am here to find out the problem with you. i have a JPanel on which i draw an image. to do that at first i take the Graphics object of the JPanel and then draw image on this graphics object.

    my code is like belo

    Graphics g = jPanel.getGraphics();
    g.drawImage(parameters);

    the code works fine. but if i resize the JPanel or minimize the window, all drawn image get vanished. please someone tell me how to solve this issue. thank you.
    Sorry, but that is absolutely NOT the way to do it. If you're calling getGraphics() on a Component, you're doing something very very wrong.

    Instead, extend JPanel (or JComponent) and override its paintComponent() method. Read this: Painting in AWT and Swing
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  4. #4
    Junior Member
    Join Date
    Mar 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problem with JPanel

    ok.. what i am doing is : i have a JTextField and a JPanel. when i write a character in TextField i want to draw the same character in JPanel. please tell me an efficient way to do that as you guys think my previous way inefficient.

  5. #5
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,612
    My Mood
    Hungover
    Thanks
    145
    Thanked 658 Times in 563 Posts

    Default Re: Problem with JPanel

    Efficiency has nothing to do with it. Calling getGraphics() is just plain wrong. Did you read the link I gave you?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. display pdf file into jpanel problem
    By Jhovarie in forum What's Wrong With My Code?
    Replies: 0
    Last Post: February 12th, 2011, 03:29 PM
  2. [SOLVED] Extends JPanel painting problem
    By Philip in forum What's Wrong With My Code?
    Replies: 3
    Last Post: July 28th, 2010, 04:16 PM
  3. problem with drawing images on JPanel
    By Asido in forum What's Wrong With My Code?
    Replies: 13
    Last Post: July 19th, 2010, 04:07 PM
  4. How to copy image from one jpanel to another jpanel
    By ramanavarayuri1986 in forum AWT / Java Swing
    Replies: 0
    Last Post: February 15th, 2010, 02:36 AM
  5. Creating and displaying a JPanel inside another JPanel
    By JayDuck in forum AWT / Java Swing
    Replies: 1
    Last Post: April 7th, 2009, 09:02 AM