Results 1 to 7 of 7
  1. #1
    Cruncher is offline Member
    Join Date
    Mar 2010
    Posts
    88
    Rep Power
    0

    Default Swing components vs. custom painting

    Okay i'll try to explain an arguement i had with my programming teacher.

    For my assignments that have animation in them, he wants everything to be done with Swing components i.e JLabel.

    For example, some of you here have helped me with Tetris, kind of.

    Because my tetris used a paint method, but not double buffered, it flickered, this is when he made the arguement that i should be making all of the tetris pieces JLabels, or a class that extends it. That's why when i posted that one time, that's why i was using JLabels. Afterwards i was informed that i should keep my idea of making a class extend rectangle, and just paint them all inside of a label, but using double buffer techniques. It worked good, and i like that way.

    The problem is that my teacher still expects me to make everything on the screen a label, and i don't get why? his arguement was that swing is double buffered, my counter arguement was that double buffering isn't all that hard.

    when you're adding all sorts of components and labels to a content pane, it gets messy dealing with all the visible fields of these components, so i like my way better.

    his final arguement came down to, well this is newer, they had to have released it for a reason, so i'm going to trust Sun on this one.


    Anyway, i've been rambling a lot, what do you guys think is better to use?

  2. #2
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default

    Because my tetris used a paint method, but not double buffered, it flickered
    Thats probably because you implemented the painting incorrectly. Painting on Swing components is double buffered by default. You should NOT be overriding paint() when doing custom painting. You SHOULD be overriding paintComponent().

    his arguement was that swing is double buffered, my counter arguement was that double buffering isn't all that hard.
    Why reinvent the wheel if you don't have to?

    when you're adding all sorts of components and labels to a content pane, it gets messy dealing with all the visible fields of these components, so i like my way better.
    You still have to manage all the rectangles that you are painting. There location, color etc all have to be managed which means you need to implement some kind of class to maintain all this information. Again, JLabel already has properties that do this for you.

    You arguement is general in nature. The general answer is to use existing components so you don't have to reinvent the wheel. If for some reason existing classes don't satisfy your requirements then you look for alternatives, but you don't look for alternatives first. Without know all the specifics its hard to give specific advice.

  3. #3
    Cruncher is offline Member
    Join Date
    Mar 2010
    Posts
    88
    Rep Power
    0

    Default

    Yeah i did change it to use paintComponent later.

    my MAIN issue with using swing the way it does the layering system.
    when putting something on top of something else you have to make sure it was added in the right order from the beginning. if you add anything new it will be in the back, which is not always what i want, this means i have to have everything declared and initialized possibly long before it's ever used, and it's just confusing.

    I posted a topic a while ago, "how do i bring a swing component to the front of an applet" or something like that, which was for tetris, and they told me to go back to using custom painting, and a rectangle class.

  4. #4
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default

    I don't see what layering has to do with anything. All the shapes in tetris are painted on a flat grid, they are not painted on top of one another.

  5. #5
    Cruncher is offline Member
    Join Date
    Mar 2010
    Posts
    88
    Rep Power
    0

    Default

    well, i have a label filled with a color so you can see the game field?

    and he wants us to add things directly to the applet.
    so when you create new bricks, it will be behind the label.

    and Tetris is just an example, i'm talking generally, this isn't a question, it's a discussion.

  6. #6
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    5

    Default

    Quote Originally Posted by Cruncher View Post
    well, i have a label filled with a color so you can see the game field?

    and he wants us to add things directly to the applet.
    so when you create new bricks, it will be behind the label.

    and Tetris is just an example, i'm talking generally, this isn't a question, it's a discussion.
    How about using a JPanel instead of a JLabel, drawing the background image in a BufferedImage object that is displayed in the JPanel's paintComponent method, and draw the tetris objects on top of this background image, again in the paintComponent method?

  7. #7
    Cruncher is offline Member
    Join Date
    Mar 2010
    Posts
    88
    Rep Power
    0

    Default

    curmudgeon

    that is very similar to how my current version of it works, (except i used a JLabel, i try to keep things with what we've learned)

    My point is that's not how he wants it done, he wants all of the tetris peices to be separate labels.

Similar Threads

  1. Swing components not working
    By primalpop in forum AWT / Swing
    Replies: 30
    Last Post: 11-15-2009, 06:10 PM
  2. netbeans+swing = painting program?
    By yuriythebest in forum New To Java
    Replies: 3
    Last Post: 12-14-2008, 09:55 PM
  3. Tab order on swing components
    By ashvin@projectdemo.biz in forum AWT / Swing
    Replies: 1
    Last Post: 05-31-2008, 11:06 AM
  4. Where is it best to declare swing components?
    By MacNstuff in forum AWT / Swing
    Replies: 1
    Last Post: 02-06-2008, 01:59 AM
  5. Replies: 2
    Last Post: 07-22-2007, 01:01 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
  •