Solved

Java Canvas - Setting a Background Image

Posted on 2013-11-28
3
1,435 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
  • 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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

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

Suggested Solutions

Windows Script Host (WSH) has been part of Windows since Windows NT4. Windows Script Host provides architecture for building dynamic scripts that consist of a core object model, scripting hosts, and scripting engines. The key components of Window…
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
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…

809 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