www.pudn.com > BlackCross.rar > backgroundHandler.java


import java.io.IOException; 
import java.io.InputStream; 
import javax.microedition.lcdui.Graphics; 
import javax.microedition.lcdui.Image; 
 
class backgroundHandler 
{ 
    Image bkgBild[]; 
    int width; 
    int height; 
    int xPos; 
    int yPos; 
    int tileSize; 
    int horizontalScroll; 
    int verticalScroll; 
    int tilesUsed[]; 
    byte tiles[]; 
    byte tilesWidth[]; 
    byte tilesHeight[]; 
     
    public backgroundHandler() 
    { 
        bkgBild = new Image[32]; 
        xPos = 0; 
        yPos = 0; 
        tileSize = 32; 
        horizontalScroll = 0; 
        verticalScroll = 0; 
        tilesUsed = new int[32]; 
        tiles = new byte[800]; 
        tilesWidth = new byte[1]; 
        tilesHeight = new byte[1]; 
    } 
 
    public void setBackgroundproperties(int i, int j) 
    { 
        width = i; 
        height = j; 
    } 
 
    void loadTileNumbers(int i) 
    { 
        for(int j = 0; j < 32; j++) 
            tilesUsed[j] = 0; 
 
        try 
        { 
            InputStream inputstream = getClass().getResourceAsStream("/sets/tile" + i + ".set"); 
            inputstream.read(tilesWidth, 0, 1); 
            inputstream.read(tilesHeight, 0, 1); 
            inputstream.read(tiles, 0, tilesWidth[0] * tilesHeight[0]); 
            inputstream.close(); 
            tilesWidth[0] /= 4; 
            tilesHeight[0] *= 4; 
            xPos = 0; 
            yPos = tilesHeight[0] * tileSize - (128 / tileSize + 2) * tileSize; 
            if(tilesWidth[0] > 128 / tileSize) 
                horizontalScroll = 1; 
            if(tilesHeight[0] > 128 / tileSize) 
                verticalScroll = 1; 
            for(int k = 0; k < tilesWidth[0] * tilesHeight[0]; k++) 
                tilesUsed[tiles[k]] = 1; 
 
        } 
        catch(Exception exception) { } 
    } 
 
    void loadTileset(int i) 
    { 
        try 
        { 
            for(int j = 0; j < 32; j++) 
                if(tilesUsed[j] == 1) 
                    bkgBild[j] = Image.createImage("/nbkg" + i + "/til" + j + ".png"); 
 
        } 
        catch(IOException ioexception) { } 
    } 
 
    void draw(Graphics g) 
    { 
        if(horizontalScroll == 1) 
        { 
            if(xPos < 0) 
                xPos = 0; 
            if(xPos > tilesWidth[0] * 32 - 160) 
                xPos = tilesWidth[0] * 32 - 160; 
        } 
        if(verticalScroll == 1) 
        { 
            if(yPos < 0) 
                yPos = 0; 
            if(yPos > tilesHeight[0] * 32 - 160) 
                xPos = tilesHeight[0] * 32 - 160; 
        } 
        int i = -xPos % 32; 
        int j = -yPos % 32; 
        int k = yPos / 32; 
        int l = xPos / 32; 
        int l1 = i; 
        byte byte1 = 4; 
        byte byte2 = 4; 
        if(width <= 96) 
            byte2 = 3; 
        if(height <= 96) 
            byte1 = 3; 
        if(height <= 54) 
            byte1 = 2; 
        for(int i1 = 0; i1 < byte1 + verticalScroll; i1++) 
        { 
            int k1 = k * tilesWidth[0]; 
            for(int j1 = 0; j1 < byte2 + horizontalScroll; j1++) 
            { 
                byte byte0 = tiles[k1 + j1 + l]; 
                g.drawImage(bkgBild[byte0], l1, j, 20); 
                l1 += 32; 
            } 
 
            l1 = i; 
            j += 32; 
            k++; 
        } 
 
    } 
    
}