File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Game Development and the fly likes Background image for text based RPG Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Game Development
Bookmark "Background image for text based RPG" Watch "Background image for text based RPG" New topic
Author

Background image for text based RPG

Richard Henderson
Ranch Hand

Joined: Feb 18, 2013
Posts: 42
I've been working on this on and off for a while and have quite a bit of the back end mechanics working but I'm still struggling with this hurdle.

I want to display a background image (an old scroll) in my j frame. Then add text over the top to display the character's stats. The frame includes a text box to display descriptive text as each area loads and a j panel which will hopefully display a simple 2d image of the current area too. (that is a whole 'nother issue for later)

I've tried to use a couple of methods to paint the image and my code has become a bit messy now...


One method I used actually painted the image on the frame but it covered up all the labels and text fields i'd put in. The other method threw no errors but didn't paint.

So any advice about how I should be doing this would be very welcome. And if any of my other code would help I can post it.
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 9990
    
    7

You are on the right track.
Check out https://www.coderanch.com/how-to/java/BackgroundImageOnJPanel

In pseudo code
1) Have a custom panel with the background image
2) Set it as your jframe's content pane
3) Ensure all other child containers (read panels) you add to the content pane are transparent. Check out JPanel.setOpaque()


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Richard Henderson
Ranch Hand

Joined: Feb 18, 2013
Posts: 42
Thanks for the quick reply.

I've just tried to implement that code you linked but it's got errors here (lines 1 and 2): Line 1 errors say "modifier protected not allowed here" and "annotation type not applicable to this kind of declaration"
Line 2 error says " ';' expected", which usually means I haven't closed something properly earlier but i'm pretty sure I got this verbatim.

So when I get that sorted out, then I make sure my main frame includes it's own background object and when it initialises it should paint in the background. Right?
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 9990
    
    7

Yup.
Looks like you missed out the closing brace after line 7. Also you need to make sure your class extends JPanel. You might have missed out on that one too which would give rise to line 1 error
Richard Henderson
Ranch Hand

Joined: Feb 18, 2013
Posts: 42
I think I've got those both covered, or i'm going blind...
Time for the whole class now:
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 9990
    
    7

Your paintComponent is inside the constructor which starts at line 4 and ends at 21!
The paintComponent is a method which should be outside the constructor.

Just in case you do not know the difference
http://docs.oracle.com/javase/tutorial/java/javaOO/constructors.html
http://docs.oracle.com/javase/tutorial/java/javaOO/methods.html

Typically the structure should be


Richard Henderson
Ranch Hand

Joined: Feb 18, 2013
Posts: 42
Ah ha! Thank you.

The lay out of the code in the link is different to what I'm used to (As you can see from my code) and I didn't see that closing brace. So I was going blind but looking in the wrong place too.

Now to see if I can make it work before I post again.
Richard Henderson
Ranch Hand

Joined: Feb 18, 2013
Posts: 42
Didn't see your edit till now but it's ok, I know the difference between constructors and methods.

So the background is now displaying correctly with this GUI constructor:
And once I've made my other components transparent I can move on to new problems!

Like how to make those components transparent.
txtOutput.setOpaque(false);
I would have thought but isn't working yet...
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 9990
    
    7

What is txtOutput? JTextField? JPanel? Anything else?
Richard Henderson
Ranch Hand

Joined: Feb 18, 2013
Posts: 42
Sorry.

It's actually a JTextArea.

I'm also wondering why the JOptionPane that I added isn't staying invisible until it's called like in the last WIP version of this game?
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 9990
    
    7

Please UseOneThreadPerQuestion
You can start a separate topic for your other question.

I ran this code and it works when you comment in the call to set opaque.

Post your SSCCE so we can see whats going wrong.
Richard Henderson
Ranch Hand

Joined: Feb 18, 2013
Posts: 42
Might take a while to get that sorted. Like maybe tomorrow.

Should this example actually open a gui window when run or just have all the code i'm using? I've mostly been using the "design" tab of NetBeans to set the parameters of the components and the code enters the uneditable section.
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 9990
    
    7

Just save locally and run it as is.
Notice the call to
It will give you a red background, instead of the custom image background like you have done.
Richard Henderson
Ranch Hand

Joined: Feb 18, 2013
Posts: 42
I ran that code too, it does work fine.

It's a lot more concise than what I have and I can't figure out how to make an SSCCE while retaining the issue I have. I seem to need three classes to do it.
Richard Henderson
Ranch Hand

Joined: Feb 18, 2013
Posts: 42
This is what I've got but it's not working...



Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 9990
    
    7

Recommended reading http://docs.oracle.com/javase/tutorial/uiswing/components/frame.html Check out how to add components to a JFrame and how to use layout managers
Richard Henderson
Ranch Hand

Joined: Feb 18, 2013
Posts: 42
Thanks for the help Maneesh.

I've been working through the stuff in the link and have fixed most of my issues. It seems the opaqueness issue was cause by the generated code form the Netbeans drag and drop components. I've used setLayout(null) and will manually add and position my labels and other components. The Frame is setResizable(false) so it wont get messed up later.

Unfortunately I can't get my textArea to display and function the scrollPane properly now. I'll just spend a while longer trying to figure it out and I'll post a new thread to get some ideas how to run the MVC.
 
Consider Paul's rocket mass heater.
 
subject: Background image for text based RPG
 
Similar Threads
Setting an image as a background of a JFrame
Background Image not showing
Image does not appear?
Scale a BufferedImage. (Solved)
How to set image as background of JTree component