Results 1 to 6 of 6
  1. #1
    yanipao is offline Member
    Join Date
    Oct 2009
    Posts
    27
    Rep Power
    0

    Default how to put a background image in a JPanel

    hi. this is my code for the startMenu of my memory game. how can i put a backgroundimage in this? thank you

    Java Code:
    import java.lang.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.util.*;
    import javax.swing.*;
    import javax.swing.event.*;
    //import static java.util.Collections.*;
    
    class GameInterface extends JFrame implements ActionListener{
    
    private JButton startBtn, exitBtn, howtoBtn, scoreBtn, levelBtn;
    private JPanel wholeMenuPanel, firstMenuPanel, secondMenuPanel;
    private JLabel welcomeLabel, nameLabel;
    private TextField nameField;
    
    public GameInterface(){
    
    	init();
    	MenuPanel();
    	firstMenuPanel();
    	secondMenuPanel();
    	setTitle("GuessMe");
    	setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	setSize(700,500);
    	setVisible(true);
    }
    public void init(){
    
    	try{
    	jbInit();
    	}
    	catch(Exception e){
    	e.printStackTrace();
    	}	
    	}
    
    private void jbInit()throws Exception {
    
    	startBtn = new JButton ("Start/New Game");
    	startBtn.addActionListener(this);
    
    	howtoBtn = new JButton ("How to play this game");
    	howtoBtn.addActionListener(this);
    
    	levelBtn = new JButton ("Level");
    	levelBtn.addActionListener(this);
    
    	scoreBtn = new JButton ("Score");
    	scoreBtn.addActionListener(this);
    
    	exitBtn = new JButton ("Exit/Quit Game");
    	exitBtn.addActionListener(this);
    
    	welcomeLabel.setBounds(new Rectangle(21, 13, 118, 30));
    	nameLabel.setBounds(new Rectangle(21, 50, 118, 30));
    	
    	welcomeLabel.setText("WELCOME!");
    	nameLabel.setText("Your name here:");
    	
    	nameField = new TextField("", 20);
    
    	//welcomeLabel.addAcionListener(this);
    	//nameLabel.addActionListener(this);
    	nameField.addActionListener(this);
    }
    
    public void MenuPanel(){
    
    	wholeMenuPanel = new JPanel();
    	firstMenuPanel = new JPanel();
    	secondMenuPanel = new JPanel();
    
    	wholeMenuPanel.setLayout(new BorderLayout());
    	firstMenuPanel.setLayout(new FlowLayout());
    	secondMenuPanel.setLayout(new FlowLayout());
    
    	wholeMenuPanel.add(firstMenuPanel, BorderLayout.NORTH);
    	wholeMenuPanel.add(secondMenuPanel, BorderLayout.CENTER);
    }
    
    public void firstMenuPanel(){
    
    	Panel strtPnl = new Panel();
    	strtPnl.setLayout(null);
    	
    	Panel buttonPnl = new Panel();
    
    	buttonPnl.add(startBtn);
    	buttonPnl.add(exitBtn);
    	buttonPnl.add(howtoBtn);
    	buttonPnl.add(levelBtn);
    	buttonPnl.add(scoreBtn);
    
    	add(strtPnl, BorderLayout.CENTER);
    	add(buttonPnl, BorderLayout.SOUTH);
    }
    public void secondMenuPanel(){
    	
    	JLabel welcomeLabel = new JLabel();
    	JLabel nameLabel = new JLabel();
    	TextField nameField = new TextField();
    	
    	//backgroundLabel = new JLabel ();
    	//add(welcomeLabel, FlowLayout.CENTER);
    	//add(nameLabel, FlowLayout.CENTER);
    	//add(nameField, TextLayout.CENTER);
    
    	
    
    
    }
    
    public void actionPerformed(ActionEvent e){
    	if (startBtn == e.getSource()){
    		//GuessMe application = new GuessMe();
    	}
    	if (exitBtn == e.getSource()){
    		System.exit(0);
    	}
    	if (howtoBtn == e.getSource()){
    		JOptionPane pane = new JOptionPane();
    		pane.showMessageDialog(null, "To play this game, the player shoud click the cards and match up the pairs.");
    	}
    	if (levelBtn == e.getSource()){}
    	if (scoreBtn == e.getSource()){}
    }
    
    
    public static void main(String[] args){
    	GameInterface Application = new GameInterface();
    }
    }
    Last edited by yanipao; 10-19-2009 at 11:14 AM.

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Google is our friend.

  3. #3
    yanipao is offline Member
    Join Date
    Oct 2009
    Posts
    27
    Rep Power
    0

    Default

    thanks...=)

  4. #4
    steelshark is offline Member
    Join Date
    Aug 2009
    Posts
    25
    Rep Power
    0

    Default

    I recently had to do this, and the way i did was by making a new class, ImagePanel..It just extends the JPanel class.Hope this helps you a bit

  5. #5
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    In that case then steelshark is also our friend.

  6. #6
    steelshark is offline Member
    Join Date
    Aug 2009
    Posts
    25
    Rep Power
    0

    Default

    just tryin to help him out a bit ^^
    but makin friends is cool ;-)

Similar Threads

  1. Background Image of Applet
    By BangJava in forum Java Applets
    Replies: 8
    Last Post: 11-24-2010, 06:48 AM
  2. J2ME background image
    By sandeeprao.techno in forum CLDC and MIDP
    Replies: 0
    Last Post: 09-14-2009, 10:02 AM
  3. Background image
    By leiferouis in forum New To Java
    Replies: 9
    Last Post: 03-08-2009, 06:49 PM
  4. Image as background
    By Java.child in forum AWT / Swing
    Replies: 2
    Last Post: 10-03-2008, 12:37 AM
  5. Why this image background is black ?
    By samson in forum Java 2D
    Replies: 1
    Last Post: 07-17-2007, 05:24 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
  •