Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Trouble getting an image to display.

  1. #1
    Junior Member
    Join Date
    May 2011
    Posts
    8
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default 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.

    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.


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Trouble getting an image to display.

    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?

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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.

Similar Threads

  1. Display Image (png)
    By vimalaranjan in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 3rd, 2011, 06:44 PM
  2. Replies: 2
    Last Post: February 14th, 2011, 05:36 PM
  3. How to display a button with an image on it?
    By ice in forum AWT / Java Swing
    Replies: 10
    Last Post: November 13th, 2010, 06:20 AM
  4. Using JFileChooser to open and display an image
    By JavaN0ob in forum What's Wrong With My Code?
    Replies: 3
    Last Post: July 31st, 2010, 06:01 PM
  5. Pixel Alteration in an image/image rotation
    By bguy in forum Java Theory & Questions
    Replies: 3
    Last Post: November 1st, 2009, 10:50 AM