Solved

How do display a background image in Canvas?

Posted on 2013-11-28
2
260 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

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…
The purpose of this article is to demonstrate how we can use conditional statements using Python.
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.
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…

708 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

15 Experts available now in Live!

Get 1:1 Help Now