How do I fix "cannot resolve method"? - java

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.

Related

How can I access the Cell Array in my getCell method? (Java)

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];
}
}

Java - counting the occurrences of a character in a random generator method using a separate method

I'm trying to figure out how to count the number of stars that were printed last time the print() method was used.
I'm confused on how to take the value of starsInLastPrint variable into the starsInLastPrint() method. My understanding is that this isn't possible. I assume there are plenty of things wrong with my current code that isn't helping. Below is my current state as I am stuck.
import java.util.Random;
public class NightSky {
private double density;
private int width;
private int height;
private int starsInLastPrint;
public NightSky(double density) {
width = 20;
height = 10;
this.density = density;
}
public NightSky(int width, int height) {
density = 0.1;
this.width = width;
this.height = height;
}
public NightSky(double density, int width, int height) {
this.density = density;
this.width = width;
this.height = height;
}
public void printLine() {
Random starPlacement = new Random();
String[] stars = new String[(this.width)];
for (int i = 0; i < this.width; i++) {
double random = starPlacement.nextDouble();
if (random <= this.density) {
stars[i] = "*";
this.starsInLastPrint++;
} else {
stars[i] = " ";
}
}
int j = 0;
while (j < stars.length) {
System.out.print(stars[j]);
j++;
}
System.out.println("");
}
public void print() {
NightSky nightSky = new NightSky(this.density, this.width, this.height);
this.starsInLastPrint = 0;
int i = 0;
while (i < this.height) {
nightSky.printLine();
i++;
}
}
public int starsInLastPrint() {
return this.starsInLastPrint;
}
}
You are on the right track. Though, you don't need to instantiate another NightSky object inside the print method. You can just do the following,
public void print() {
this.starsInLastPrint = 0;
int i=0;
while (i < this.height) {
printLine();
i++;
}
}
So, everytime you call print, it will update the stars count for that print method call. Here is the whole code,
import java.util.Random;
public class NightSky {
private double density;
private int width;
private int height;
private int starsInLastPrint;
public static void main(String[] args){
NightSky sky = new NightSky(5,5);
sky.print();
System.out.println(sky.starsInLastPrint());
sky.print();
System.out.println(sky.starsInLastPrint());
}
public NightSky(double density) {
width = 20;
height = 10;
this.density = density;
}
public NightSky(int width, int height) {
density = 0.1;
this.width = width;
this.height = height;
}
public NightSky(double density, int width, int height) {
this.density = density;
this.width = width;
this.height = height;
}
public void printLine() {
Random starPlacement = new Random();
String[] stars = new String[(this.width)];
for (int i = 0; i < this.width; i++) {
double random = starPlacement.nextDouble();
if (random <= this.density) {
stars[i] = "*";
this.starsInLastPrint++;
} else {
stars[i] = " ";
}
}
int j = 0;
while (j < stars.length) {
System.out.print(stars[j]);
j++;
}
System.out.println("");
}
public void print() {
this.starsInLastPrint = 0;
int i=0;
while (i < this.height) {
printLine();
i++;
}
}
public int starsInLastPrint() {
return this.starsInLastPrint;
}
}
Sample run of the above code:
* *
*
*
4
0

BufferedImage.getRGB does not return array

I'm using the method BufferedImage.getRGB(...) to get an array of int's from a BufferedImage, but when I try to access those int's in the array, I get an ArrayIndexOutOfBounds Exception. The int array I want to store those values in, seems to have length 0, even if it looks like the image is correctly loaded. Where am I doing wrong?
Here's the code:
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;
public class SpriteSheet {
public String path;
public int width, height;
public int[] pixels;
public SpriteSheet(String path){
BufferedImage image = null;
try {
image = ImageIO.read(SpriteSheet.class.getResourceAsStream(path));
} catch (IOException e) {
e.printStackTrace();
}
if (image == null){
return;
}
this.path = path;
this.width = image.getWidth();
this.width = image.getHeight();
pixels = image.getRGB(0, 0, width, height, pixels, 0, width);
for (int i = 0; i < pixels.length; i++){
pixels[i] = (pixels[i] & 0xff)/64; // remove alpha channel
}
for (int i = 0; i < 8; i++){
System.out.println(pixels[i]);
}
}
}
The ArrayIndexOutOfBounds is thrown in the last for cycle, when I try to display the value of the int's.
Change
this.width = image.getWidth();
this.width = image.getHeight();
To
this.width = image.getWidth();
this.height = image.getHeight(); // set height properly
Change
for (int i = 0; i < 8; i++) {
To
for (int i = 0; i < pixels.length; i++) {

Java both if() and else if() statements are being called

Okay, so I'm in the progress of making a game, and I need the collisions to work. I have an if() { } else if() {} -statement, but BOTH of them are being called. Here is my code:
Inside my Player Class:
public Rectangle[] tileRect;
public void update() {
tileRect = new Rectangle[Level1.tiles.size()];
for (int w = 0; w < Level1.tiles.size(); w++) {
Tile m = (Tile) Level1.tiles.get(w);
tileRect[w] = m.getBounds();
if(tileRect[w].intersects(getRect())) {
System.out.println("intersecting");
dy = 0;
} else if (!tileRect[w].intersects(getRect())){
dy = 4;
}
}
x += dx;
y += dy;
}
public Rectangle getRect() {
return new Rectangle(x, y, 32, 32);
}
Here is my Tile class (the Level1.tiles is an arrayList of tiles):
package level;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
public class Tile extends Rectangle {
// Defines the id of the tile, used for setting textures.
private static final long serialVersionUID = 1L;
public int id;
// Location
public int x;
public int y;
// Variables for the terrain sprite sheet.
public BufferedImage image;
public String imageLocation;
public BufferedImage[] sprite;
public int rows = 16;
public int collumns = 16;
public int width = 16;
public int height = 16;
public Tile(int idVal, int xPos, int yPos) {
x = xPos * 32;
y = yPos * 32;
id = idVal;
setBounds(x, y, 32, 32);
createImages();
}
public BufferedImage getImage() {
return sprite[id];
}
public void createImages() {
imageLocation = "res/tile/terrain.png";
try {
image = ImageIO.read(new File(imageLocation));
} catch (IOException e) {
System.out.println("Unable to find file " + imageLocation
+ ", printing stack trace!");
e.printStackTrace();
}
sprite = new BufferedImage[rows * collumns];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < collumns; j++) {
sprite[(i * collumns) + j] = image.getSubimage(j * width, i
* height, width, height);
}
}
}
public int getXLoc() {
return x;
}
public int getYLoc() {
return y;
}
public void setX(int xPos) {
x = xPos;
}
public void setY(int yPos) {
y = yPos;
}
}
I'm getting the "intersecting" message in the console, but the player is still falling down (because dy = 4). Please help! I've been trying to solve this all morning...
If and Else cannot both be caught at the same time.
Looks like you are looping through, seeing an intersection, then continuing to loop through regardless.
Try adding a break command to your for loop.
if(tileRect[w].intersects(getRect())) {
System.out.println("intersecting");
dy = 0;
break;
} else if (!tileRect[w].intersects(getRect())){
dy = 4;
}
The break will stop your for loop from continuing and you will exit the loop with dy = 0; rather than going onto the next tile and changing it back to dy = 4;
if and else if cannot be called in the same run. If if condition is true, then any else is never run (not even checked). It's probably different runs. Debug or put a log to see the flow.
Also
if(tileRect[w].intersects(getRect())) {
System.out.println("intersecting");
dy = 0;
} else if (!tileRect[w].intersects(getRect())){
dy = 4;
}
is much simpler written as
if(tileRect[w].intersects(getRect())) {
System.out.println("intersecting");
dy = 0;
} else {
dy = 4;
}

Cannot be instantiated

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);
}
}
}

Categories

Resources