Java-Gaming.org
Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
Featured games (78)
games approved by the League of Dukes
Games in Showcase (406)
games submitted by our members
Games in WIP (293)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Repainting a transparent bufferimage  (Read 1268 times)
0 Members and 1 Guest are viewing this topic.
Offline gerard_pp

Junior Member


Medals: 1



« Posted 2012-04-16 20:53:54 »

Hello JGO

im triying to diaplay a grid on a transparent bufferimage, but i can't find a viable way to repaint it to the screen without clearing the previous transparent bufferimage effectively. here is what i have:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
//Create a blank transparent buffered image:
BufferedImage grid = new BufferedImage(800, 600, BufferedImage.TRANSLUCENT);
Graphics2D pintor = grid.createGraphics();

//transfer a single prototipe tile on to  my bufferedimage
prototipo.paint(pintor);

//painting it
public void printMapa(Graphics2D g){
   Rectangle2D.Double rect = new Rectangle2D.Double(0, 0, pantancho, pantalto);
   pintor.setComposite(AlphaComposite.getInstance(AlphaComposite.CLEAR, 0.0f));
   pintor.fill(rect);
        g.drawImage(grid, 0, 0, padre);
        pintor = grid.createGraphics();
}


Any ideas...?

Warning: Plz, excuse my bad english...
Offline ra4king

JGO Kernel


Medals: 264
Projects: 2


I'm the King!


« Reply #1 - Posted 2012-04-17 01:52:49 »

What exactly are you trying to do?

Offline gerard_pp

Junior Member


Medals: 1



« Reply #2 - Posted 2012-04-17 12:53:56 »

Firstly, thanks for your concern.

I have a blank bufferedimage (transparent) on the stage, where i am painting directly using a graphics2D object "pintor".

I need to clear that image to it's original state, to repaint the next frame of animation, not leaving a trace of what was there previuosly.

In Java creating a new BufferedImage for each frame of an animation is slow, but reusing images with transparency from frame to frame is tricky as BufferedImage doesn't have an obvious way to set all of it's pixels to completely transparent.

Unlike other colors we can't just paint a transparent color over the image because, well, it's transparent and won't affect the pixels already in the image.

How can i clear or create a bufferedimage to hold new graphics assets in my repaint method?

Warning: Plz, excuse my bad english...
Games published by our own members! Check 'em out!
Try the Free Demo of Titan Attacks
Offline 65K
« Reply #3 - Posted 2012-04-17 13:29:01 »

Use AlphaComposite.CLEAR like you do now or just use AlphaComposite.SRC to paint your new image stuff - if I understand your issue correct.

Offline ra4king

JGO Kernel


Medals: 264
Projects: 2


I'm the King!


« Reply #4 - Posted 2012-04-17 13:30:14 »

Graphics.fillRect or clearRect?

1  
2  
3  
Graphics g = myImage.getGraphics();
g.setColor(Color.white);
g.fillRect(0,0,myImage.getWidth(),myImage.getHeight());

Offline gerard_pp

Junior Member


Medals: 1



« Reply #5 - Posted 2012-04-17 15:24:47 »

Use AlphaComposite.CLEAR like you do now or just use AlphaComposite.SRC to paint your new image stuff - if I understand your issue correct.

Hello 65k

This way i can clear nicely my transparent bufferimage, but im having a problem. I cannot redraw anything on my image. It keeps blank. Any ideas?

Warning: Plz, excuse my bad english...
Offline nsigma
« Reply #6 - Posted 2012-04-17 15:42:21 »

This way i can clear nicely my transparent bufferimage, but im having a problem. I cannot redraw anything on my image. It keeps blank. Any ideas?

Have you set the composite back to AlphaComposite.SrcOver?

Praxis LIVE - Open-source graphical environment for developing intermedia performance tools, projections and interactive spaces.
Praxis LIVE on Twitter
Offline gerard_pp

Junior Member


Medals: 1



« Reply #7 - Posted 2012-04-17 16:14:38 »

no...    Grin

Let me try it.

Warning: Plz, excuse my bad english...
Offline ra4king

JGO Kernel


Medals: 264
Projects: 2


I'm the King!


« Reply #8 - Posted 2012-04-17 19:48:41 »

A simple fillRect is all that is needed, no need for AlphaComposite.

Offline Riven
« League of Dukes »

JGO Overlord


Medals: 438
Projects: 4


Hand over your head.


« Reply #9 - Posted 2012-04-17 19:54:43 »

This is the solution:
1  
2  
3  
4  
5  
6  
7  
   public static void makeTransparent(BufferedImage img)
   {
      Graphics2D g = img.createGraphics();
      g.setComposite(AlphaComposite.getInstance(AlphaComposite.CLEAR, 0.0f));
      g.fillRect(0, 0, img.getWidth(), img.getHeight());
      g.dispose();
   }

Note how it keeps the composite local to the temporary Graphics2D object, as not to 'influence' the state of the Graphics instance you're using to render after you cleared the image.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Projects: Revenge of the Titans, Titan Attacks, Droid Assault, and Ultratron
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline nsigma
« Reply #10 - Posted 2012-04-17 20:02:28 »

This is the solution:
1  
2  
3  
4  
5  
6  
7  
   public static void makeTransparent(BufferedImage img)
   {
      Graphics2D g = img.createGraphics();
      g.setComposite(AlphaComposite.getInstance(AlphaComposite.CLEAR, 0.0f));
      g.fillRect(0, 0, img.getWidth(), img.getHeight());
      g.dispose();
   }

Note how it keeps the composite local to the temporary Graphics2D object, as not to 'influence' the state of the Graphics instance you're using to render after you cleared the image.

Dang, I was just halfway through correcting @ra4king when you posted!  Wink

To be a pedant, I think you could just use -

1  
g.setComposite(AlphaComposite.Clear);


The alpha value should be irrelevant from memory, and it saves allocating a composite - them nanoseconds can build up!  Grin

Praxis LIVE - Open-source graphical environment for developing intermedia performance tools, projections and interactive spaces.
Praxis LIVE on Twitter
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
 
Get high quality music tracks for your game!

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

The invasion has landed! On Mars! And you're there to beat 'em!
cubemaster21 (76 views)
2013-05-17 21:29:12

alaslipknot (87 views)
2013-05-16 21:24:48

gouessej (117 views)
2013-05-16 00:53:38

gouessej (112 views)
2013-05-16 00:17:58

theagentd (123 views)
2013-05-15 15:01:13

theagentd (112 views)
2013-05-15 15:00:54

StreetDoggy (156 views)
2013-05-14 15:56:26

kutucuk (178 views)
2013-05-12 17:10:36

kutucuk (177 views)
2013-05-12 15:36:09

UnluckyDevil (185 views)
2013-05-12 05:09:57
Complex number cookbook
by Roquen
2013-04-24 12:47:31

2D Dynamic Lighting
by Oskuro
2013-04-17 16:46:12

2D Dynamic Lighting
by Oskuro
2013-04-17 16:45:57

2D Dynamic Lighting
by Oskuro
2013-04-17 16:23:20

Noise (bandpassed white)
by Roquen
2013-04-05 17:36:01

Noise (bandpassed white)
by Roquen
2013-04-03 16:17:38

Java Data structures
by Roquen
2013-03-29 13:21:12

Topic Request
by kutucuk
2013-03-22 21:42:01
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!
Page created in 0.114 seconds with 21 queries.