Results 1 to 2 of 2
  1. #1
    tony_stark is offline Member
    Join Date
    May 2011
    Posts
    1
    Rep Power
    0

    Default use image for background

    Hello first time in this forum.

    I have this project and one the tasks is to use the Programs>Set background... menuItem to set an image as the background of the main window. The problem is that when i click a menu from the menubar or i minimize the main window the image dissapeares (partialiy when i click a menu and entirely when i minimize). What kind of updating i need to keep the image for background?

    Please help !

    MainWindow.java

    Java Code:
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.KeyEvent;
    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.io.IOException;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import javax.imageio.ImageIO;
    import javax.swing.JFileChooser;
    import javax.swing.JFrame;
    import javax.swing.JMenu;
    import javax.swing.JMenuBar;
    import javax.swing.JMenuItem;
    import javax.swing.JPanel;
    import javax.swing.WindowConstants;
    
    public class MainWindow extends JFrame implements ActionListener{
        
        public static final int S = 500;
        
        public MainWindow () {
        
            setSize(S,S);
            setTitle("Whatever");
            setResizable(false);
                                                    
            setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
            
            JMenu programMenu = new JMenu("Program");
            programMenu.setMnemonic(KeyEvent.VK_P);
                    
            JMenuItem setBack = new JMenuItem("Set background...");
            setBack.setMnemonic(KeyEvent.VK_B);
            setBack.addActionListener(this);
            
            programMenu.add(setBack);
            
            JMenuBar mbar = new JMenuBar();
            mbar.add(programMenu);
    
            
            setJMenuBar(mbar);
            
        }
    
        @Override
        public void actionPerformed(ActionEvent e) {
    
            String actionCommand = e.getActionCommand();
            
            if (actionCommand.equals("Set background...")) {
            
                JFileChooser fc = new JFileChooser();
                fc.addChoosableFileFilter(new ImageFilter());
                int returnVal = fc.showOpenDialog(this);
                
                if (returnVal == JFileChooser.APPROVE_OPTION) {
                    
                    File file = fc.getSelectedFile();
                    
                    try {
                        
                         img = ImageIO.read(file);
                         getContentPane().getGraphics().drawImage(img,0,0,null);
                        
                    } catch (IOException ex) {
                        Logger.getLogger(MainWindow.class.getName()).log(Level.SEVERE, null, ex);
                    }                
    
                }
            
            }
           
        }
        
    }
    MainClass.java

    Java Code:
    public class MainClass {
    
        public static void main(String[] args) {
            
            MainWindow mainWindow = new MainWindow();
            mainWindow.setVisible(true);
    
        }
    }
    Last edited by tony_stark; 05-31-2011 at 08:16 PM.

  2. #2
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default

    Never use getGraphics to do any custom painting.

    To display an image use an Icon on a JLabel or do custom painting on a JPanel. See Background Panel Java Tips Weblog for and example of both approaches.

Similar Threads

  1. [Help] W/ background image
    By gundum584 in forum New To Java
    Replies: 9
    Last Post: 01-10-2011, 05:48 AM
  2. JApplet background image
    By footballHunter in forum New To Java
    Replies: 1
    Last Post: 11-24-2010, 02:46 AM
  3. Need help with JFrame background image
    By ProGenius in forum New To Java
    Replies: 6
    Last Post: 12-27-2009, 04:17 PM
  4. Background image
    By leiferouis in forum New To Java
    Replies: 9
    Last Post: 03-08-2009, 05:49 PM
  5. Image as background
    By Java.child in forum AWT / Swing
    Replies: 2
    Last Post: 10-02-2008, 11:37 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •