Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (580)
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]
  ignore  |  Print  
  Transparancy [fairly simple]  (Read 2198 times)
0 Members and 1 Guest are viewing this topic.
Offline elamre

JGO Coder


Medals: 17
Projects: 1


hitar!


« Posted 2012-02-08 23:20:00 »

Hello, im trying to work with transparancy. So i have copy'd the following code:
now this 0xFF00FF00 used to be 0xFF0000 for blue. But i have changed to make it work with purple/pink. However this does not seem to work.
Can anybody help me out with this simple question?

1  
2  
3  
4  
5  
6  
7  
8  
9  
            private Image setTransparency(BufferedImage image){
      ImageFilter filter = new RGBImageFilter(){
         public final int filterRGB(int x, int y, int rgb){
            return (rgb << 8) & 0xFF00FF00;                  //0xFF(255.)00(0.)FF(255.)00(0) r,g,b,t
        }
      };
      ImageProducer ip = new FilteredImageSource(image.getSource(), filter);
      return Toolkit.getDefaultToolkit().createImage(ip);
   }


My projects:
Tower Defence!]http://www.java-gaming.org/topics/iconified/25690/view.html]Tower Defence! [lll.......] 30%!
Lightsnakerider! [llllll....] 60%!
Offline GabrielBailey74
« Reply #1 - Posted 2012-02-08 23:25:26 »

One second, i'll post all methods needed =D.

The color of course:


The actual transparent method, similar to yours:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
   private static BufferedImage makeTransparent(BufferedImage tmpImage) {
        int h = tmpImage.getHeight(null);
        int w = tmpImage.getWidth(null);
        BufferedImage resultImage = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
        int transparentColor = tmpImage.getRGB(0,0);  
        for (int y = 0; y < h; y++)
          for (int x = 0; x < w; x++)  {
            int color = tmpImage.getRGB(x, y);
            if (color == transparentColor) {
               color = color & 0x00FFFFFF; // clear the alpha flag
           }
               resultImage.setRGB(x, y, color);
          }
        return resultImage;
   }


The method where you would actually pass the images through transparency in the method above(Example commented out)
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
   public static void makeTransparents() throws Exception {
      try {
          //BufferedImage tmp0 = ImageIO.read(new File("storage/sprites/interfaces/hud0.png"));
        //hud0 = makeTransparent(tmp0);
         SystemOut.append("[Transparent Image Created.");
         SystemOut.append("==== FINISHED CALLING TRANSPARENT IMAGES ====");
      } catch (FileNotFoundException e3) {
         System.out.println("File not found.");
         e3.printStackTrace();
      }
   }


The whole TransparentImage class (if needed):
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  
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileNotFoundException;
import javax.imageio.ImageIO;
import server.system.print.SystemOut;

public class TransparentImage {

   public static BufferedImage hud0; //Define the BufferedImage
  public static int tps = 0;
   public static boolean loaded = false;

   public static SystemOut so = new SystemOut(); //Ignore
 
   public TransparentImage() {
   }
   
   public static void makeTransparents() throws Exception {
      try {
          BufferedImage tmp0 = ImageIO.read(new File("storage/sprites/interfaces/hud0.png")); //Specify the file path.
        hud0 = makeTransparent(tmp0); //Pass that BufferedImage through our "makeTransparent()" method
         SystemOut.append("[Transparent Image Created.");
         SystemOut.append("==== FINISHED CALLING TRANSPARENT IMAGES ====");
      } catch (FileNotFoundException e3) {
         System.out.println("File not found.");
         e3.printStackTrace();
      }
   }
   
   private static BufferedImage makeTransparent(BufferedImage tmpImage) {
        int h = tmpImage.getHeight(null);
        int w = tmpImage.getWidth(null);
        BufferedImage resultImage = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
        int transparentColor = tmpImage.getRGB(0,0);  
        for (int y = 0; y < h; y++)
          for (int x = 0; x < w; x++)  {
            int color = tmpImage.getRGB(x, y);
            if (color == transparentColor) {
               color = color & 0x00FFFFFF; // clear the alpha flag
           }
               resultImage.setRGB(x, y, color);
          }
        return resultImage;
   }
   
}

Offline elamre

JGO Coder


Medals: 17
Projects: 1


hitar!


« Reply #2 - Posted 2012-02-09 00:07:39 »

Thanks a lot! why wasnt my function working though?
and what does this line do:
1  
rgb << 8


Ive made with your code the following sprite code(first time im using sprite sheets. seems useful! Smiley  )
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  
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;


public class Sprite {
   BufferedImage imgSheet;
   //TODO -> use the Offset
  private int xOffset, yOffset, x, y, size;
   
   
   Sprite( int x , int y, int offset, int size){
      init();
      this.x = x;
      this.size = size;
      this.y=y;
      setImage();
      xOffset = x+offset;
      yOffset = y+offset;
   }
   
   void setImage(){
     
   }
   
   void init(){
      try {
         imgSheet = ImageIO.read(new File("sheet.png"));
      } catch (IOException e) {
         System.out.println("Error while loading image sheet! \n Program will now abort.");
         e.printStackTrace();
      }
     
   }
   //Returns the corresponding image
  public Image getImage(){
      return setTransparency(imgSheet.getSubimage(x*size+xOffset, y*size+yOffset, size, size));
   }
   
   private Image setTransparency(BufferedImage image){
        int h = image.getHeight(null);
        int w = image.getWidth(null);
        BufferedImage resultImage = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
        int transparentColor = image.getRGB(0,0);  
        for (int y = 0; y < h; y++)
          for (int x = 0; x < w; x++)  {
            int color = image.getRGB(x, y);
            if (color == transparentColor) {
               color = color & 0x00FFFFFF; // clear the alpha flag
           }
               resultImage.setRGB(x, y, color);
          }
        return resultImage;
   }
}


My projects:
Tower Defence!]http://www.java-gaming.org/topics/iconified/25690/view.html]Tower Defence! [lll.......] 30%!
Lightsnakerider! [llllll....] 60%!
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline GabrielBailey74
« Reply #3 - Posted 2012-02-09 00:26:19 »

Java Operators:
http://docs.oracle.com/javase/tutorial/java/nutsandbolts/operators.html
Haven't used that operator myself yet :/

Offline ReBirth
« Reply #4 - Posted 2012-02-09 03:24:37 »

Shifting. If you had met binary then you'll know. That means to shift all bits to left for 8 steps according to the "arrow". So N >> 1 equals N / 2.

Offline elamre

JGO Coder


Medals: 17
Projects: 1


hitar!


« Reply #5 - Posted 2012-02-09 03:55:12 »

Shifting. If you had met binary then you'll know. That means to shift all bits to left for 8 steps according to the "arrow". So N >> 1 equals N / 2.

I remember my teacher saying something about that, only that was with microcontrollers and in c >>. But thanks for the heads-up!

Anyways i have a small problem while trying to draw text. If the text does not have pink in it, then the black will become transparent. And sometimes the pink doesnt get transparent. I use rather much code, i hope somebody can be bothered to see my problem!
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  
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;


public class TextChar {
   
   BufferedImage imgSheet;
   Image Char;

   private int xPosition, yPosition;
   
   
   TextChar(int xPosition, int yPosition, int x, int y, int offset, int size){
      this.xPosition = xPosition;
      this.yPosition = yPosition;
      try {
         imgSheet = ImageIO.read(new File("charSheet.png"));
      } catch (IOException e) {
         System.out.println("Error while loading image sheet! \n Program will now abort.");
         e.printStackTrace();
      }

      Char = setTransparency(imgSheet.getSubimage(x*size+x+offset, y*size+y+offset, size, size));
   }
   TextChar(int xPosition, int yPosition, int x, int y, int offset, int size, int useless){
      this.xPosition = xPosition;
      this.yPosition = yPosition;
      try {
         imgSheet = ImageIO.read(new File("charSheet.png"));
      } catch (IOException e) {
         System.out.println("Error while loading image sheet! \n Program will now abort.");
         e.printStackTrace();
      }

      Char = imgSheet.getSubimage(x*size+x+offset, y*size+y+offset, size, size);
   }
   
   private Image setTransparency(BufferedImage image){
        int h = image.getHeight(null);
        int w = image.getWidth(null);
        BufferedImage resultImage = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
        int transparentColor = image.getRGB(0,0);  
        for (int y = 0; y < h; y++)
          for (int x = 0; x < w; x++)  {
            int color = image.getRGB(x, y);
            if (color == transparentColor) {
               color = color & 0x00FFFFFF; // clear the alpha flag
           }
               resultImage.setRGB(x, y, color);
          }
        return resultImage;
   }
   
   public void draw(Graphics g){
      g.drawImage(Char, xPosition, yPosition, null);
   }
}

And my Line class, which will make for each char an object:
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  
import java.awt.Graphics;
import java.util.ArrayList;


public class TextLine {
   private ArrayList<TextChar> characters;
   //TextChar(int xPosition, int yPosition, int x, int y, int offset, int size)
  TextLine(int x, int y, String line){
      characters = new ArrayList<TextChar>();
       char[] stringArray;

       stringArray = line.toCharArray();
       for(int i = 0; i<stringArray.length;i++){
          switch(stringArray[i]){
          case 'a':
             characters.add(new TextChar(x+i*6,y,0,0,1,7));
             break;
          case 'b':
             characters.add(new TextChar(x+i*6,y,1,0,1,7));
             break;
          case 'c':
             characters.add(new TextChar(x+i*6,y,2,0,1,7));
             break;
          case 'd':
             characters.add(new TextChar(x+i*6,y,3,0,1,7));
             break;
          case 'e':
             characters.add(new TextChar(x+i*6,y,4,0,1,7));
             break;
          case 'f':
             characters.add(new TextChar(x+i*6,y,5,0,1,7));
             break;
          case 'g':
             characters.add(new TextChar(x+i*6,y,6,0,1,7));
             break;
          case 'h':
             characters.add(new TextChar(x+i*6,y,7,0,1,7));
             break;
          case 'i':
             characters.add(new TextChar(x+i*6,y,8,0,1,7));
             break;
          case 'j':
             characters.add(new TextChar(x+i*6,y,9,0,1,7));
             break;
          case 'k':
             characters.add(new TextChar(x+i*6,y,0,1,1,7));
             break;
          case 'l':
             characters.add(new TextChar(x+i*6,y,1,1,1,7));
             break;
          case 'm':
             characters.add(new TextChar(x+i*6,y,2,1,1,7,1));
             break;
          case 'n':
             characters.add(new TextChar(x+i*6,y,3,1,1,7,1));
             break;
          case 'o':
             characters.add(new TextChar(x+i*6,y,4,1,1,7));
             break;
          case 'p':
             characters.add(new TextChar(x+i*6,y,5,1,1,7));
             break;
          case 'q':
             characters.add(new TextChar(x+i*6,y,6,1,1,7));
             break;
          case 'r':
             characters.add(new TextChar(x+i*6,y,7,1,1,7));
             break;
          case 's':
             characters.add(new TextChar(x+i*6,y,8,0,1,7));
             break;
          case 't':
             characters.add(new TextChar(x+i*6,y,9,0,1,7));
             break;
          case 'u':
             characters.add(new TextChar(x+i*6,y,0,2,1,7));
             break;
          case 'v':
             characters.add(new TextChar(x+i*6,y,1,2,1,7));
             break;
          case 'w':
             characters.add(new TextChar(x+i*6,y,2,2,1,7));
             break;
          case 'x':
             characters.add(new TextChar(x+i*6,y,3,2,1,7));
             break;
          case 'y':
             characters.add(new TextChar(x+i*6,y,4,2,1,7,1));
             break;
          case 'z':
             characters.add(new TextChar(x+i*6,y,5,2,1,7,1));
             break;

          }
       }
   }
   
   public void draw(Graphics g){
      for(int i = 0; i<characters.size(); i++){
         TextChar m = (TextChar)characters.get(i);
            m.draw(g);
      }
   }
}

Screenshot of the problem (i have used 2 ways, right is the normal one, and left is the one with another constructor for some of the problematic chars, which wont make it transparent)

http://img189.imageshack.us/img189/3500/transparencybug.png <- Bigger size. on the other one you wont be able to see it

My projects:
Tower Defence!]http://www.java-gaming.org/topics/iconified/25690/view.html]Tower Defence! [lll.......] 30%!
Lightsnakerider! [llllll....] 60%!
Offline ReBirth
« Reply #6 - Posted 2012-02-09 04:10:44 »

Crap! what a big screen you have there Cheesy
My real study is microcontroller so I know that before +=.

btt, your code looks fine. Need deep thought to find out Clueless


Offline GabrielBailey74
« Reply #7 - Posted 2012-02-09 04:11:36 »

Uploading a Image atm sec.

Could be the image  Shocked
If you can see it:


Look real close, it looks like you're first chars are spaced fine, than towards the edge you're cutting them a little closer:
Not giving enough space between chars for the transparency to fill it.

Offline elamre

JGO Coder


Medals: 17
Projects: 1


hitar!


« Reply #8 - Posted 2012-02-09 11:08:16 »

So how would i fix this? do i need bigger letters? (The horror of making them all over again! ><)
Also why is my black getting transparent then? any ideas?

My projects:
Tower Defence!]http://www.java-gaming.org/topics/iconified/25690/view.html]Tower Defence! [lll.......] 30%!
Lightsnakerider! [llllll....] 60%!
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.

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

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

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

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

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

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

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

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

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

CJLetsGame (209 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!