Trouble getting an image to display.
Im using an arraylist in a game for collision detection. I create an object, and put it into the arraylist. When I need to check collisions, or draw the images, I want to be able to call them through the array, not by their original instantiation. The code below is most relevant to the problem.
Code :
public class SolidObject {
//For counting the array.
private int arrayInt;
private boolean Solid;
private Image Ground1;
private boolean Collision;
private boolean CollisiontoLeft;
private boolean CollisiontoRight;
private boolean CollisiontoUp;
private boolean CollisiontoDown;
private int xPosLeft;
private int yPosUp;
private int xPosRight;
private int yPosDown;
private int gamexPos;
private Component parent;
//Holds all solid objects.
static ArrayList <SolidObject> SolidObject = new ArrayList <SolidObject>();
//Instantiates a solid object. (Don't forget to put it in the ArrayList)
public SolidObject(int X, int Y, Component parent) {
xPosLeft = X;
yPosUp = Y;
xPosRight = X + Constants.Ground1xSize;
yPosDown = Y + Constants.Ground1ySize;
gamexPos = X + Constants.Ground1xSize/2;
Solid = true;
this.parent = parent;
}
public void setSolidObjectImages(Image Ground1) {
this.Ground1 = Ground1;
//Needs to be finished.
public void paintSolidObject(Graphics gameGraphics) {
gameGraphics.drawImage(SolidObject.get(arrayInt).getImage(), SolidObject.get(arrayInt).getxPosLeft(), SolidObject.get(arrayInt).getyPosUp(), parent);
}
public int getxPosLeft() {
return xPosLeft;
}
public int getyPosUp() {
return yPosUp;
}
public Image getImage() {
return Ground1;
}
I can't get the image to draw. I'm not very familiar with array lists, so I'm not sure if i may be trying to access values inside the objects wrong or something. I instantiate a new ground object in the main class, and then add it into the arraylist with the add method. Not really sure where to go from here, or why its not working. Eclipse says theres no errors, but somethings not working right lol.
Re: Trouble getting an image to display.
Code java:
public class SolidObject {
static ArrayList <SolidObject> SolidObject = new ArrayList <SolidObject>();
This is just asking for trouble. You have given a variable the same name as the class which can only lead to confusion. Whenever you see SolidObject in the code is it referring to the class or the ArrayList. Also, having a list of objects inside the class is a bad idea in my opinion. Do you have a list of Books inside each Book you own? Or does it make more sense to have a Library class that has a List of Books?
Re: Trouble getting an image to display.
Is the paintSolidObject method being called?
In that method I'd get the object from the ArrayList and then call its methods to allow some debugging print outs:
SoldiObject thisObject = SolidObject.get(arrayInt); // get the element
System.out.println("thisObject=" + thisObject);
then refer to it:
thisObject.getImage() ... etc
Add a toString method to the class so the println shows the contents of the object.