Results 1 to 2 of 2
Thread: use image for background
- 05-31-2011, 08:13 PM #1
Member
- Join Date
- May 2011
- Posts
- 1
- Rep Power
- 0
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
MainClass.javaJava 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); } } } } }
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.
- 05-31-2011, 08:20 PM #2
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
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
-
[Help] W/ background image
By gundum584 in forum New To JavaReplies: 9Last Post: 01-10-2011, 05:48 AM -
JApplet background image
By footballHunter in forum New To JavaReplies: 1Last Post: 11-24-2010, 02:46 AM -
Need help with JFrame background image
By ProGenius in forum New To JavaReplies: 6Last Post: 12-27-2009, 04:17 PM -
Background image
By leiferouis in forum New To JavaReplies: 9Last Post: 03-08-2009, 05:49 PM -
Image as background
By Java.child in forum AWT / SwingReplies: 2Last Post: 10-02-2008, 11:37 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks