?
Solved

Java Canvas - Setting a Background Image

Posted on 2013-11-28
3
Medium Priority
?
1,594 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 2000 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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

Artificial Intelligence comes in many forms, and for game developers, Path-Finding is an important ability for making an NPC (Non-Playable Character) maneuver through terrain.  A* is a particularly easy way to approach it.  I’ll start with the algor…
As game developers, we quickly learn that Artificial Intelligence (AI) doesn’t need to be so tough.  To reference Space Ghost: “Moltar, I have a giant brain that is able to reduce any complex machine into a simple yes or no answer. (http://www.youtu…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
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.
Suggested Courses

770 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