BufferedImage到BufferedImage数组,Tiles Disapear

时间:2015-12-02 03:48:21

标签: java bufferedimage

好吧Stackoverflow,你现在就是我的最后一行。

如果你看看下面的代码和图片,你会发现有两个文件被迅速命名

Tile.java

TileMap.java

有关这些课程的更多信息,请参阅Google“ForeignGuyMike Dragon Tale Tutorial Part 8”,了解该项目的下载文件。 我现在正在读他的课堂阅读瓷砖,因为它似乎对我的努力非常有效。切换到BufferedImage数组的原因是允许每个tile的动画,现在它可以处理包含超过1帧的图像。

现在为这两个班级: 它们是故意设置的,不是通过图像制作动画,只是为了显示它们打破这个程序而不是合并我的成像动画功能。

他们在这里

代码/图像中断之前 enter image description here

public class TileMap {

// position
private double x;
private double y;

// bounds
private int xmin;
private int ymin;
private int xmax;
private int ymax;

private double tween;

// map
private int[][] map;
private int tileSize;
private int numRows;
private int numCols;
private int width;
private int height;

// tileset
private BufferedImage[] tileset;
private int numTilesAcross;
private Tile[][] tiles;

private Animation an;

// drawing
private int rowOffset;
private int colOffset;
private int numRowsToDraw;
private int numColsToDraw;

public TileMap(int tileSize) {
    this.tileSize = tileSize;
    numRowsToDraw = GamePanel.HEIGHT / tileSize + 2;
    numColsToDraw = GamePanel.WIDTH / tileSize + 2;
    tween = 0.07;
    an = new Animation();
}

public void loadTiles(BufferedImage[] s, int delay) {

    an.setDelay(delay);
    an.setFrames(s);

    try {

        tileset = s;

        numTilesAcross = tileset[0].getWidth() / tileSize;
        tiles = new Tile[2][numTilesAcross];

        BufferedImage[] subimage = new BufferedImage[s.length];
        for(int col = 0; col < numTilesAcross; col++) {
            subimage[0] = tileset[0].getSubimage(
                        col * tileSize,
                        0,
                        tileSize,
                        tileSize
                    );
            tiles[0][col] = new Tile(subimage[0], Tile.NORMAL);
            subimage[0] = tileset[0].getSubimage(
                        col * tileSize,
                        tileSize,
                        tileSize,
                        tileSize
                    );
            tiles[1][col] = new Tile(subimage[0], Tile.BLOCKED);
        }

    }
    catch(Exception e) {
        e.printStackTrace();
    }

}

public void loadMap(String s) {

    try {

        InputStream in = getClass().getResourceAsStream(s);
        BufferedReader br = new BufferedReader(
                    new InputStreamReader(in)
                );

        numCols = Integer.parseInt(br.readLine());
        numRows = Integer.parseInt(br.readLine());
        map = new int[numRows][numCols];
        width = numCols * tileSize;
        height = numRows * tileSize;

        xmin = GamePanel.WIDTH - width;
        xmax = 0;
        ymin = GamePanel.HEIGHT - height;
        ymax = 0;

        String delims = "\\s+";
        for(int row = 0; row < numRows; row++) {
            String line = br.readLine();
            String[] tokens = line.split(delims);
            for(int col = 0; col < numCols; col++) {
                map[row][col] = Integer.parseInt(tokens[col]);
            }
        }

    }
    catch(Exception e) {
        e.printStackTrace();
    }

}

public int getTileSize() { return tileSize; }
public double getx() { return x; }
public double gety() { return y; }
public int getWidth() { return width; }
public int getHeight() { return height; }

public int getType(int row, int col) {
    int rc = map[row][col];
    int r = rc / numTilesAcross;
    int c = rc % numTilesAcross;
    return tiles[r][c].getType();
}

public void setTween(double d) { tween = d; }

public void setPosition(double x, double y) {

    this.x += (x - this.x) * tween;
    this.y += (y - this.y) * tween;

    fixBounds();

    colOffset = (int)-this.x / tileSize;
    rowOffset = (int)-this.y / tileSize;

}

private void fixBounds() {
    if(x < xmin) x = xmin;
    if(y < ymin) y = ymin;
    if(x > xmax) x = xmax;
    if(y > ymax) y = ymax;
}

public void update() {
    an.update();
}

public void draw(Graphics2D g) {

    for(
        int row = rowOffset;
        row < rowOffset + numRowsToDraw;
        row++) {

        if(row >= numRows) break;

        for(
            int col = colOffset;
            col < colOffset + numColsToDraw;
            col++) {

            if(col >= numCols) break;

            if(map[row][col] == 0) continue;

            int rc = map[row][col];
            int r = rc / numTilesAcross;
            int c = rc % numTilesAcross;

            g.drawImage(
                tiles[r][c].getImage(),
                (int)x + col * tileSize,
                (int)y + row * tileSize,
                null
            );

        }

    }

}

public class Tile {

private BufferedImage image;
private int type;

// tile types
public static final int NORMAL = 0;
public static final int BLOCKED = 1;

public Tile(BufferedImage image, int type) {
    this.image = image;
    this.type = type;
}

public BufferedImage getImage() { return image; }
public int getType() { return type; }

现在这是实施更改后的代码

变更集:

Tile.java

-Constructor Param已从BufferedImage更改为BufferedImage []

-BufferedImage image to BufferedImage [] image;

-getImage to getImage(int i){return image [i]; }

TileMap.java

- 初始化的子图像并将其设置为BufferedImage [s.length];

- 删除所有子图像[0]到子图像

draw中的

-getImage现在是getImage(0); //硬编码

enter image description here

public class TileMap {

// position
private double x;
private double y;

// bounds
private int xmin;
private int ymin;
private int xmax;
private int ymax;

private double tween;

// map
private int[][] map;
private int tileSize;
private int numRows;
private int numCols;
private int width;
private int height;

// tileset
private BufferedImage[] tileset;
private int numTilesAcross;
private Tile[][] tiles;

private Animation an;

// drawing
private int rowOffset;
private int colOffset;
private int numRowsToDraw;
private int numColsToDraw;

public TileMap(int tileSize) {
    this.tileSize = tileSize;
    numRowsToDraw = GamePanel.HEIGHT / tileSize + 2;
    numColsToDraw = GamePanel.WIDTH / tileSize + 2;
    tween = 0.07;
    an = new Animation();
}

public void loadTiles(BufferedImage[] s, int delay) {

    an.setDelay(delay);
    an.setFrames(s);

    try {

        tileset = s;

        numTilesAcross = tileset[0].getWidth() / tileSize;
        tiles = new Tile[2][numTilesAcross];

        BufferedImage[] subimage = new BufferedImage[s.length];
        for(int col = 0; col < numTilesAcross; col++) {
            subimage[0] = tileset[0].getSubimage(
                        col * tileSize,
                        0,
                        tileSize,
                        tileSize
                    );
            tiles[0][col] = new Tile(subimage, Tile.NORMAL);
            subimage[0] = tileset[0].getSubimage(
                        col * tileSize,
                        tileSize,
                        tileSize,
                        tileSize
                    );
            tiles[1][col] = new Tile(subimage, Tile.BLOCKED);
        }

    }
    catch(Exception e) {
        e.printStackTrace();
    }

}

public void loadMap(String s) {

    try {

        InputStream in = getClass().getResourceAsStream(s);
        BufferedReader br = new BufferedReader(
                    new InputStreamReader(in)
                );

        numCols = Integer.parseInt(br.readLine());
        numRows = Integer.parseInt(br.readLine());
        map = new int[numRows][numCols];
        width = numCols * tileSize;
        height = numRows * tileSize;

        xmin = GamePanel.WIDTH - width;
        xmax = 0;
        ymin = GamePanel.HEIGHT - height;
        ymax = 0;

        String delims = "\\s+";
        for(int row = 0; row < numRows; row++) {
            String line = br.readLine();
            String[] tokens = line.split(delims);
            for(int col = 0; col < numCols; col++) {
                map[row][col] = Integer.parseInt(tokens[col]);
            }
        }

    }
    catch(Exception e) {
        e.printStackTrace();
    }

}

public int getTileSize() { return tileSize; }
public double getx() { return x; }
public double gety() { return y; }
public int getWidth() { return width; }
public int getHeight() { return height; }

public int getType(int row, int col) {
    int rc = map[row][col];
    int r = rc / numTilesAcross;
    int c = rc % numTilesAcross;
    return tiles[r][c].getType();
}

public void setTween(double d) { tween = d; }

public void setPosition(double x, double y) {

    this.x += (x - this.x) * tween;
    this.y += (y - this.y) * tween;

    fixBounds();

    colOffset = (int)-this.x / tileSize;
    rowOffset = (int)-this.y / tileSize;

}

private void fixBounds() {
    if(x < xmin) x = xmin;
    if(y < ymin) y = ymin;
    if(x > xmax) x = xmax;
    if(y > ymax) y = ymax;
}

public void update() {
    an.update();
}

public void draw(Graphics2D g) {

    for(
        int row = rowOffset;
        row < rowOffset + numRowsToDraw;
        row++) {

        if(row >= numRows) break;

        for(
            int col = colOffset;
            col < colOffset + numColsToDraw;
            col++) {

            if(col >= numCols) break;

            if(map[row][col] == 0) continue;

            int rc = map[row][col];
            int r = rc / numTilesAcross;
            int c = rc % numTilesAcross;

            g.drawImage(
                tiles[r][c].getImage(0), // hard code the image at 0
                (int)x + col * tileSize,
                (int)y + row * tileSize,
                null
            );

        }

    }

}

public class Tile {

private BufferedImage[] image;
private int type;

// tile types
public static final int NORMAL = 0;
public static final int BLOCKED = 1;

public Tile(BufferedImage[] image, int type) {
    this.image = image;
    this.type = type;
}

public BufferedImage getImage(int i) { return image[i]; }
public int getType() { return type; }

1 个答案:

答案 0 :(得分:1)

对于任何想知道答案的人来说,它已经解决了。

部分

<div ng-app="eventModule" >
<div ng-controller="eventController">
<div>
<span >Thumbnail Image</span>
<input type="file" id="fileToUpload" onchange="angular.element(this).scope().selectThumbnail(this.files)"  accept="image/*"  />
</div>
<div>
<span >Large Image</span>
<input type="file" onchange="angular.element(this).scope().selectLargeImage(this.files)" class="LargeImageSubCategory" />
</div>
</div>
<span  data-ng-click="SaveFile()">Submit</span>
</div>

<script>
    var eventModule = angular.module('eventModule', []);
    eventModule.controller('eventController', function ($scope,ArticleService, $http, $sce) {
        $scope.selectThumbnail = function (file) {
            $scope.SelectedThumbnail = file[0];
        }

        $scope.selectLargeImage = function (file) {
            $scope.SelectedLargeImage = file[0];
        }

        $scope.SaveFile = function () {
            $scope.IsFormSubmitted = true;
            $scope.Message = "";
            ArticleService.UploadFile($scope.SelectedThumbnail, $scope.SelectedLargeImage).then(function (d) {
                alert(d.Message);
                ClearForm();
            }, function (e) {
                alert(e);
            });
         };
    });
    eventModule.service("ArticleService", function ($http, $q) {
        this.UploadFile = function (Thumbnail, LargeImage, TitleHeading, Topic, SmallDesc, LargeDesc) {
            var formData = new FormData();
            formData.append("Thumbnail", Thumbnail);
            formData.append("LargeImage", LargeImage);
            // here when i am trying to send two files so controller is not called
            //and function is breaking and alert is comming "File Upload Failed"
            formData.append("TitleHeading", TitleHeading);
            formData.append("Topic", Topic);
            var defer = $q.defer();
            $http.post("/Articles/SaveFiles", formData,
            {
               withCredentials: true,
               headers: { 'Content-Type': undefined },
               transformRequest: angular.identity
            }).success(function (d) {
               defer.resolve(d);
            }).error(function () {
               defer.reject("File Upload Failed!");
            });
            return defer.promise;
         }
    });
</script>

//And My ArticlesController.cs code is

[HttpPost]
public JsonResult SaveFiles(string TitleHeading, string Topic)
{
string Message, fileName, actualFileName;
Message = fileName = actualFileName = string.Empty;
bool flag = false;
if (Request.Files != null)
{
var file = Request.Files[0];
actualFileName = file.FileName;
fileName = Guid.NewGuid() + Path.GetExtension(file.FileName);
int size = file.ContentLength;
try
{
file.SaveAs(Path.Combine(Server.MapPath("~/UploadedFiles"), fileName));
using (TCDataClassesDataContext dc = new TCDataClassesDataContext())
{
Article insert = new Article();
insert.ArticleId = Guid.NewGuid();
insert.TitleHeading = TitleHeading;
insert.SmallImagePath = fileName;
dc.Articles.InsertOnSubmit(insert);
dc.SubmitChanges();
Message = "File uploaded successfully";
flag = true;
}
}
catch (Exception)
{
Message = "File upload failed! Please try again";
}}
return new JsonResult { Data = new { Message = Message, Status = flag } };
}

是罪魁祸首。感谢haraldk提供测试此问题的建议,修复方法是简单地将子图像放在循环中。不断更改内容并使用相同的数组只是将整个数组的内容存储在整个数组中,从而弄乱了它。

更新了代码示例

BufferedImage[] subimage = new BufferedImage[s.length];
    for(int col = 0; col < numTilesAcross; col++) {
        subimage[0] = tileset[0].getSubimage(
                    col * tileSize,
                    0,
                    tileSize,
                    tileSize
                );
        tiles[0][col] = new Tile(subimage, Tile.NORMAL);
        subimage[0] = tileset[0].getSubimage(
                    col * tileSize,
                    tileSize,
                    tileSize,
                    tileSize
                );
        tiles[1][col] = new Tile(subimage, Tile.BLOCKED);
    }