Results 1 to 10 of 10
  1. #1
    Jeremy8 is offline Member
    Join Date
    Feb 2009
    Posts
    12
    Rep Power
    0

    Default What is wrong with my ArrayList??

    *code removed*
    Last edited by Jeremy8; 11-15-2009 at 12:24 AM.

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    You have changed the signature of paintComponent() so that it no longer overrides that of JPanel. The method you have written will not be called as part of the painting process.

    Consider making the buildings array an instance variable so that the painting code has access to it. This makes sense because a reference to the collections is a fair part of what makes up the "state" of a CityPanel2 instance.

    And drop the instance variable building. This plays a role briefly while an instance is being created, but is no part of the "state" (=="the things which make an instance what it is").

  3. #3
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    To elaborate (I hope) buildings should be declared where you declared building, which would allow the
    paintComponent method to access the ArrayList. As well, building can be removed as
    buildings.add(new Building2(parameters)), should be sufficient. Finally, I find it easier to use a Canvas
    and the public void paint(Graphics g) method, as this is called immediately after the constructor.
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Quote Originally Posted by Singing Boyo View Post
    Finally, I find it easier to use a Canvas
    and the public void paint(Graphics g) method, as this is called immediately after the constructor.
    ??? Are we talking about AWT and applets? In that case paint() is used to render the applet within the browser whenever that's needed. That may include a time shortly after the applet's constructor has finished. Or it may not.

  5. #5
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    paint is also used by the java.awt.Canvas class, and a Canvas may be placed within a JPanel or JFrame. However, as the paint method must be overridden for anything to be painted, the class using the method must extend Canvas
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

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

    Default

    I hate to disagree boyo, but I think that it would be a very bad thing to place a Canvas object within a JPanel or a JFrame unless you have a very good reason to do so (I've never had to have done this), and you know exactly what and why you are doing this. Much much better is to place Swing components within Swing components and not to mix AWT and Swing components.

  7. #7
    Jeremy8 is offline Member
    Join Date
    Feb 2009
    Posts
    12
    Rep Power
    0

    Default

    Quote Originally Posted by pbrockway2 View Post
    You have changed the signature of paintComponent() so that it no longer overrides that of JPanel. The method you have written will not be called as part of the painting process.

    Consider making the buildings array an instance variable so that the painting code has access to it. This makes sense because a reference to the collections is a fair part of what makes up the "state" of a CityPanel2 instance.

    And drop the instance variable building. This plays a role briefly while an instance is being created, but is no part of the "state" (=="the things which make an instance what it is").
    Thanks! I should have realized that... now I feel dumb :p

  8. #8
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    You're welcome.

  9. #9
    conv_bay is offline Member
    Join Date
    Apr 2009
    Posts
    15
    Rep Power
    0

    Default

    waa...

    the expert user help u...

    for the expert user...
    plz help me in my topic...

    "COMBINATION WORDS" - in new java

  10. #10
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    sorry about the idea of mixing AWT and Swing... I have never found the Swing equivalent of a Canvas, but have always used Swing graphics with the exception of the Canvas, and it has never caused any major problems for me. However, Fubarable was probably right. If you have a good idea of how to use AWT graphics, that would most likely be a better idea.
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

Similar Threads

  1. what wrong
    By pro85 in forum New To Java
    Replies: 3
    Last Post: 02-09-2009, 02:07 PM
  2. What did i do wrong
    By jpnym15 in forum New To Java
    Replies: 8
    Last Post: 11-17-2008, 11:07 AM
  3. So, what am I doing wrong?
    By Charles_Smith in forum New To Java
    Replies: 0
    Last Post: 10-29-2008, 03:50 PM
  4. Can someone tell me what I did wrong??
    By booter4429 in forum New To Java
    Replies: 7
    Last Post: 08-13-2008, 09:35 PM
  5. Replies: 2
    Last Post: 04-21-2008, 12:43 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
  •