Solved

Java Canvas - Setting a Background Image

Posted on 2013-11-28
3
1,495 Views
Last Modified: 2013-11-29
How do I get the background image to display?


// Set up the game window
      private void configureGUI()
      {
            try
            {
                  Image img = ImageIO.read(new File("C:/Java/background.jpg"));
            }
            catch (IOException e)
            {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
            }
            
            // Create the window object
            frame = new JFrame("Comets");
            frame.setSize(playWidth+20, playHeight+35);
            frame.setResizable(false);
                        
            // The program should end when the window is closed
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            
            // Set the window's layout manager
            frame.setLayout(new FlowLayout());
            
            // Create the play area
            playArea = new Canvas();
            playArea.setSize(playWidth, playHeight);
            playArea.setBackground(Color.BLACK);
            playArea.setFocusable(false);
            frame.add(playArea);
            
            // Make the frame listen to keystrokes
            frame.addKeyListener(this);
      }
0
Comment
Question by:mfinocc
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 16

Expert Comment

by:imladris
ID: 39683778
You need to create a class derived from Canvas and paint on it. Something like:

class PlayCanvas extends Canvas
{   Image background;

     PlayCanvas(Image bkg)
     {   background=bkg;
         return;
     }

     public void paint(Graphics g)
     {   g.drawImage(background,0,0,null);
         return;
     }
}

Then, in configureUI, you need to use it:

     playArea=new PlayCanvas(img);

The paint method can be expanded to show anything else you need to show as well.
0
 

Author Comment

by:mfinocc
ID: 39683798
Where does C:/Java/background.jpg go in the above block?
0
 
LVL 16

Accepted Solution

by:
imladris earned 500 total points
ID: 39685641
If I'm understanding the question right, it doesn't change. You're still initializing the variable img in configureUI. Then img is passed into the PlayCanvas class when it is constructed so that it can be used in paint.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Help with my python script 6 178
groovy example issue 10 113
Is there a canned solution for a custom genealogy website 2 115
Interview question Javascript, database 12 109
What is RenderMan: RenderMan is a not any particular piece of software. RenderMan is an industry standard, defining set of rules that any rendering software should use, to be RenderMan-compliant. Pixar's RenderMan is a flagship implementation of …
If you haven’t already, I encourage you to read the first article (http://www.experts-exchange.com/articles/18680/An-Introduction-to-R-Programming-and-R-Studio.html) in my series to gain a basic foundation of R and R Studio.  You will also find the …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question