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 6 of 6

Thread: Slick2D/LWJGL - How do you set a background?

  1. #1
    Member Cronus's Avatar
    Join Date
    Feb 2013
    Location
    Gothenburg, Sweden
    Posts
    41
    My Mood
    Inspired
    Thanks
    6
    Thanked 7 Times in 6 Posts

    Question Slick2D/LWJGL - How do you set a background?

    Hello, I'm learning some good ol' game development and I came across the need to change the boring black background to a static image. The only problem is that the background overlaps all other images/labels/else and is the only thing visible. Here is my "render" of the class/state called "Menu.java" (Slightly modified to simplify the reading):

     
            @Override
    	public void render(GameContainer gameContainer, StateBasedGame stateBasedGame, Graphics g) throws SlickException {		
     
    		Image background = new Image("res/MenuBG.png"); //The background
    		g.drawImage(background, 0, 0, null); //Rendering the background
     
    		g.drawString(VariableNameHere, 360, 10); //A string displayed on the screen
    		g.drawString(AnotherVariableHere, 10, 450); //Another one
     
    		g.drawRect(500, 150, 75, 225); //A rectangle shape 
     
    		g.drawImage(HereIsAnImage, 500, 150); //An image
     
    		g.drawString(YetAnotherVariable, 100, 175); //A string
     
    		g.drawString(GuessWhatThisIs, 100, 275); //Yet again, a string
     
    	}

    The only thing visible is the background, everything else is ignored, why is that so!?

    When I'm done with learning game development, I will hopefully publish a thread with source code for classic games like Pong, Snake, and Pacman.


  2. #2
    Member
    Join Date
    May 2011
    Location
    west palm beach, FL
    Posts
    189
    My Mood
    Tired
    Thanks
    41
    Thanked 11 Times in 10 Posts

    Default Re: Slick2D/LWJGL - How do you set a background?

    you should check out these tutorials
    https://www.youtube.com/watch?v=AXND...0C2267A8922854

    he teaches pretty good and its easy to follow...if you do all 12 videos youll know how to put a map and character and move around without going off the screen etc

  3. #3
    Member Cronus's Avatar
    Join Date
    Feb 2013
    Location
    Gothenburg, Sweden
    Posts
    41
    My Mood
    Inspired
    Thanks
    6
    Thanked 7 Times in 6 Posts

    Default Re: Slick2D/LWJGL - How do you set a background?

    Ha! I'm actually following his tutorials. I want to add a background behind all other rendered objects though...

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Slick2D/LWJGL - How do you set a background?

    You shouldn't initialize the Image every time render() is called. Initialize it once, then use the same instance over and over again.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Member Cronus's Avatar
    Join Date
    Feb 2013
    Location
    Gothenburg, Sweden
    Posts
    41
    My Mood
    Inspired
    Thanks
    6
    Thanked 7 Times in 6 Posts

    Default Re: Slick2D/LWJGL - How do you set a background?

    Could you express yourself in code? Thanks for replying!

  6. #6
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Slick2D/LWJGL - How do you set a background?

    Quote Originally Posted by Cronus View Post
    Could you express yourself in code? Thanks for replying!
    What part of my reply is confusing?

    The render() method can be called many times a second. Creating a new Image every time is expensive and unnecessary. Create your background image instance before the render() function, exactly like you do with the other images.

    If you want help, you'll have to provide an SSCCE that demonstrates what you're actually doing. Otherwise we're just guessing.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Java Game Programming Tutorials - Slick2D library
    By worsewicked in forum Java Programming Tutorials
    Replies: 2
    Last Post: June 14th, 2013, 11:58 AM
  2. slick2d problem getting an error
    By derekxec in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 20th, 2013, 07:42 PM
  3. Replies: 2
    Last Post: October 31st, 2012, 01:07 AM
  4. set Background Excel cell color using POI API
    By ngamal in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 26th, 2011, 11:20 AM
  5. JButton set background problem
    By ellias2007 in forum AWT / Java Swing
    Replies: 1
    Last Post: February 25th, 2010, 12:15 AM

Tags for this Thread