Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (513)
Games in Android Showcase (120)
games submitted by our members
Games in WIP (577)
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  
  Masked Transparent color with drawImage?  (Read 1693 times)
0 Members and 1 Guest are viewing this topic.
Offline Pakz

Junior Newbie





« Posted 2011-05-19 19:56:09 »

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.

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  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
129  
130  
131  
132  
133  
134  
135  
136  
137  
138  
139  
140  
141  
142  
143  
144  
145  
146  
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.
Offline zoto

Senior Duke


Medals: 4



« Reply #1 - Posted 2011-05-19 20:56:41 »

Quote
I have a black color that should not be drawn.
Use
1  
new Color(0, 0, 0, 0)

 the 4th argument is the alpha level.

Quote
Oh and how do you return a color value from a method? return new Color(10,10,10) in this kind of way?
Not really sure what problem you are having with this.
1  
2  
3  
public Color getColor(){
  return new Color(10,10,10);
}
Offline philfrei
« Reply #2 - Posted 2011-05-19 21:09:19 »

Just to supplement the previous reply, you will also want to set your Image to type "TYPE_INT_ARGB" in order to take advantage of the alpha channel (0 = transparent, 255 = fully opaque).

I usually do pixel editing via a BufferedImage defined like so:
1  
img = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);   

"It's after the end of the world! Don't you know that yet?"
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Pakz

Junior Newbie





« Reply #3 - Posted 2011-05-19 21:24:26 »

I got it working thank you.

I guess regular images do not have the transparant mode since I did not get that working but with the bufferedimages it did work.


If anyone is interested here is the working code. (no external media is used)

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  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
129  
130  
131  
132  
133  
134  
135  
136  
137  
138  
139  
140  
141  
142  
143  
144  
145  
import java.applet.*;
import java.awt.*;
import java.util.Random;
import java.awt.image.BufferedImage;


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[][]={
                     {4,4,4,4,5,5,4,4,4,4,4,4,5,4,4,4},
                     {4,5,4,4,4,4,4,4,4,4,4,4,5,5,4,4},
                     {4,4,5,4,4,4,4,4,4,4,4,4,5,5,4,4},
                     {4,4,5,5,5,5,4,4,4,4,4,4,4,5,5,4},
                     {4,4,5,5,5,5,4,4,4,5,4,4,4,4,5,4},
                     {5,4,4,4,5,5,4,4,4,5,4,4,4,4,4,5},
                     {5,4,4,4,4,4,4,4,4,5,4,4,4,4,4,4},
                     {4,4,4,4,5,4,4,4,4,5,5,4,4,4,4,4},
                     {4,4,4,4,4,5,4,4,4,5,5,5,4,4,4,4},
                     {4,4,4,4,4,4,4,4,4,5,5,5,5,4,4,4},
                     {4,4,5,4,4,4,4,4,4,4,5,5,5,4,4,4},
                     {4,4,5,5,4,4,4,4,4,4,5,5,5,5,4,4},
                     {4,5,5,5,4,4,4,4,5,4,4,5,5,5,4,4},
                     {4,5,5,5,5,5,4,4,5,5,4,4,5,5,4,4},
                     {4,4,4,4,5,5,5,4,4,4,4,4,4,5,4,4},
                     {4,4,4,4,4,5,5,5,4,4,4,4,5,4,4,4},
                     };
     public void init(){
        setBackground(Color.black);
        offscreen = createImage(getSize().width,getSize().height);
          bufferGraphics = offscreen.getGraphics();

       image2 = new BufferedImage(16, 16, BufferedImage.TYPE_INT_ARGB);
        Graphics test1 = image2.getGraphics();
        for( int y = 0 ; y < 16 ; y++ ){
           for ( int x = 0 ; x < 16 ; x++ ){
            test1.setColor(getcolor(tree1[x][y]));
              test1.fillRect(y,x,1,1);
           }
        }
       image3 = new BufferedImage(16, 16, BufferedImage.TYPE_INT_ARGB);
        Graphics test2 = image3.getGraphics();
        for( int y = 0 ; y < 16 ; y++ ){
           for ( int x = 0 ; x < 16 ; x++ ){
            test2.setColor(getcolor(mountain1[x][y]));
              test2.fillRect(y,x,1,1);
           }
        }

       image4 = new BufferedImage(16, 16, BufferedImage.TYPE_INT_ARGB);
        Graphics test3 = image4.getGraphics();
        for( int y = 0 ; y < 16 ; y++ ){
           for ( int x = 0 ; x < 16 ; x++ ){
            test3.setColor(getcolor(ground1[x][y]));
              test3.fillRect(y,x,1,1);
           }
        }


     }


   public Color getcolor(int c){
      if (c ==  0 ) return new Color(0,0,0,0);
      if (c ==  1 ) return new Color(0,240,0,255);
      if (c ==  2 ) return new Color(200,100,0,255);
      if (c ==  3 ) return new Color(200,200,200,255);
      if (c ==  4 ) return new Color(30,200,10,255);
      if (c ==  5 ) return new Color(50,220,20,255);
      return new Color(0,0,0,0);
   }

   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,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,this);
              }
              if (r1 == 0){
                 bufferGraphics.drawImage(image3,32+x*16,32+y*16,this);
              }
           }
        }
        g.drawImage(offscreen,0,0,this);
     }
     public void update(Graphics g){
          paint(g);
     }
 }
Offline pitbuller
« Reply #4 - Posted 2011-05-19 22:18:35 »

I would use color array for swiching colors. But usually I just try over optimize not important things.
1  
final static Color[] colors = {new Color(0,0,0,0),new Color(0,240,0,255),new Color(200,100,0,255),new Color(200,200,200,255),new Color(30,200,10,255),new Color(50,220,20,255)};
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.

Longarmx (52 views)
2014-10-17 03:59:02

Norakomi (43 views)
2014-10-16 15:22:06

Norakomi (33 views)
2014-10-16 15:20:20

lcass (38 views)
2014-10-15 16:18:58

TehJavaDev (68 views)
2014-10-14 00:39:48

TehJavaDev (68 views)
2014-10-14 00:35:47

TehJavaDev (60 views)
2014-10-14 00:32:37

BurntPizza (73 views)
2014-10-11 23:24:42

BurntPizza (45 views)
2014-10-11 23:10:45

BurntPizza (86 views)
2014-10-11 22:30:10
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
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!