Java位移和颜色

时间:2013-09-29 13:47:40

标签: java bit-shift

嗨,我是java的新手,我遇到的问题是我的颜色是单色的,所以我改变了一些东西然后我现在所有的都是黑屏这些是我的图形类

    package game.game.gfx;

public class Screen 
{
public static final int MAP_WIDTH =64;
public static final int MAP_WIDTH_MASK = MAP_WIDTH -1;

public int[]pixels;

public int xOffset =0;
public int yOffset =0;
public int width;
public int height;

public SpriteSheet sheet;
public Screen(int width,int height,SpriteSheet sheet)
{
    this.width = width;
    this.height = height;
    this.sheet = sheet;

    pixels = new int[width*height];
    }


public  void render(int xPos,int yPos,int tile,int colour){
    xPos -=xOffset;
    yPos -=yOffset;
    int xTile = tile % 32;
    int yTile = tile / 32;
    int tileOffset = (xTile<<3)+(yTile<<3)*sheet.width;
    for(int y=0;y<8;y++){
        if(y+yPos < 0 || y + yPos >=height) continue;
        int ySheet=y;

        for(int x=0;x<8;x++){
            if(x+xPos <0||x+xPos >=width) continue;
            int xSheet=x;
            int col = (colour >> (sheet.pixels[xSheet + ySheet*sheet.width+tileOffset]*8))& 255;
            if(col<255)pixels[(x+xPos)+(y+yPos)*width]=col;
        }
    }

}

}

======

SpriteSheet

    package game.game.gfx;

import java.awt.image.BufferedImage;
import java.io.IOException;

import javax.imageio.ImageIO;


public class SpriteSheet 
{
public String path;
public int width;
public int height;

public int[]pixels;
public SpriteSheet(String path)
{
BufferedImage image = null; 

try {
    image = ImageIO.read(SpriteSheet.class.getResourceAsStream(path));
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
if (image == null)
{
    return;
}
this.path = path;
this.width = image.getWidth();
this.height = image.getHeight();

pixels = image.getRGB(0, 0, width, height, null, 0, width);
for (int i =0; i < pixels.length;i++)
{
    pixels[i] =(pixels[i]&0xff)/64;
}
for (int i =0;i<8;i++)
{
System.out.println(pixels[i]);  
}

}
}

=====

颜色

package game.game.gfx;

public class Colours {
public static int get(int colour1,int colour2,int colour3,int colour4){
    return  (get(colour4)<<24)+(get(colour3)<<16+(get(colour2)<<8)+get(colour1));   
}
private static int get(int colour) {
    if(colour < 0)return 255;
    int r =colour/ 100 % 10;
    int g =colour/ 10 % 10;
    int b =colour % 10;
    return r*36+g*6+b;
}
}

0 个答案:

没有答案