Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (109)
games submitted by our members
Games in WIP (536)
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  
  Help making my sprite transparent  (Read 2937 times)
0 Members and 1 Guest are viewing this topic.
Offline luisoft

JGO Coder


Projects: 6


Java games rock!


« Posted 2011-02-22 22:57:18 »

Hi, I don't know what I'm doing wrong. Can someone help me?

I have the following sprites compressed and respective colors
1  
2  
3  
      final int color[] = { -16776960, -10461715, -137067, -237988, -196613,
            -12981001, -13839010, -14832074, -2794921, -32641, };
      final String str = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACCCCCCCAAAAAAAACCCCCCCCAAAAAACCCCCCCCCCCAAAAAAADEEEDEEEDAAAAAADEEBEEEBEEDAAAAADDEEEDEEEDDAAABBDDDDBBBDBBBBABBBDDDDBBBDBBBBBBBBDDDDDDDDBDBABBBBDDDDDADDBDBABBBDDDDDDDDDDDCAAABDDDDDDDDDDDCAAAAADDDDDDDDDCCCAAAAADDDDDDDDCCCAAAAADDDAADDDCCCAAAADDDDADDDDACAAEEAAACCCCCCCAEEAEEEACCCCCCCCAEEEEECCCCCCCCCCCEEAEEEEDEEEDEEEDEEEEEADEEBEEEBEEDEAEEEDDEEEDEEEDDEEEBBDDDDBBBDBBBBABBBDDDDBBBDBBBBBBBBDDDDDDDDBDBEBBBBDDDDDADDBDBEBBBDDDDDDDDDDDCEAEBDDDDDDDD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AAACCCCCAAAAAAAAIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIJJJJIIJJJJIIIIIIJJJJIIJJJJIIIIIIJJJJIIJJJJIIIIIIJJJJIIJJJJIIIIIIJJJJIIJJJJIIIIAAIIIIIIIIIIIIIIAAIIIIIIIIIIIIIIIIJJJJIIJJJJIIIIIIJJJJIIJJJJIIIIIIJJJJIIJJJJIIIIIIJJJJIIJJJJIIIIIIJJJJIIJJJJIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII";


I want to make the first color (-16776960 - which is represented by letter A) transparent, so to decompress my sprite I'm using the following:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
      for (int i = 0; i < sprites.length; i++) {
         sprites[i] = new BufferedImage(TILE_SIZE / 2, TILE_SIZE / 2, 2);
         Graphics2D g = sprites[i].createGraphics();

         for (int y = 0; y < 16; y++) {
            for (int x = 0; x < 16; x++) {
               if (str.charAt((y * 16 + x) + (i * 16 * 16)) != 'A') {
                  g.setColor(new Color(color[str.charAt((y * 16 + x)
                        + (i * 16 * 16)) - 65]));
               } else {
                  g.setColor(new Color(0xff000000 | color[str
                        .charAt((y * 16 + x) + (i * 16 * 16)) - 65]));
               }
               g.fillRect(x, y, TILE_SIZE / 2, TILE_SIZE / 2);

            }
         }


When color is A I'm doing an "or" with 0xff000000 but it's not working, and my sprite is not transparent. I need it transparent to make it
use the ladder. See spritesheet:

http://howlerbr1.game-host.org:8080/molemole.gif

The player is still with black background instead of transparent.

thanks for any help!
Offline pjt33
« Reply #1 - Posted 2011-02-22 23:38:23 »

At the very least you need to use the Color constructor which takes a boolean indicating whether it should take into account the alpha component of the int parameter.
Offline luisoft

JGO Coder


Projects: 6


Java games rock!


« Reply #2 - Posted 2011-02-23 00:01:04 »

Have already tried that but there was no change. Still getting my background as black instead of transparent.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline pjt33
« Reply #3 - Posted 2011-02-23 00:43:40 »

It's probably created with black.

The other thing you could do, which is a bit more expensive, is access the backing data store directly.
1  
int[] buf = ((DataBufferInt)sprites[i].getRaster().getDataBuffer()).getData();

On the bright side, you can then eliminate the Color constructor calls entirely. In fact, you can probably simplify the whole arithmetic quite a lot to something along the lines of

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
int strOff = 0;
for (int i = 0; i < sprites.length; i++) {
    sprites[i] = new BufferedImage(16, 16, 2);
    int[] buf = ((DataBufferInt)sprites[i].getRaster().getDataBuffer()).getData();

    int bufOff = 0;
    for (int y = 0; y < 256; y++) {
        char ch = str.charAt(strOff++);
        buf[bufOff++] = ch == 'A' ? color[0] : (0xff000000 | color[ch - 'A']);
    }
}


And to be honest, you're probably not saving anything by not having the 0xff000000 in the color array in the first place.

Final edit: also, color[0] doesn't look very transparent to me. It's 0xff000100: fully opaque almost-black.
Offline me

Senior Member


Projects: 4


Java games rock!


« Reply #4 - Posted 2011-02-23 09:35:16 »

Hi Luisoft,

The way I used transparency was to create a bufferedimage with TYPE_INT_ARGB and
not to draw anything when I needed transparency.

Taken from maze4k 2010 entry:
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  
import java.applet.Applet;
import java.awt.image.*;
import java.awt.event.*;
import java.awt.*;

public class transparent extends Applet implements Runnable {

   final int color[]={0, -1};

   // SpriteSheet
  final String str="$ $ ###############$ $ $ $#$ $ $ $ $ $ $ $  # ############  # $ $ $  $  #    $ $ $$$$##########$$ $$$         $$$$";


   public final void start() {
                new Thread(this).start();
        }


        public final void run() {
      int width=48;
      int height=48;
      int TILESIZE=24;

      final BufferedImage screen = new BufferedImage(800, 600, BufferedImage.TYPE_INT_RGB);
                Graphics g =  screen.getGraphics();

      final BufferedImage SpriteSheet=new BufferedImage(4*TILESIZE,4*TILESIZE,BufferedImage.TYPE_INT_ARGB);
                Graphics ssg=SpriteSheet.getGraphics();
                for(int i=0; i < str.length(); i++){
         // If there is a space it is transparent!
                       if(str.charAt(i)!=' '){
                                int cc=str.charAt(i)-35;
                                ssg.setColor(new Color(color[cc]));
                                ssg.fillRect((i%width)*(TILESIZE/12),(i/height)*(TILESIZE/12), TILESIZE/12, TILESIZE/12);
                        }
                }        
      int bg=-1;      
      while (true) {
                        g.setColor(new Color(bg));
                        g.fillRect(0, 0, 800, 600);
         g.drawImage(SpriteSheet, 30,30,this);
         getGraphics().drawImage(screen, 0, 0, null);
         // new background color
        bg++;

         try{
            Thread.sleep(10);
         }catch(Exception e){}      
      }
   }
}



This example changes the background color and you
will notice that the white and black dots nicely fit on
the background.

Hope this helps.

Best regards from

M.E.

M.E.
--------
Offline halfwitgoat

Junior Member


Projects: 2



« Reply #5 - Posted 2011-02-23 14:17:35 »

An alpha of 0xff is fully opaque. You want 0x00.

But as me said, if you want fully transparent, why draw the pixel at all?

Offline luisoft

JGO Coder


Projects: 6


Java games rock!


« Reply #6 - Posted 2011-02-23 16:22:17 »

I tried, see the if in the code below, I want to make the black color transparent, and so I skip it when drawing the bufferedimage to have the transparency:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
      final int color[] = { 0xff000000, 0xff605ded, 0xfffde995, 0xfffc5e5c,
            0xfffcfffb, 0xff39ecf7, 0xff2cd55e, 0xff1dae36, 0xffd55a56,
            0xffff807e, };

                        sprites[i] = new BufferedImage(TILE_SIZE / 2, TILE_SIZE / 2, 2);
         Graphics2D g = sprites[i].createGraphics();
         for (int y = 0; y < 16; y++) {
            for (int x = 0; x < 16; x++) {
               if (color[str.charAt((y * 16 + x) + (i * 16 * 16)) - 65] != 0xff000000) {
                  g.setColor(new Color(color[str.charAt((y * 16 + x)
                        + (i * 16 * 16)) - 65], true));
                  g.fillRect(x, y, TILE_SIZE / 2, TILE_SIZE / 2);
               }

            }
         }


Using the if my sprite is not correctly drawn:



If I remove the if then I got:



But the last one is not transparent, it's full opaque.

I'm not seeing what I'm doing wrong, it must be a stupid thing but I did not find it yet... any idea?
Offline zeroone
« Reply #7 - Posted 2011-02-23 16:39:37 »

To use fillRect to draw 1 pixel at time, shouldn't the width and height be set to 1?

1  
g.fillRect(x, y, 1, 1);
Offline luisoft

JGO Coder


Projects: 6


Java games rock!


« Reply #8 - Posted 2011-02-23 17:59:08 »

To use fillRect to draw 1 pixel at time, shouldn't the width and height be set to 1?

1  
g.fillRect(x, y, 1, 1);


My guess was right! It was a REALLY stupid mistake in my code.

zeroone, thanks a lot again, the problem was exactly that one! Filling a pixel at time solved the problem.

Now I need to get back to work (writting some automated tests here... it's cool, but I'm losing game programming practice Sad )

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.

CogWheelz (18 views)
2014-07-30 21:08:39

Riven (25 views)
2014-07-29 18:09:19

Riven (15 views)
2014-07-29 18:08:52

Dwinin (13 views)
2014-07-29 10:59:34

E.R. Fleming (33 views)
2014-07-29 03:07:13

E.R. Fleming (12 views)
2014-07-29 03:06:25

pw (43 views)
2014-07-24 01:59:36

Riven (44 views)
2014-07-23 21:16:32

Riven (30 views)
2014-07-23 21:07:15

Riven (31 views)
2014-07-23 20:56:16
List of Learning Resources
by SilverTiger
2014-07-31 18:29:50

List of Learning Resources
by SilverTiger
2014-07-31 18:26:06

List of Learning Resources
by SilverTiger
2014-07-31 13:54:12

HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54
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!