Results 1 to 7 of 7
Like Tree2Likes
  • 2 Post By Tolls

Thread: Which component to draw on?

  1. #1
    Join Date
    Aug 2011
    Posts
    62
    Rep Power
    0

    Default Which component to draw on?

    Hello forum,
    I have read some tutorials on game programming and Java 2D, but I am still confused about the architecture of such programs. Some people draw on Canvas and add that to a JFrame, some draw directly on a JFrame and others on a JPanel inside a JFrame. Which option would you recommend and where are the advantages/disadvantages of every single approach? Thanks.

  2. #2
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,429
    Rep Power
    7

    Default Re: Which component to draw on?

    Hi,
    Canvas and JFrame doesn't work. Never mix lightweight and heavyweight components, i.e. Swing and AWT. I'd go for JPanels in JFrames. Read this tutorial: Lesson: Performing Custom Painting (The Java™ Tutorials > Creating a GUI With JFC/Swing)
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  3. #3
    Join Date
    Aug 2011
    Posts
    62
    Rep Power
    0

    Default Re: Which component to draw on?

    Thank you. Should the JFrame be extended in the game class (which is often done) or better used as a property?

  4. #4
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,429
    Rep Power
    7

    Default Re: Which component to draw on?

    As a property. You're not modifying it's behaviour, you're only using it.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: Which component to draw on?

    It is almost never necessary to extend JFrame.
    A JFrame is simply the top level container of a Swing app, all the bits of the GUI ending up in its base JPanel.
    Extending JFrame to add things to it would by like extending ArrayList simply to populate it with values.
    DarrylBurke and doWhile like this.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  6. #6
    Join Date
    Aug 2011
    Posts
    62
    Rep Power
    0

    Default Re: Which component to draw on?

    Thanks to all of you. It's somehow annoying that so many bad code examples can be found on the internet. Apart from the Oracle docs, which Java coding websites are really reliable?

  7. #7
    Join Date
    Aug 2011
    Posts
    62
    Rep Power
    0

    Default Re: Which component to draw on?

    But what about the BufferStrategy class? Isn't that a good reason to use Frame + Canvas for games?

Similar Threads

  1. Replies: 1
    Last Post: 11-05-2012, 08:09 PM
  2. Calling methods of a component from a different component
    By Mate de Vita in forum AWT / Swing
    Replies: 0
    Last Post: 10-26-2012, 09:15 PM
  3. why cant i draw this box???
    By stefandanielsen in forum New To Java
    Replies: 2
    Last Post: 05-12-2011, 03:53 PM
  4. how to draw an arc
    By Baker in forum New To Java
    Replies: 1
    Last Post: 04-16-2009, 10:05 PM
  5. help me draw... please...
    By kureikougaiji in forum New To Java
    Replies: 1
    Last Post: 01-28-2008, 01:22 PM

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
  •