What's new

Can someone help me with rendering sprites? (In Java)

J

JaypaK

Enthusiast
Messages
924
Reaction score
97
I was hoping someone would be able to help me. I have a problem where I need to render a 16x16 (1 tile) sprite, but for some reason it's rendering four. I have pictures of my code and the outcome of it.

The Screen class:
screen.png


Code:
Code:
package com.jaypak.top.graphics;
 
import java.util.Random;
 
public class Screen {
 
private int width, height;
public int[] pixels;
public final int MAP_SIZE = 64;
public final int MAP_MASK_SIZE = MAP_SIZE - 1;
 
public int[] tiles = new int[MAP_SIZE * MAP_SIZE];
 
private Random random = new Random();
 
public Screen(int width, int height){
 
this.width = width;
this.height = height;
pixels = new int[width * height];
 
for (int i = 0; i < MAP_SIZE * MAP_SIZE; i++){
tiles[i] = random.nextInt(0xff);
}
 
}
 
public void clear(){
for(int i = 0; i < pixels.length; i++){
pixels[i] = 0;
}
}
public void render(int xOffset, int yOffset){
for(int y = 0; y < height; y++){
int yy = y + yOffset;
//if(yy < 0 || yy >= height) break;
for(int x = 0; x < width; x++){
int xx = x + xOffset;
//if(xx < 0 || xx >= width) break;
pixels[x + y * width] = Sprite.grass.pixels[(x & 15) + (y & 15) * Sprite.grass.SIZE];
}
}
}
 
}

The SpriteSheet class:
Spritesheet.png


Code:
Code:
package com.jaypak.top.graphics;
 
import java.awt.image.BufferedImage;
import java.io.IOException;
 
import javax.imageio.ImageIO;
 
public class SpriteSheet {
 
private String path;
public final int SIZE;
public int[] pixels;
 
public static SpriteSheet tiles = new SpriteSheet("/textures/SpriteSheet.png", 256);
 
public SpriteSheet(String path, int size){
this.path = path;
SIZE = size;
pixels = new int[SIZE * SIZE];
load();
}
 
private void load(){
try {
BufferedImage image = ImageIO.read(SpriteSheet.class.getResource(path));
int w = image.getWidth();
int h = image.getHeight();
image.getRGB(0, 0, w, h, pixels, 0, w);
} catch (IOException e) {
e.printStackTrace();
}
}
 
}

Sprite class:
Sprite.png


Code:
Code:
package com.jaypak.top.graphics;
 
public class Sprite {
 
public final int SIZE;
private int x, y;
public int[] pixels;
private SpriteSheet sheet;
 
public static Sprite grass = new Sprite(16, 0, 0, SpriteSheet.tiles);
 
public Sprite(int size, int x, int y, SpriteSheet sheet){
SIZE = size;
pixels = new int[SIZE * SIZE];
this.x = x * size;
this.y = y * size;
this.sheet = sheet;
load();
}
 
private void load(){
for(y = 0; y < SIZE; y++){
for(x = 0; x < SIZE; x++){
pixels[x + y * SIZE] = sheet.pixels[(x + this.x) + (y + this.y) * sheet.SIZE];
}
}
}
 
}

The outcome:

tiles.png


It looks like it renders 4 tiles instead of one, and I don't know why.
 
Z

Zerker24

Enthusiast
Messages
945
Reaction score
206
Can you post the SpriteSheet image? I don't quite understand the issue. I am thinking your spritesheet might have 4 tiles in it, and it is rendering the entire image. If you send me your sprite I should be able to figure out the issue.
 
X

Xeren

♦♦♦ God Complex ♦♦♦
Messages
5,671
Reaction score
2,120
I'm thinking it has something to do with your sprite picture, as I do not see anything wrong with the code given.
 
Top Bottom