This is my first post here. hello!
I spend an hour or so looking into this but I have not figured out how to solve this one. The problem I have right now is that I want to draw a image that I created in my code with transparancy. I have a black color that should not be drawn.
I am using createimage and drawimage. Just the Java’s regular images. They have a mask color option don’t they?
How do you draw for say a mountain sprite on a grass sprite without erasing the grass besides the mountain edges.
Here is the code that I was working on.
import java.applet.*;
import java.awt.*;
import java.util.Random;
public class DataSpritesInJava02 extends Applet {
Random r = new Random();
Graphics bufferGraphics;
Image offscreen;
Image image2;
Image image3;
Image image4;
// Here I have stored a sprite that will be drawn onto the screen.
// Note : you have to switch the x and y in the create sprite part to get
// the right sprite view since the layout of array data has switched x and y view.
private short tree1[][]={
{0,0,0,0,0,0,1,1,1,1,1,0,0,0,0,0},
{0,0,0,0,1,1,1,1,1,1,1,1,0,0,0,0},
{0,0,0,1,1,1,1,1,1,1,1,1,1,0,0,0},
{0,0,1,1,1,1,1,1,1,1,1,1,1,1,0,0},
{0,0,1,1,1,1,1,1,1,1,1,1,1,1,0,0},
{0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0},
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
{0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0},
{0,0,1,1,1,1,1,1,1,1,1,1,1,1,0,0},
{0,0,0,0,1,1,1,1,1,1,1,1,0,0,0,0},
{0,0,0,0,0,0,0,2,2,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,2,2,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,2,2,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,2,2,0,0,0,0,0,0,0}
};
private short mountain1[][]={
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,3,3,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,3,3,0,0,0,0,0,0,0},
{0,0,0,0,0,0,3,3,3,3,0,0,0,0,0,0},
{0,0,0,0,0,0,3,3,3,3,0,0,0,0,0,0},
{0,0,0,0,0,3,3,3,3,3,3,0,0,0,0,0},
{0,0,0,0,0,3,3,3,3,3,3,0,0,0,0,0},
{0,0,0,0,3,3,3,3,3,3,3,3,0,0,0,0},
{0,0,0,0,3,3,3,3,3,3,3,3,0,0,0,0},
{0,0,0,3,3,3,3,3,3,3,3,3,3,0,0,0},
{0,0,0,3,3,3,3,3,3,3,3,3,3,0,0,0},
{0,0,3,3,3,3,3,3,3,3,3,3,3,3,0,0},
{0,0,3,3,3,3,3,3,3,3,3,3,3,3,0,0},
{0,3,3,3,3,3,3,3,3,3,3,3,3,3,3,0},
{0,3,3,3,3,3,3,3,3,3,3,3,3,3,3,0},
{3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3},
};
private short ground1[][]={
{0,0,0,0,1,1,0,0,0,0,0,0,1,0,0,0},
{0,1,0,0,0,0,0,0,0,0,0,0,1,1,0,0},
{0,0,1,0,0,0,0,0,0,0,0,0,1,1,0,0},
{0,0,1,1,1,1,0,0,0,0,0,0,0,1,1,0},
{0,0,1,1,1,1,0,0,0,1,0,0,0,0,1,0},
{1,0,0,0,1,1,0,0,0,1,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0},
{0,0,0,0,1,0,0,0,0,1,1,0,0,0,0,0},
{0,0,0,0,0,1,0,0,0,1,1,1,0,0,0,0},
{0,0,0,0,0,0,0,0,0,1,1,1,1,0,0,0},
{0,0,1,0,0,0,0,0,0,0,1,1,1,0,0,0},
{0,0,1,1,0,0,0,0,0,0,1,1,1,1,0,0},
{0,1,1,1,0,0,0,0,1,0,0,1,1,1,0,0},
{0,1,1,1,1,1,0,0,1,1,0,0,1,1,0,0},
{0,0,0,0,1,1,1,0,0,0,0,0,0,1,0,0},
{0,0,0,0,0,1,1,1,0,0,0,0,1,0,0,0},
};
public void init(){
setBackground(Color.black);
offscreen = createImage(getSize().width,getSize().height);
bufferGraphics = offscreen.getGraphics();
image2 = createImage(16,16);
Graphics test1 = image2.getGraphics();
for( int y = 0 ; y < 16 ; y++ ){
for ( int x = 0 ; x < 16 ; x++ ){
test1.setColor(Color.black);
if (tree1[x][y] == 1 ){
test1.setColor(Color.green);
}
if (tree1[x][y] == 2 ){
test1.setColor(new Color(200,100,0));
}
// Here we draw the pixel.
test1.fillRect(y,x,1,1);
}
}
image3 = createImage(16,16);
Graphics test2 = image3.getGraphics();
for( int y = 0 ; y < 16 ; y++ ){
for ( int x = 0 ; x < 16 ; x++ ){
test2.setColor(Color.black);
if (mountain1[x][y] == 3 ){
test2.setColor(new Color(100,100,100));
}
test2.fillRect(y,x,1,1);
}
}
image4 = createImage(16,16);
Graphics test3 = image4.getGraphics();
for( int y = 0 ; y < 16 ; y++ ){
for ( int x = 0 ; x < 16 ; x++ ){
test3.setColor(new Color(30,200,10));
if (ground1[x][y] == 1 ){
test3.setColor(new Color(50,220,20));
}
test3.fillRect(y,x,1,1);
}
}
}
public void paint(Graphics g){
bufferGraphics.clearRect(0,0,getSize().width,getSize().height);
bufferGraphics.setColor(Color.red);
bufferGraphics.drawString("Data [][] Sprites - Tiny trees.",10,10);
for( int y = 0 ; y < 10 ; y++){
for( int x = 0 ; x < 16 ; x++){
bufferGraphics.drawImage(image4,32+x*16,32+y*16,new Color(0,0,0),this);
}
}
int r1 = 0;
for( int y = 0 ; y < 10 ; y++){
for( int x = 0 ; x < 16 ; x++){
r1 = r.nextInt(4);
if (r1 == 1){
bufferGraphics.drawImage(image2,32+x*16,32+y*16,new Color(0,0,0),this);
}
if (r1 == 0){
bufferGraphics.drawImage(image3,32+x*16,32+y*16,new Color(0,0,0),this);
}
}
}
g.drawImage(offscreen,0,0,this);
}
public void update(Graphics g){
paint(g);
}
}
Oh and how do you return a color value from a method? return new Color(10,10,10) in this kind of way?
Java is kind of difficult for me as a beginner.