Solved

Java Canvas - Setting a Background Image

Posted on 2013-11-28
3
1,346 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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

There is an easy way, in .NET, to centralize the treatment of all unexpected errors. First of all, instead of launching the application directly in a Form, you need first to write a Sub called Main, in a module. Then, set the Startup Object to th…
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.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
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.

707 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now