Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Java Canvas - Setting a Background Image

Posted on 2013-11-28
3
Medium Priority
?
1,680 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
Having just graduated from college and entered the workforce, I don’t find myself always using the tools and programs I grew accustomed to over the past four years. However, there is one program I continually find myself reverting back to…R.   So …
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 tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
Suggested Courses

610 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