Solved

How do display a background image in Canvas?

Posted on 2013-11-28
2
261 Views
Last Modified: 2013-11-29
How do I get the background image to display in my Canvas playArea?


// 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 Comments
 
LVL 8

Accepted Solution

by:
Mohit Vijay earned 500 total points
ID: 39684121
use css or style with backgroup-image: [path]
0
 

Author Comment

by:mfinocc
ID: 39685245
This is an eclipse java application, not a web app.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
This video teaches viewers about errors in exception handling.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

910 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

24 Experts available now in Live!

Get 1:1 Help Now