Transparancy [fairly simple]
elamre:
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);
}
GabrielBailey74:
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;
}
}
elamre:
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! :) )
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;
}
}
GabrielBailey74:
Java Operators:
http://docs.oracle.com/javase/tutorial/java/nutsandbolts/operators.html
Haven't used that operator myself yet :/
ReBirth:
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.
Navigation
[0] Message Index
[#] Next page