Results 1 to 7 of 7
  1. #1
    sahildave1991 Guest

    Unhappy trying to add image to background in JFrame...what's the problem in this program

    i've been trying to add a imagein the background of a frame and over it i want to add two buttons play and quit.
    can anyone solve the problem in this program ??
    Java Code:
    package Screens;
    
    import java.awt.event.*;
    import java.awt.*;
    
    import javax.swing.*;
    
    public class FirstScreen extends JFrame implements ActionListener
    {
    	JLabel screen;
    	JButton play,quit;
    	public void FirstScreen()
    	{
    		setLayout(null);
    	
    		screen= new JLabel(new ImageIcon ("back.png"));
    		play=new JButton("PLAY");
    		quit= new JButton("QUIT");
    		
    		screen.setBounds(0, 0, 534, 329);
    		play.setBounds(50, 40, 20, 10);
    		quit.setBounds(10, 10, 50, 30);
    		
    		add(screen);
    		add(quit);
    		add(play);
    	
    		this.setSize(534,329);
    		this.setResizable(false);
    	
    		play.addActionListener(this);
    		quit.addActionListener(this);		
    	}
    	
    	public static void main(String [] arr)
    	{
    		FirstScreen os = new FirstScreen();
    		os.setVisible(true);
    	}
    	
    }
    i am really new to java and dont know wat is happening :(

  2. #2
    sahildave1991 Guest

    Default

    ignore ActionListener commands.....i forgot to delete it in this code....

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    I recommend that you avoid using null layout but instead use the standard layout managers. Often the best way to set the background of a JFrame is to paint an image in a JPanel and make that JPanel the JFrame's contentPane. An example of drawing an image in a JPanel can be found here: BackgroundImageOnJPanel.

    Much luck!

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    I recommend that you avoid using null layout but instead use the standard layout managers. Often the best way to set the background of a JFrame is to paint an image in a JPanel and make that JPanel the JFrame's contentPane. An example of drawing an image in a JPanel can be found here: BackgroundImageOnJPanel.

    Much luck!

  5. #5
    mrp3rf3ct83 is offline Member
    Join Date
    Jul 2010
    Posts
    6
    Rep Power
    0

    Default Here you go!!!

    First off, whenever working with files on the computer, it's always best to write in the entire address for the file. You also have to override the paintCompont method in order to repaint the background. This here is the code you need. I did your buttons a little differently due to ease of writing with layout managers.

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;

    public class FirstScreen extends JFrame{
    public FirstScreen() throws Exception{
    setSize(400,400);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setLocation(500, 100);
    JPanel panel = new TestPanel();
    this.add(panel);
    }

    public static void main(String [] a) throws Exception{
    JFrame frame = new FirstScreen();
    frame.setVisible(true);
    }
    }
    class TestPanel extends JPanel implements ActionListener {

    String pngFile = "C:\\Users\\Documents\\back.png"; // Change the directory to match your file location
    Image pngImage = Toolkit.getDefaultToolkit().getImage(pngFile);
    JButton quit, play;
    Image image;

    public TestPanel() throws Exception {

    setLayout(new BorderLayout());

    play = new JButton("Play");
    play.addActionListener(this);
    add(play, BorderLayout.NORTH);

    quit = new JButton("Exit");
    quit.addActionListener(this);
    add(quit, BorderLayout.SOUTH);
    }

    public void actionPerformed(ActionEvent e) {
    Object obj = e.getSource();
    try {
    if (obj == play) {
    image = pngImage;
    } else {
    System.exit(0);
    }
    repaint();
    } catch (Exception f) {}
    }

    @ Override
    public void paintComponent( Graphics g ){
    super.paintComponent( g );
    Graphics2D g2d = (Graphics2D) g;
    g2d.drawImage(image, 0, 0, getWidth(), getHeight(), null); // getWidth/Height gets the size of your panel so it will fit inside
    }
    }

  6. #6
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default

    Use println statements to check if you are loading the image correctly. I'd recommend using getClass().getResource() methods...this will provide a relative path so you do not have to rely on the full path to the image and can package the image in a jar along with your app (helps prevent path problems when running on difference computers/platforms). Providing the full path may end up working, but you may end up being stuck if you move or run your app on a computer other than your own, or even move your app to a different directly than the one specified.
    see How to Use Icons (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Swing Components)

    If you insist on using null layouts, then you need to specify the locations of the components added to the gui. But I'm with Fubarable, use a layout manager.

  7. #7
    mrp3rf3ct83 is offline Member
    Join Date
    Jul 2010
    Posts
    6
    Rep Power
    0

    Default

    If you're trying to debug a program, typing in hard code is easier to manage. You get the kinks out first and then find out better ways to do it to add flexibility. Typing in the complete path will ensure that you're file is being read correct without incorporating any methods. Also if you want your program to look like you want it, where the buttons are exactly where you want them, you should read up on GridBagConstraints. You can pretty much put buttons where you want to on the panel.

Similar Threads

  1. Replies: 1
    Last Post: 07-07-2010, 09:25 AM
  2. JFrame image background
    By 851marc in forum NetBeans
    Replies: 5
    Last Post: 03-09-2010, 06:27 PM
  3. How to add a background image to JFrame
    By dunafrothint in forum AWT / Swing
    Replies: 1
    Last Post: 02-26-2010, 10:17 PM
  4. Need help with JFrame background image
    By ProGenius in forum New To Java
    Replies: 6
    Last Post: 12-27-2009, 04:17 PM
  5. Replies: 2
    Last Post: 03-24-2008, 05:29 AM

Posting Permissions

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