Java-Gaming.org Hi !
Featured games (85)
games approved by the League of Dukes
Games in Showcase (612)
Games in Android Showcase (172)
games submitted by our members
Games in WIP (658)
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 3171 times)
0 Members and 1 Guest are viewing this topic.
Offline gerard_pp

Junior Devvie


Medals: 1



« Posted 2012-04-16 18: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: 389
Projects: 3
Exp: 5 years


I'm the King!


« Reply #1 - Posted 2012-04-16 23:52:49 »

What exactly are you trying to do?

Offline gerard_pp

Junior Devvie


Medals: 1



« Reply #2 - Posted 2012-04-17 10: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!
Legends of Yore - The Casual Retro Roguelike
Offline 65K
« Reply #3 - Posted 2012-04-17 11: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: 389
Projects: 3
Exp: 5 years


I'm the King!


« Reply #4 - Posted 2012-04-17 11: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 Devvie


Medals: 1



« Reply #5 - Posted 2012-04-17 13: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 13: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 - hybrid visual IDE for creative coding
Digital Prisoners - interactive spaces and projections
Offline gerard_pp

Junior Devvie


Medals: 1



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

no...    Grin

Let me try it.

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

JGO Kernel


Medals: 389
Projects: 3
Exp: 5 years


I'm the King!


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

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

Offline Riven
« League of Dukes »

« JGO Overlord »


Medals: 1039
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #9 - Posted 2012-04-17 17: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!
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline nsigma
« Reply #10 - Posted 2012-04-17 18: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 - hybrid visual IDE for creative coding
Digital Prisoners - interactive spaces and projections
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 
Roquen (15 views)
2015-08-29 11:30:54

GamerC4 (25 views)
2015-08-22 20:38:50

GamerC4 (23 views)
2015-08-22 20:37:18

GamerC4 (27 views)
2015-08-22 20:37:01

Kefwar (31 views)
2015-08-22 18:07:24

GamerC4 (24 views)
2015-08-22 01:00:24

GamerC4 (37 views)
2015-08-22 01:00:17

GamerC4 (24 views)
2015-08-22 00:57:35

GamerC4 (25 views)
2015-08-22 00:56:59

BurntPizza (31 views)
2015-08-21 01:38:01
HotSpot Options
by Roquen
2015-08-29 11:33:11

Rendering resources
by Roquen
2015-08-17 12:42:29

Rendering resources
by Roquen
2015-08-17 09:36:56

Rendering resources
by Roquen
2015-08-13 07:40:51

Networking Resources
by Roquen
2015-08-13 07:40:43

List of Learning Resources
by gouessej
2015-07-09 11:29:36

How Do I Expand My Game?
by bashfrog
2015-06-14 11:34:43

List of Learning Resources
by PocketCrafter7
2015-05-31 05:37:30
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
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!