GAMES LESSON Twenty-four: Making Sprite Transparent

The technique to make the sprite tile transparent is a little bit complicated, but it works! Basically we just make all the bluish background from the guy.jpg sprite image transparent and store this in an Image variable called g2.

The part of the code which filters the bluish background and makes it transparent is a single method:

public Image makeClear(){ Image temp=null; try{ int pixel[] = new int[w*h]; PixelGrabber pg = new PixelGrabber(guy, 0, 0, w, h, pixel, 0, w); if(pg.grabPixels() &&((pg.status() & ImageObserver.ALLBITS) != 0)){ for(int x = 0; x< w * h; x++){ int r = (pixel[x] & 0x00ff0000)>>16; int g = (pixel[x] & 0x0000ff00)>>8; int b = (pixel[x] & 0x000000ff); //filter for bluish pixels if(b>50 && g>50){ pixel[x]= (0<<24)|(r<<16)|(g<<8)|b; } } temp=createImage(new MemoryImageSource(w, h, pixel, 0, w)); } }catch(InterruptedException e) { } return temp; }

The bits which make the image transparent are the first ones in the line that looks like this:

pixel[x]= (0<<24)|(r<<16)|(g<<8)|b;

If the 0 were set to 255 the image would not be transparent at all. Values between 0 and 255 set the image to different degrees of transparentness. For instance 50 would be mostly see-through, whereas 200 would be mostly solid.

import java.awt.*; import java.applet.*; import java.awt.event.*; import java.awt.image.*; public class TERR3 extends Applet{ Dimension d; //brick=0, sky=1, guy=2 int[][] map = { { 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,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,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,0,0,0,1,1,1,1,1,1,1,1}, { 1,1,1,1,1,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,0,0,0,1,1,1,1,2,1,1,1,0,0,0,0,0,0,0,1,1,1,1,1,1}, { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, }; //28x8 int xguy=11; int yguy=6; int xedge = xguy - 5; int bgx=-100; int taps; Image bg, brick, guy; Image offI; Image g2; int w=20; int h=20; public void init(){ d = getSize(); offI=createImage(d.width,d.height); brick = getImage(getDocumentBase(), "brick.jpg"); bg = getImage(getDocumentBase(), "bg.jpg"); guy = getImage(getDocumentBase(), "guy.jpg"); g2 = makeClear(); requestFocus(); this.addKeyListener(new KeyAdapter(){ public void keyPressed(KeyEvent k) { if(k.getKeyCode() == KeyEvent.VK_LEFT){ if(taps%2==0) bgx++; if(xguy>0){ map[yguy][xguy]=1; xguy--; if(map[yguy][xguy]==1) map[yguy][xguy]=2; else{ yguy--; map[yguy][xguy]=2; } } else{ map[yguy][xguy]=1; xguy=27; if(map[yguy][xguy]==1) map[yguy][xguy]=2; else{ yguy--; map[yguy][xguy]=2; } } } else if(k.getKeyCode() == KeyEvent.VK_RIGHT){ if(taps%2==0) bgx--; map[yguy][xguy]=1; xguy++; xguy%=28; if(map[yguy][xguy]==1) map[yguy][xguy]=2; else{ yguy--; map[yguy][xguy]=2; } } if(bgx>0) bgx=0; if(bgx<-300) bgx=-300; if(map[yguy+1][xguy]==1){ map[yguy][xguy]=1; yguy++; map[yguy][xguy]=2; } xedge = xguy-5; if(xedge<0) xedge=28+xedge; taps++; repaint(); } }); } public Image makeClear(){ Image temp=null; try{ int pixel[] = new int[w*h]; PixelGrabber pg = new PixelGrabber(guy, 0, 0, w, h, pixel, 0, w); if(pg.grabPixels() &&((pg.status() & ImageObserver.ALLBITS) != 0)){ for(int x = 0; x< w * h; x++){ int r = (pixel[x] & 0x00ff0000)>>16; int g = (pixel[x] & 0x0000ff00)>>8; int b = (pixel[x] & 0x000000ff); if(b>50 && g>50){ pixel[x]= (0<<24)|(r<<16)|(g<<8)|b; } } temp=createImage(new MemoryImageSource(w, h, pixel, 0, w)); } }catch(InterruptedException e) { } return temp; } public void update(Graphics g){ paint(g); } public void paint(Graphics g){ Graphics offG = offI.getGraphics(); offG.setColor(Color.black); offG.drawImage(bg, bgx, 0, this); int hori=0; for(int y=0; y<map.length; y++) for(int x=xedge; x<xedge+11; x++){ if(map[y][x%28] == 0) offG.drawImage(brick,20*hori,10+20*y,this); else if(map[y][x%28] == 2) offG.drawImage(g2,20*hori,10+20*y,this); hori++; hori%=11; } g.drawImage(offI, 0, 0, this); } }


ASSIGNMENT: