Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (579)
games submitted by our members
Games in WIP (500)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: 1 [2]
  ignore  |  Print  
  ARGB Image, set all visible pixels to white  (Read 5079 times)
0 Members and 1 Guest are viewing this topic.
Offline Abuse

JGO Coder


Medals: 10


falling into the abyss of reality


« Reply #30 - Posted 2010-10-21 16:01:10 »

VolatileImages losing their contents is part of the API; your code has to account for it. (though I wish the API had been designed differently so this wasn't necessary...)
Have a read of the volatile image tutorial.

As for your specific problem:

  • don't recreate the scratch VolatileImage - create one large enough for your largest sprite & keep hold of it.
  • make sure all your drawing operations are being accelerated by using the java2d logging/tracing options

If you want the flash itself to be alpha'ed, then you should use the DST_ATOP rule instead.
It'll maintain your source's transparency channel but composite the colour channels of source & destination.

Here's a little test app. I knocked up the last time this thread was touched. Forgive the lack of documentation & crude means of animating, it was hacked together Wink
Oh, and don't use this as an example of how to do the compositing in a performant manner - this is purely a visualization tool for the compositing rules.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.MultipleGradientPaint.CycleMethod;
import java.awt.RadialGradientPaint;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;

import javax.swing.JFrame;
import javax.swing.JPanel;


public class CompositeTest extends JPanel {
   

   
   /**
    *
    */

   private static final long serialVersionUID = 1L;


   public enum AlphaCompositeRules {
      CLEAR(AlphaComposite.CLEAR),
      SRC(AlphaComposite.SRC),
      DST(AlphaComposite.DST),
      SRC_OVER(AlphaComposite.SRC_OVER),
      DST_OVER(AlphaComposite.DST_OVER),
      SRC_IN(AlphaComposite.SRC_IN),
      DST_IN(AlphaComposite.DST_IN),
      SRC_OUT(AlphaComposite.SRC_OUT),
      DST_OUT(AlphaComposite.DST_OUT),
      SRC_ATOP(AlphaComposite.SRC_ATOP),
      DST_ATOP(AlphaComposite.DST_ATOP),
      XOR(AlphaComposite.XOR);
     
      private int rule;
      private AlphaCompositeRules(int rule) {
         this.rule = rule;
      }
     
      public AlphaComposite getInstance() {
         return getInstance(1.0f);
      }
     
      public AlphaComposite getInstance(float alpha) {
         return AlphaComposite.getInstance(rule, alpha);
      }
   };

   private static final int IMG_SIZE = 64;
   
   private BufferedImage sprite = new BufferedImage(IMG_SIZE ,IMG_SIZE , BufferedImage.TYPE_INT_ARGB);
   private BufferedImage scratch = new BufferedImage(IMG_SIZE, IMG_SIZE, BufferedImage.TYPE_INT_ARGB);
   
   
   public CompositeTest() {
      setBackground(Color.black);
      Graphics2D g2d = sprite.createGraphics();
     
      g2d.setPaint(new RadialGradientPaint(new Rectangle2D.Float(0,0,IMG_SIZE,IMG_SIZE), new float[]{0.0f,1.0f},new Color[]{Color.WHITE, Color.BLUE}, CycleMethod.NO_CYCLE));
      g2d.setClip(new Ellipse2D.Float(0,0,IMG_SIZE,IMG_SIZE));
      g2d.fillRect(0, 0, IMG_SIZE, IMG_SIZE);
      g2d.dispose();
   }
   
   public Dimension getPreferredSize() {
      return new Dimension(IMG_SIZE *2, IMG_SIZE *(AlphaCompositeRules.values().length+1));
   }

   private void clear(BufferedImage img, Color c) {
      Graphics2D g = img.createGraphics();
      g.setColor(c);
      g.setComposite(AlphaComposite.Src);
      g.fillRect(0,0,img.getWidth(), img.getHeight());
      g.dispose();
   }
   
   private int alpha = 0;
   private int alphaDelta = 1;
   
   public void paint(Graphics g) {
      super.paint(g);
      alpha+=alphaDelta;
      if(alpha==255 || alpha==0) {
         alphaDelta=-alphaDelta;
      }
     
      clear(scratch, new Color(255,0,0,alpha));
      g.drawImage(scratch, 0, 0, null);
      g.drawImage(sprite, IMG_SIZE, 0, null);
      g.setColor(Color.white);
      g.drawString("\u03B1=" + alpha, 0, IMG_SIZE/2);
     
     
      for(int i = 0;i < AlphaCompositeRules.values().length;i++) {
         clear(scratch, new Color(255,0,0,alpha));
         Graphics2D scratchG = scratch.createGraphics();
         
         scratchG.setComposite(AlphaCompositeRules.values()[i].getInstance());
         scratchG.drawImage(sprite, 0, 0, null);
         scratchG.dispose();
         
         g.drawImage(scratch, IMG_SIZE, (i+1)*IMG_SIZE,null);
         g.setColor(Color.white);
         g.drawString(AlphaCompositeRules.values()[i].toString(), 0, (i+1)*IMG_SIZE+IMG_SIZE/2);
      }
     
      repaint();
     
   }
   
   
   public static void main(String[]args) {
      JFrame frame = new JFrame();
      frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
      frame.getContentPane().add(new CompositeTest());
      frame.pack();
      frame.setVisible(true);
   }
}


Make Elite IV:Dangerous happen! Pledge your backing at KICKSTARTER here! https://dl.dropbox.com/u/54785909/EliteIVsmaller.png
Offline Cero
« Reply #31 - Posted 2010-10-22 16:45:59 »

thanks for the test app, its really good to look at, and compare.

I have read the http://gpwiki.org/index.php/Java:Tutorials:VolatileImage before.
Problem is: using the DX pipeline, the mentioned glitch happens, and when using a validation loop, as you should if you use volatileimage, it will result in a infinite loop
It also only happens if you have more than one screen connected and enabled in windows
I have actually found this Bug in the java bug database, so yeah its kinda strange.

Offline JL235

JGO Coder


Medals: 10



« Reply #32 - Posted 2010-11-05 22:36:13 »

I built a similar effect years ago and just used BufferedImages. I turned the images to white on the fly and stored them permanently.

The bit you might be interested in is that in order to get around the memory issues of having hundreds/thousands of images in memory was to store them behind a WeakReference object within a map. When I went to draw I'd grab the image out, if it's null I'd recreate it and store it in the map, then draw using the image. This way the JVM could garbage collect them (if it needed to) and the major JVMs are encouraged to garbage collect the least used weak references first.

In an ideal world this means you'll only ever be storing the whited (or redded) images for the enemies who are currently on screen.

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Cero
« Reply #33 - Posted 2010-11-09 01:36:34 »

Quite the attractive concept. Gonna look into that.

Offline JL235

JGO Coder


Medals: 10



« Reply #34 - Posted 2010-11-09 10:28:20 »

I believe you can also use the other types of references (SoftReference and PhantomReference) if you wanted to try to optimize it further. I can't remember the exact differences, but I believe the JVM will prioritise certain types of references over others when looking to reclaim memory.

So if you find a situation where certain generated images are used waaaaaay more then others then you can cache those behind a different type of reference to reflect this.

Pages: 1 [2]
  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.

xsi3rr4x (40 views)
2014-04-15 18:08:23

BurntPizza (36 views)
2014-04-15 03:46:01

UprightPath (52 views)
2014-04-14 17:39:50

UprightPath (34 views)
2014-04-14 17:35:47

Porlus (51 views)
2014-04-14 15:48:38

tom_mai78101 (73 views)
2014-04-10 04:04:31

BurntPizza (132 views)
2014-04-08 23:06:04

tom_mai78101 (232 views)
2014-04-05 13:34:39

trollwarrior1 (195 views)
2014-04-04 12:06:45

CJLetsGame (203 views)
2014-04-01 02:16:10
List of Learning Resources
by SHC
2014-04-18 03:17:39

List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22: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!