I've been trying to create a sprite to be used on our java game. Everything seems normal, i see no errors on my program but suddenly when it's to be run an error appeared that it Cannot be Instantiated. Can somebody tell me what's wrong with it?
#SuppressWarnings("unused")
public class TrueSprite
{
BufferedImage spriteSheet = ImageIO.read(new File("robin.png"));
int width = 240, height = 314, rows = 5 , columns = 5;
BufferedImage[] sprites = new BufferedImage[rows * columns];
public TrueSprite(int width, int height, int rows, int columns) throws IOException
{
this.width = width;
this.height = height;
this.rows = rows;
this.columns = columns;
for(int i = 0; i < rows; i++)
{
for(int j = 0; j < columns; j++)
{
sprites[(i * columns) + j ] = spriteSheet.getSubimage(i * width, j * height, width, height);
}
}
}
public void paint(Graphics g)
{
g.drawImage(sprites[1], 100, 100, null);
}
}
here's the error:
load: really.sprite.TrueSprite.class can't be instantiated.
java.lang.InstantiationException: really.sprite.TrueSprite
at java.lang.Class.newInstance(Unknown Source)
at sun.applet.AppletPanel.createApplet(Unknown Source)
at sun.applet.AppletPanel.runLoader(Unknown Source)
at sun.applet.AppletPanel.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
First of all, you have no constructor, which is essential for your class to be instantiated. Put this inside your class declaration:
public TrueSprite() {
//code to run when your class is instantiated.
}
Now, this won't do anything special, but you can call it with:
TrueSprite sprite = new TrueSprite();
Additionally, your class needs to be cleaned up. Try putting your assignment code inside the constructor we just built, and the declaration to these variables outside of it:
#SuppressWarnings("unused")
public class TrueSprite
{
private final int width;
private final int height;
private final int rows;
private final int cols;
private BufferedImage bigImg;
private BufferedImage[] sprites;
public TrueSprite(int width, int height, int rows, int columns) {
this.width = width;
this.height = height;
this.rows = rows;
this.cols = columns;
this.bigImg = ImageIO.read(new File("robin.png"));
this.sprites = new BufferedImage[rows * cols];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
sprites[(i * cols) + j] = bigImg.getSubimage(j * width, i * height, width, height);
}
}
}
public void paint(Graphics g)
{
g.drawImage(sprites[1], 100, 100, null);
}
}
Just make sure you pass in four valid arguments into your TrueSprite constructor to call it correctly:
TrueSprite sprite = new TrueSprite(200, 500, 20, 50);
I think you need to create a non param constructor like
public TrueSprite() throws IOException
Your BufferedImage[] sprites = new BufferedImage[rows * columns]; outside the constructor initialised the sprites array with a size of 5*5=25. If you created a TrueSprit with a higher row or column, e.g. TrueSprite(100,100,100,100), the entries after sprites[24] will not be instantiated yet.
You should always place your initialisation inside the constructor. i.e.
private BufferedImage[] sprites;
public TrueSprite(int width, int height, int rows, int columns) throws IOException
{
this.width = width;
this.height = height;
this.rows = rows;
this.columns = columns;
this.sprites = new BufferedImage[rows * columns];
for(int i = 0; i < rows; i++)
{
for(int j = 0; j < columns; j++)
{
sprites[(i * columns) + j ] = spriteSheet.getSubimage(i * width, j * height, width, height);
}
}
}
This will set the rows and columns to the correct value before creating your array with the values. Furthermore, you should have used a default constructor if you wanted to have default values. i.e.
public TrueSprite() throws IOException
{
this.width = 240;
this.height = 314;
this.rows = 5;
this.columns = 5;
this.sprites = new BufferedImage[rows * columns];
for(int i = 0; i < rows; i++)
{
for(int j = 0; j < columns; j++)
{
sprites[(i * columns) + j ] = spriteSheet.getSubimage(i * width, j * height, width, height);
}
}
}
Related
I've run this code this in the newest version of Eclipse but in that, it did not specify this error message but did point at the pixels[x + y * width] = Sprite.grass.pixels[(x & 15) + (y & 15) *Sprite.grass.SIZE]; line of the code. So I didn't know what was the error so I ran it on an older version of the IDE, Kepler. In the IDE it gave this error message. I searched a lot on stackoverflow, there were several cases where people got the error message but most of them were related to importing android.R or something of that sort. But I'm not doing this for any android application, this is just for a normal project. So none of the solutions matched my problem.
The error message is pointing towards pixels and SIZE in Sprite class. If you need the SpriteSheet class code too let me know. There I was trying to load an image. This is all I can explain for now. So could anyone help me out, please?
package com.thecherno.rain.graphics;
import java.util.Random;
public class Screen {
private int width, height;
public int[] pixels;
final int MAP_SIZE = 64;
final int MAP_SIZE_INT = MAP_SIZE - 1;
public int[] tiles = new int[MAP_SIZE * MAP_SIZE];
public 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(0xffffff);
tiles[0] = 0;
}
}
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++) {
for(int x = 0; x < width; x++) {
pixels[x + y * width] = Sprite.grass.pixels[(x & 15) + (y & 15) *Sprite.grass.SIZE];
}
}
}
}
package com.thecherno.rain.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(int y = 0; y < SIZE; y++) {
for(int x = 0; x < SIZE; x++) {
pixels[x + y * SIZE] = sheet.pixels[(x + this.x) + (y + this.y) * sheet.SIZE];
}
}
}
}
my Task is to make an implementation of Conway's Game of Life. Therefor I need to create the class GameMap. In this class I will initialize an 2D Array.
Therefor I use those two methods.
private static Cell[][] buildCellArray(int width, int height){
Cell[][] cellArray = new Cell[width][height];
int i;
int j;
for(i = 0; i < width; i++) {
for(j = 0; j < height; j++) {
cellArray[i][j] = new Cell();
}
}
return cellArray;
}
public GameMap(int sizeX, int sizeY) {
buildCellArray(sizeX, sizeY);
}
Now I want to access the cellArray to access a special Cell with the getCell(int posX, int posY) method.
My question is how I can access the cellArray?
I wanted to access it like this:
public Cell getCell(int posX, int posY){
return cellArray[posX][posY];
}
So that I get the Cell at a special position.
I hope somebody can help me out.
So the complete code part is:
public class GameMap {
private static Cell[][] buildCellArray(int width, int height){
Cell[][] cellArray = new Cell[width][height];
int i;
int j;
for(i = 0; i < width; i++) {
for(j = 0; j < height; j++) {
cellArray[i][j] = new Cell();
}
}
return cellArray;
}
public GameMap(int sizeX, int sizeY) {
buildCellArray(sizeX, sizeY);
}
public Cell getCell(int posX, int posY){
return cellArray[posX][posY];
}
}
And the IDE says that cellArray in the method getCell is not a variable.
The IDE says cellArray cannot be resolve to a variable because it is local variable, to pass this problem just move Cell[][] cellArray = new Cell[width][height]; outside the buildCellArray().
public class GameMap {
Cell[][] cellArray;
private static Cell[][] buildCellArray(int width, int height){
int i;
int j;
for(i = 0; i < width; i++) {
for(j = 0; j < height; j++) {
cellArray[i][j] = new Cell();
}
}
return cellArray;
}
public GameMap(int sizeX, int sizeY) {
buildCellArray(sizeX, sizeY);
}
public Cell getCell(int posX, int posY){
return cellArray[posX][posY];
}
}
I have a class to separate and join image in tiles. It works fine when the sides of the tile correspond to the dimensions of the image, i.e. height 250, tile height 25. But when it's not it doesn't create smaller tiles at the borders as it should.
Where would be the problem to properly create the border tiles smaller than the rest?
Constructor:
public EdgeBufferedImage(BufferedImage image, int w, int h){
this.sourceImg = image;
this.setCol((int)Math.ceil(image.getWidth()/(double)w));
this.setRow((int)Math.ceil(image.getHeight()/(double)h));
this.setWidth(image.getWidth());
this.setHeight(image.getHeight());
this.setTilew(w);
this.setTileh(h);
this.setMatImg(new BufferedImage[row][col]);
}
Methods:
Image tiling
public void createSmallImages() {
int rows = getRow();
int columns = getCol();
int smallWidth = getTilew();
int smallHeight = getTileh();
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
if (j == columns - 1) smallWidth = getWidth() - (getTilew() * j);
if (i == rows - 1) smallHeight = getHeight() - (getTileh() * i);
matImg[i][j] = getSourceImg().getSubimage(j * smallWidth, i
* smallHeight, smallWidth, smallHeight);
}
smallWidth = getTilew();
smallHeight = getTileh();
}
}
Image joining
public void joinTiles(){
int rows = getRow();
int columns = getCol();
int smallWidth = getTilew();
int smallHeight = getTileh();
BufferedImage comb = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_RGB);
Graphics2D g = (Graphics2D) comb.getGraphics();
g.setColor(Color.RED);
for (int row = 0; row < rows; row++){
for (int col = 0; col < columns; col++){
BufferedImage piece = getMatImg()[row][col];
if (col == columns - 1) smallWidth = getWidth() - (getTilew() * col);
if (row == rows - 1) smallHeight = getHeight() - (getTileh() * row);
g.drawImage(piece, col * smallWidth, row * smallHeight, smallWidth, smallHeight, null);
g.drawRect(col * smallWidth, row * smallHeight, smallWidth, smallHeight);
}
smallWidth = getTilew();
smallHeight = getTileh();
}
g.dispose();
setSourceImg(comb);
}
Original image is 512*512
So first I have this class:
public float getPixel(int height, int width)
{
return data[height][width];
}
public void setPixel(float value, int height, int width)
{
if (value > getMax())
value = getMax();
if (value < 0)
value = 0;
data[height][width] = value;
}
private Image(String magicNumber, int height, int width, float max) {
this.magicNumber = magicNumber;
this.width = width;
this.height = height;
this.max = max;
data = new float[height][width];
}
...
public Image clone()
{
Image clone = new Image(getMagicNumber(), getHeight(), getWidth(), getMax());
for (int i = 0; i < getHeight(); i++)
{
for (int j = 0; j < getWidth(); j++)
{
clone.setPixel(getPixel(i, j), i, j);
}
}
return clone;
}
And then this class:
public class Filter {
public Filter() {
}
public Image linearFilter(Image image, float[][] kernel)
{
Image filtered = image.clone();
for (int i = 0; i < getHeight(); i++)
{ /* cannot resolve getHeight*/
...
}
return filtered;
}
}
I have two questions:
1) Why do I don't need to create an instance of the class Image. Here I can already use filtered.setPixels...
2) How do I fix the Problem with "cannot resolve method"?
I can't really tell from your post because the first snippet you've posted doesn't seem to be an entire class but rather a portion of it. I assume you have an Image class, and that Image class has a method called getHeight().
Inside the for loop condition for (int i = 0; i < getHeight(); i++), you'll most likely want to change getHeight() to filtered.getHeight() because getHeight() is a method inside of the Image class, and filtered is (presumably) of type Image.
I've been trying to create a sprite for our game but the problem is when i tried to run the program this error appears
Exception in thread "main" java.awt.image.RasterFormatException: (y + height) is outside of Raster
at sun.awt.image.ByteInterleavedRaster.createWritableChild(Unknown Source)
at java.awt.image.BufferedImage.getSubimage(Unknown Source)
at really.sprite.TrueSprite.<init>(TrueSprite.java:31)
at really.sprite.Sprite.main(Sprite.java:13)
here's the program that causes the error:
public class TrueSprite
{
BufferedImage spriteSheet = ImageIO.read(new File("resources/robin.png"));
int width = 240, height = 314, rows = 5 , columns = 5;
BufferedImage[] sprites = new BufferedImage[rows * columns];
public TrueSprite(int width, int height, int rows, int columns) throws IOException
{
this.width = width;
this.height = height;
this.rows = rows;
this.columns = columns;
for(int i = 0; i < rows; i++)
{
for(int j = 0; j < columns; j++)
{
sprites[(i * columns) + j ] = spriteSheet.getSubimage(i * width, j * height, width, height);
}
}
}
public void paint(Graphics g)
{
g.drawImage(sprites[1], 314, 240, null);
}
}
the image size I imported is 1200 x 1570.