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!
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:
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
http://img189.imageshack.us/img189/3500/transparencybug.png <- Bigger size. on the other one you wont be able to see it