This discussion is archived
1 2 Previous Next 28 Replies Latest reply: Aug 10, 2012 5:38 AM by Patcha RSS

AWT - How to delete (make transparent) a part of an Image

Patcha Newbie
Currently Being Moderated
Hi all,
there is way to delete a section of an Image?

I have a certain amount of elements which have to be moved all together on a coloured background (which have to stay fixed instead).

So I thought about creating a transparent BufferedImage, draw all elements only once, then move them all together each time it's needed.

The sole problems is each element (on different positions) have to be deleted if it goes over a certain coordinate.
So to make it really work, I need to delete them from the BufferedImage when necessary (or make their aread transparent again).

There is a way to make it work?
I tried to search on google... but it's hard. Or maybe I don't use right keys...
  • 1. Re: AWT - How to delete (make transparent) a part of an Image
    Patcha Newbie
    Currently Being Moderated
    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!
  • 2. Re: AWT - How to delete (make transparent) a part of an Image
    morgalr Explorer
    Currently Being Moderated
    To change the transparency of a section of an image you have to make sure you are using an ARGB image and change the alpha value to the appropriate value for the amount of transparnecy you desire.
  • 3. Re: AWT - How to delete (make transparent) a part of an Image
    Patcha Newbie
    Currently Being Moderated
    Do you mean to change alpha value, then draw/fill on the section I want transparent?
    Does it actually do something, or simply draw nothing 'cause drawing transparent stuff??
  • 4. Re: AWT - How to delete (make transparent) a part of an Image
    morgalr Explorer
    Currently Being Moderated
    When you change the Alpha value in an ARGB Image you make it transparent. When you paint that over an existing image, no change in the original image is displayed, but if you change the Alapha to a translucent value, you can see the overlay affecting the existing image.

    BTW: when you draw on the ARGB Image, you need to draw with colors that have active Alpha values--in other words, if you draw on your ARGB with a standard "RED" it will show RED, but if you draw with a custom color RED with an Alpha set to some transparency level, you will see the overlay of the color onto the underlaying background when the ARGB Image is painted.
  • 5. Re: AWT - How to delete (make transparent) a part of an Image
    Patcha Newbie
    Currently Being Moderated
    I still didn't try... but shouldn't it just mix translucent draw with existing image background?

    I'm also suspecting that it's much more "light" to draw a certain amount of small png (uhm... 60 per 1kb?), than make around 5 draws with big alpha images.
  • 6. Re: AWT - How to delete (make transparent) a part of an Image
    morgalr Explorer
    Currently Being Moderated
    Obviously it's going to be quicker to just draw the portion you want to utilize, you can easily do so, and I assumed you would, but then there was that part of you title that says "make transparent" and that does not mean delete. Since you put it in parenthesis, I assumed you were using that as the definition of what you meant by "delete".

    At this point, I would have to say: please define what you want--deleted (actually removed) or transparent (still present, but not visible).
  • 7. Re: AWT - How to delete (make transparent) a part of an Image
    Patcha Newbie
    Currently Being Moderated
    Ok, let's take this example:

    I have a full transparent image 800 x 600, then I randomically draw here and there 60 small images (pratically 1 x 6).

    So at the end I have the original transparent 800 x 600 with 60 elements fixed on it.

    Now from this ending image I wanna delete one or more of those 60 elements.

    I can't make them just repainted with a different color, 'cause I don't have a backgorund color, I have other images under it... so I guess it have to be transparent...
    I don't think you can actually make holed Images.... do you?


    EDIT:
    My current solution is to redraw all 60 small images each time.

    I was just guessing how to make it ligheter... so I thought drawing all elements on another transparent image to move them all together, could save something on load.

    Edited by: Patcha on 31-lug-2012 22.45
  • 8. Re: AWT - How to delete (make transparent) a part of an Image
    morgalr Explorer
    Currently Being Moderated
    I'm having trouble visualizing what and why you are doing this from your explanation. I am thinking of something like a checker board and you are painting each tile. What you are going to do is reveal what is underneath of those tiles from time to time.

    To show what is underneath, you can easily just paint the corresponding portion of the background onto the image--no transparency, deleting, or magicry neaded. To cover it up, then just paint the appropriate tile in the location.

    This basically boils down to, you have a matrix--position (X, Y) and each of those tiles are (w, h) dimension. So you draw the tile at (X*w, Y*h) or in the case of the reveal, you take location (X*w, Y*h) and copy a tile w by h on your image at (X*w, Y*h). Simple as that--no transparency, no deletion, no repaint it all, no magic--just a little bit of math.

    In your case, you just store the location where you put the "random" image, then paint the background over it. Please note, that you don't need to paint onto the background image--you paint to the GraphicsContext of the JPanel you are using for display. That way you don't ever corrupt your background image.
  • 9. Re: AWT - How to delete (make transparent) a part of an Image
    Patcha Newbie
    Currently Being Moderated
    You almost guessed it! Except in my case all elements move together too, until some reach a certain coordinate and need to be removed.

    I have no problem to share what I'm doing, but I think you need a facebook account to see the link:
    http://www.facebook.com/notes/adriano-patcha-serio/piove-my-java-game-beta-100/10151096077566405
    But if you don't think you need my game-notes (or you can't reach that link), this simpler one should work in any case: http://www.patcha.it/Stuffs/Piove.zip


    Praticallly I have those 60 drops going down on screen, and they have to be deleted when they reach the bottom (and redrawed on top).
    That's why I thought about a transparent image to draw them all: so at the end I could move them all together with a single draw, just by moving the single transparent image where I put them.
    Or something similar.

    The idea you had should actually work anyway.
    I could cover the drops which reached the bottom by taking the piace of background image on that location and redraw it on "dropped drops".


    But I'm still doubtfull if this can actually make my system lighter or not.

    I think I do too much "drawImage" in that game... and it still flashes sometimes (expecially on high refresh rate setting).
    I really need a way to make drawing lighter or do less drawImage...
  • 10. Re: AWT - How to delete (make transparent) a part of an Image
    Patcha Newbie
    Currently Being Moderated
    I found the "flashing" problem source...
    Actually none of my drawImage is too slow... the problem is made by "repaint()" frame''s method.
    It's seems to work in a separate thread, and sometimes it is too slow and calls update(g) too late, when I'm already modifying the buffer with next thread run.

    For now I resolved by calling update(g) directly at the end of the thread run, and so jump repaint()... there is a more clean way to jump "repaint()" stuffs?
  • 11. Re: AWT - How to delete (make transparent) a part of an Image
    morgalr Explorer
    Currently Being Moderated
    Change to Swing, it's double buffered by default and will give you smoother graphics. I've had 1000's of animated objects in a JPanel without the "Flash" and have not had to go to manual update cycles. The best thing to do to stop the flashing is to use a BufferedImage as your "canvas", and then just do a drawImage in your paintComponent method. Use a javax.swing.Timer for your Timer and render your current state to your BufferedImage on the Timer Event, then call refresh. 60 objects should never give you a "Flash", not even with an old slow computer. I've done 1000's of animated objects with a single core AMD 1.8GHz machine set at 1600x1200 screen resolution with no "Flash".

    Your paintComponent(Graphics g) should look something like this:
    public void paintComponent(Graphics g){
      super.paintComponent(g);  //only include this line if your BufferedImage does not cover your object's entire display
      g.drawImage(bi, 0, 0, this);  //where bi is your BufferedImage
    }
    What this will buy you is: double buffering (with automatic management of a back image), tightest possible painting, no blocking in your threads--and no "Flash".
  • 12. Re: AWT - How to delete (make transparent) a part of an Image
    gimbal2 Guru
    Currently Being Moderated
    Its highly machine specific though; even if you use Swing, its still Java2D under there. And Java2D has the least dependable performance metrics I have ever seen.

    When you start to introduce transparency things can really break down on certain machines, especially those with integrated graphics cards.
  • 13. Re: AWT - How to delete (make transparent) a part of an Image
    morgalr Explorer
    Currently Being Moderated
    Yeppers, That's why there's no mention of transparency in that recommendation, and use of Swing for automatic double buffering management and the BufferedImage for rendering to produce the very tightest paintComponent possible. Literally all you're doing each paint cycle is painting your already rendered BufferedImage to the back buffer.
    gimbal2 wrote:
    Its highly machine specific though; even if you use Swing, its still Java2D under there. And Java2D has the least dependable performance metrics I have ever seen.

    When you start to introduce transparency things can really break down on certain machines, especially those with integrated graphics cards.
  • 14. Re: AWT - How to delete (make transparent) a part of an Image
    Patcha Newbie
    Currently Being Moderated
    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 core = Executors.newScheduledThreadPool(1);
    core.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?
1 2 Previous Next

Legend

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