This discussion is archived
1 2 Previous Next 28 Replies Latest reply: Aug 10, 2012 5:38 AM by Patcha Go to original post RSS
  • 15. Re: AWT - How to delete (make transparent) a part of an Image
    morgalr Explorer
    Currently Being Moderated
    If you go to Swing you'll want to use a JPanel and stay with all Swing components.

    You have implemented off screen rendering with the BufferedImage, but are you doing double buffered? (using a back buffer)

    Swing will not give you a performance boost over your manual rendering configuration, but what I showed does what I said, and that is, gives you automated managment of the back buffer, off screen rendering, and the tightest paint loop available. In your program, if you don't already do it, you just need to implenent double buffering.

    I've also been known to use a "Fixed BufferedImage" and a "Render to BufferedImage" to make sure I'm not rendering during a repaint request--That just goes to using 2 BufferedImages and swapping the "Fixed", previous, with the freshly rendered on "Render to".
    Patcha wrote:
    I already do buffering.

    This is my "update()" method:
    @Override
    public void update(Graphics graph) {
         // Drawing everything from the buffer, and removing flicker effect
         graph.drawImage(buffer, buff_x, buff_y, null);
    }
    Where buffer is my BufferedImage where I draw all my stuffs inside my loop thread.

    But my loop is a ScheduledExecutorService At Fixed Rate, with a loop every around each 16 milliseconds.
    This is at the end of my Frame extending class' constructor:
    ScheduledExecutorService drawer = Executors.newScheduledThreadPool(1);
    drawer.scheduleAtFixedRate(new CoreThread(this, buffMan), 5, getCore_refresh(), TimeUnit.MILLISECONDS);
    CoreThread extends TimerTask (which implements Runnable), and getCore_refresh returns (int) 1000/60 (which is 16millis, around 65fps).

    I read somewhere that it's known repaint() makes partial draws with loop stuffs, due to a queue which can slow the paint and update call randomically.
    So partial draws makes flashing-like effects.
    I shouldn't need repaint, anyway, 'cause, for what I understood, it's more useful inside webpages with more than one applet running... is it right?


    I thought Swing was optimized for interfaces, while it was pratically the same for 2D graphic stuffs...

    Should I use a JFrame and write inside a Panel instead? It will really give me performance boost with 2D graphics?
  • 16. Re: AWT - How to delete (make transparent) a part of an Image
    morgalr Explorer
    Currently Being Moderated
    Patcha,
    I downloaded your game off of facebook and ran it. I see what you are saying with the "Flash". You need double buffering if you are not already doing it and you're probably running into a conflict with your threading model.
  • 17. Re: AWT - How to delete (make transparent) a part of an Image
    Patcha Newbie
    Currently Being Moderated
    Those flashing are resolved by not calling repaint, actually: it's an AWT repaint method issue...
    I think I don't need repaint's queue actually, so it didn't seems so bad to me to call directly paint(g) or update(g) methods.


    Well, I simply dunno what's back buffer / double buffer.
    I read somewhere it was enough to have a buffer (well... it didn't talk about more than one, where I read it). And so I did.

    I'm just programming this game with the main purpose to explore new java stuffs.
    (In my job I don't use 2D graphics, neither swing... it's much more web related, with jsp and struts.)


    How hard could be for me now to pass from pure AWT to swing?

    It's enough to extend JFrame instead than Frame and print my buffer inside JPanel's paintComponent method?

    I still need my own thread and scheduler or what?
  • 18. Re: AWT - How to delete (make transparent) a part of an Image
    gimbal2 Guru
    Currently Being Moderated
    Patcha wrote:
    I'm just programming this game with the main purpose to explore new java stuffs.
    I do that too but if I'd choose to use AWT/Java2D I'd stay as far away from the windowing toolkit as possible. Create a Frame, create a Canvas, create a BufferStrategy for double buffering and then do a manual old fashioned game loop. that's pretty much as close to the metal as you get.

    But nowadays I don't kid myself, its better to just use LibGDX and not be bothered by the problems of Java2D at all.
  • 19. Re: AWT - How to delete (make transparent) a part of an Image
    Patcha Newbie
    Currently Being Moderated
    Where's the fun on using a ready library, then? XD
    First I have to try it myself. But it's a nice stuff to explore after. :)

    gimbal2, are you suggesting me to not use swings instead? I'm not really sure I understood this.

    I read about BufferStrategy strategy somewhere (like it was an alterntive to BufferImage?), but didn't investigate more.
    The Canvas is like a Panel?
    Should I extend Canvas instead of Frame, or put the Canvas inside the Frame (like a Panel)?

    Heee so much new stuffs... I have to give a look all around...
  • 20. Re: AWT - How to delete (make transparent) a part of an Image
    gimbal2 Guru
    Currently Being Moderated
    Patcha wrote:
    Where's the fun on using a ready library, then? XD
    The fun is in creating the game, not "making Java2D work" ;)
    First I have to try it myself. But it's a nice stuff to explore after. :)
    Can't argue with that though!
  • 21. Re: AWT - How to delete (make transparent) a part of an Image
    Patcha Newbie
    Currently Being Moderated
    I read here -> http://www.cafeaulait.org/course/week6/28.html

    The utility of a Canvas is to have a rectangular area to draw in.
    I should extend it in a class, then override his paint() method.

    What's the real utility for?

    In my current configuration I override both my Frame's paint and update.
    I took it from somewhere over internet. Doing it and using a buffer was suggested to remove Flashing.
    (Unluckily all tutorial about 2D games are related to Applets... but I can face with that...)

    I just noticed now it's a bit odd, 'cause in my paint now I just call update, and in my update I write my buffer... while usually in the original Frame and Canvas methods it happens the contrary (is update calling paint).

    Anyway, if I do call super.paint() or super.update() my game starts flashing again!
    I think it's becacuse they use to draw a black square outside the buffer... so it flashes.

    What I mean is... do I really need a Canvas?
    Should I do inside a Canvas class what I currently do directly inside my Frame Class?
    Do I really need it?

    The sole odd stuff in my code as it is now, is that I have to "skip" myself window's borders... otherwise java seems to calculate it as part of the window, inside Windows. So it starts drawing under Windows bars...

    Would Canvas resolve this?
    Or a Canvas is just an "organization" matter?


    Tomorrw I'll check about BufferStrategy, too...
  • 22. Re: AWT - How to delete (make transparent) a part of an Image
    morgalr Explorer
    Currently Being Moderated
    Canvas will give you a display area that is true, you will not have to try to calculate the borders of the Frame.

    If you cover your entire display area with your Buffered Image, it's is not needed to call super. Double buffering will get rid of the flicker from a call to super, as it will not clear and then write to the active display, but a back buffer.

    I believe in AWT overriding paint and update are the appropriate thing to do.
  • 23. Re: AWT - How to delete (make transparent) a part of an Image
    DarrylBurke Guru Moderator
    Currently Being Moderated
    Patcha wrote:
    Uhm... maybe the right place for this topic should be this one -> Java 2D

    Could you please move it, if you agree with me, so I don't make double topic?
    Thank you!
    Moved.

    I didn't read every word posted, but isn't there a use case for AlphaComposite here?

    db
  • 24. Re: AWT - How to delete (make transparent) a part of an Image
    Patcha Newbie
    Currently Being Moderated
    @ Darryl Burke: no more. Infact we changed a bit the topic over time...

    @ morgalr: so if I add a Canvas, it will already be set in the right starting posiion to avoid borders?
    After I have to call the Canvas' (overriden) paint and make the buffer replacement there, instead than inside Frame's paint?
  • 25. Re: AWT - How to delete (make transparent) a part of an Image
    gimbal2 Guru
    Currently Being Moderated
    A Canvas is nothing more than "a place to draw stuff on", the Frame "something to put the Canvas in". Actual painting should be handled through a BufferStrategy so you get the double buffering bonus.

    I'd link to an article on this stuff but I can't actually find one that does not contain some sort of horribly wrong code :/
  • 26. Re: AWT - How to delete (make transparent) a part of an Image
    morgalr Explorer
    Currently Being Moderated
    Patcha wrote:
    @ morgalr: so if I add a Canvas, it will already be set in the right starting posiion to avoid borders?
    As one of the other posters said, the Canvas is a display area. You do your overrides thin it for doing your graphics instead of the Frame. Your display addressing will be relative to the Canvas and the Frame is left alone to be a container.
  • 27. Re: AWT - How to delete (make transparent) a part of an Image
    Patcha Newbie
    Currently Being Moderated
    I'll test Canvas and BufferStrategy as soon as I'll finish my Hall of Fame screen... so I'll have a clean code to work on. ;)
    Expecially the second seems interesting.
  • 28. Re: AWT - How to delete (make transparent) a part of an Image
    Patcha Newbie
    Currently Being Moderated
    Sorry for double posting, but I continued argument here: Pls helpme implement Canvas and BufferStrategy on my old "self made" buffer
    'cause we are off topic from starting post, in here.

    Anyway, returning closer to starting post argument, I found a way to create a full new empty image with this method:
    Image draw = frame.getGraphicsConfiguration().createCompatibleImage(width, height, Transparency.BITMASK);
    Graphics2D graph = (Graphics2D) draw.getGraphics();
    
    Composite composite = graph.getComposite();
    graph.setComposite(AlphaComposite.getInstance(AlphaComposite.CLEAR));
    graph.fillRect(0, 0, width, height);
    graph.setComposite(composite);
    I used it to build a transparent base to draw on my new "transparent background" menu buttons. ;)

    I hope this can be useful for somebody having troubles to make transparent images.
1 2 Previous Next

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points