Results 1 to 13 of 13
  1. #1
    kaizen9001 is offline Member
    Join Date
    Mar 2011
    Posts
    13
    Rep Power
    0

    Default Passing a variable problem.

    Hi guys. I'm having a bit of bother (once again) with passing a variable between 2 classes.
    In my first class(called imageFrame), I have the following line of code:
    Java Code:
    myImagePanel.displayPicture(defaultLoadImage);
    the method that this refers to in the second class(called ImagePanel) is as follows:
    Java Code:
         public void displayPicture(Image  displayImage) {
            gBuffer.drawImage(displayImage, 0, 0, null);
            paintComponent(this.getGraphics());
        }
    The idea is that, I'd like "defaultLoadImage" to be passed into "displayImage", but I can't work out how to go about doing it. I know this is going to be a really simple answer, so sorry for the stupid question, but any help would really be appreciated.

    Thanks.

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,457
    Rep Power
    20

    Default

    Never use getGraphics of a Component in client code. Learn how to correctly perform custom painting:
    Lesson: Performing Custom Painting (The Java™ Tutorials > Creating a GUI With JFC/Swing)

    db

  3. #3
    kaizen9001 is offline Member
    Join Date
    Mar 2011
    Posts
    13
    Rep Power
    0

    Default

    The code above is from a lab as part of a my coursework so I'm afraid it has to be used in order to appease my lecturers. Regardless of how useless or inefficient it is.

  4. #4
    dlorde is offline Senior Member
    Join Date
    Jun 2008
    Posts
    339
    Rep Power
    7

    Default

    Quote Originally Posted by kaizen9001 View Post
    The code above is from a lab as part of a my coursework so I'm afraid it has to be used in order to appease my lecturers. Regardless of how useless or inefficient it is.
    :eek:

    Perhaps you could point out that in reading up about it, you've discovered that the lab code is, er, misguided, and provide the reference to the tutorial that explains why...

  5. #5
    kaizen9001 is offline Member
    Join Date
    Mar 2011
    Posts
    13
    Rep Power
    0

    Default

    Agreed, It's completely stupid. The idea is that we're being taught to work with other peoples code etc. Any ideas what I have to do to pass this variable across? I'm sure it's just a variable declaration somewhere, but I have no idea where it goes or how to go about writing it...

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,457
    Rep Power
    20

    Default

    To call that code misguided is like calling murder a misdemeanor. Using getGraphics() of a Component in client code is 99.999% of the time just plain wrong.

    The remaining 0.001% applies to sophisticated programs that perform necessary checks on the returned Graphics value to decide whether to use it for rendering. Certainly not coursework.

    db

  7. #7
    kaizen9001 is offline Member
    Join Date
    Mar 2011
    Posts
    13
    Rep Power
    0

    Default

    ... Ok, I understand that it's not correct... Regardless, it's still what I have to do. Can anyone please help me pass this variable?

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,784
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by kaizen9001 View Post
    ... Ok, I understand that it's not correct... Regardless, it's still what I have to do. Can anyone please help me pass this variable?
    Most likely not because even if all our code is correct that getGraphics() call is likely to return null and your program will throw a NullPointerException.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  9. #9
    kaizen9001 is offline Member
    Join Date
    Mar 2011
    Posts
    13
    Rep Power
    0

    Default

    Ok doke. Thanks for your time guys.

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

    Default

    Phew !

  11. #11
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,784
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Fubarable View Post
    Phew !
    And remember: it was me who got you all off the hook ;-)

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

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

    Default

    Quote Originally Posted by JosAH View Post
    And remember: it was me who got you all off the hook ;-)
    Heck, I wasn't even an active participant but was getting frustrated just reading this thread!

    Kind regards,

    not Jos

  13. #13
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,457
    Rep Power
    20

    Default

    It's even more exasperating that this crappy code has evidently been around for at least four years.
    Java Programming: NullPointerException doing my head in - thatsjava.com

    Their one member who responded was even worse than the questioners.

    db

Similar Threads

  1. Passing Variable In Frame
    By amitabh in forum JDBC
    Replies: 2
    Last Post: 06-07-2010, 05:36 PM
  2. passing variable in trees??
    By player123 in forum Advanced Java
    Replies: 2
    Last Post: 02-03-2009, 01:42 PM
  3. Passing Environment Variable in Eclipse Europa
    By ajaymenon.k in forum Eclipse
    Replies: 0
    Last Post: 11-19-2007, 11:08 AM
  4. Passing variable information between classes
    By zen_to_go in forum New To Java
    Replies: 1
    Last Post: 10-30-2007, 09:09 PM
  5. Variable passing, Error: IOException
    By fernando in forum New To Java
    Replies: 3
    Last Post: 07-31-2007, 03:03 PM

Posting Permissions

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