Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (538)
Games in Android Showcase (132)
games submitted by our members
Games in WIP (601)
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 2607 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: 356
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: 356
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 - open-source intermedia toolkit and live interactive visual editor
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: 356
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.

Online Riven
« League of Dukes »

« JGO Overlord »


Medals: 840
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
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 - open-source intermedia toolkit and live interactive visual editor
Digital Prisoners - interactive spaces and projections
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

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

The first screenshot will be displayed as a thumbnail.

rwatson462 (30 views)
2014-12-15 09:26:44

Mr.CodeIt (20 views)
2014-12-14 19:50:38

BurntPizza (42 views)
2014-12-09 22:41:13

BurntPizza (76 views)
2014-12-08 04:46:31

JscottyBieshaar (37 views)
2014-12-05 12:39:02

SHC (50 views)
2014-12-03 16:27:13

CopyableCougar4 (47 views)
2014-11-29 21:32:03

toopeicgaming1999 (114 views)
2014-11-26 15:22:04

toopeicgaming1999 (102 views)
2014-11-26 15:20:36

toopeicgaming1999 (30 views)
2014-11-26 15:20:08
Resources for WIP games
by kpars
2014-12-18 10:26:14

Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50
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!