Results 1 to 5 of 5
Like Tree1Likes
  • 1 Post By Fubarable

Thread: Cant get my background image to cover the whole panel

  1. #1
    Lanuk is offline Member
    Join Date
    Dec 2011
    Posts
    8
    Rep Power
    0

    Default Cant get my background image to cover the whole panel

    So basically I want my picture to take up the whole panel and labels and such placed on the image. So far my image only appears behind the labels/textfields/etc., but does not cover the whole panel as I want it to. I have looked all over the internet and have not been able to figure out how to do this (as I am new to java), so please don't link me to an online guide or something XD. Anyways, thanks in advance for any help. Here is my code so far:

    (I apologize for the long, messy code! The only really important stuff is at the very top encased in two rows of green comment slashes. I just thought id put the rest in there in case that needed to be c)

    Java Code:
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Component;
    import java.awt.Dimension;
    import java.awt.Font;
    import java.awt.Graphics;
    import java.awt.GridLayout;
    import java.awt.Image;
    import java.awt.Toolkit;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    
    import javax.swing.BorderFactory;
    import javax.swing.ImageIcon;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JMenu;
    import javax.swing.JMenuBar;
    import javax.swing.JMenuItem;
    import javax.swing.JPanel;
    import javax.swing.JTextArea;
    import javax.swing.JTextField;
    import javax.swing.border.Border;
    import javax.swing.border.TitledBorder;
    
    public class LibraryThing extends JFrame implements MouseListener{
    	
    	/////////////////////////////////////////////////////////////////////////////////////
    	private static JPanel testpic = new JPanel() {
    	     ImageIcon backImage = new ImageIcon("src/bookcase.jpg");
    	     Image image = backImage.getImage();
             {setOpaque(false);}
    	     //Override
    	     public void paintComponent (Graphics g) {
    	       g.drawImage(image, 0, 0, this);
    	       super.paintComponent(g);
    	     }
    	   };
    	 /////////////////////////////////////////////////////////////////////////////////////////////////
    	
    	
    	private static JLabel testtext = new JLabel("Hello");
    	
    	private static JFrame main = new JFrame("Library");
    	
    	private static JPanel primary = new JPanel();
    	private static JPanel panelm = new JPanel();
    	private static JPanel panelA = new JPanel();
    	private static JPanel panelB = new JPanel();
    	private static JPanel panelC = new JPanel();
    	private static JPanel panelD = new JPanel();
    	
    	
    //	private static JButton bCheckIn = new JButton("Check In Book");
    //	private static JButton bCheckOut = new JButton("Check Out Book");
    	
    	private static JLabel lblSearch = new JLabel("Search for a Book");
    	private static JTextField txtSearch = new JTextField(10);
    	
    	private static JLabel lblBookName = new JLabel("Book Name: ");
    //	private static JTextField txtBookName = new JTextField(10);
    	private static JTextField txtBookNameC = new JTextField(10);
    	
    	private static JLabel lblName = new JLabel("Enter Name: ");
    	private static JLabel lblAddress = new JLabel("Enter Address: ");
    	private static JLabel lblCardNum = new JLabel("Enter Card No.: ");
    	private static JTextField txtName = new JTextField(10);
    	private static JTextField txtAddress = new JTextField(10);
    	private static JTextField txtCardNum = new JTextField(10);
    	
    	private static JLabel lblIntrob = new JLabel("");
    	private static JLabel lblIntro = new JLabel("Welcome to the library.",JLabel.CENTER);
    	private static JLabel lblIntro2 = new JLabel("Please use the dropdown menus",JLabel.CENTER);
    	private static JLabel lblIntroa = new JLabel("to navigate the website.",JLabel.CENTER);
    	private static JLabel lblIntro3 = new JLabel("Thanks!",JLabel.CENTER);
    	
    	private static JMenuBar mB = new JMenuBar();
    	private static JMenu pat = new JMenu("Patron");
    	private static JMenu l = new JMenu("Books");
    	//private static JMenu c = new JMenu("Check in/Check out");
    	private static JMenuItem register,logout,search,list,inout,i;
    	
    	private static JTextField txtCheckIn = new JTextField(10);
    	private static JTextField txtCheckOut = new JTextField(10);
    	private static JLabel lblCheckIn = new JLabel("Check In: ");
    	private static JLabel lblCheckOut = new JLabel("Check Out: ");
    	
    	private static JLabel label = new JLabel();	
    	private static JLabel label2 = new JLabel();	
    	private static JLabel label3 = new JLabel();	
    	
    	
    	
    	//use TextArea for multiple lines of output. Can be used with toString method.
    	private static JTextArea output = new JTextArea("",5,25);
    	
    	public static void main (String[] args){
    		LibraryThing one = new LibraryThing();
    		final Patron p1 = new Patron();
    		txtName.addActionListener(one.new Listener(p1));
    		setUp();
    	}
    	private class Listener implements ActionListener{
    		Patron p1;
    		
    		public Listener(Patron p){
    			p1 = p;
    		}
    		
    		public void actionPerformed(ActionEvent e){
    			if(e.getSource()==txtName && e.getSource()==txtAddress && e.getSource() == txtCardNum){
    				
    				p1.setName(txtName.getText());
    				p1.setAddress(txtAddress.getText());
    				p1.setCardNum(txtCardNum.getText());
    				label.setText(p1.getName());
    				label2.setText(p1.getAddress());
    				label3.setText(p1.getCardNum());
    //				label2.setText(txtAddress.getText());
    //				label3.setText(txtCardNum.getText());
    //				panelD.add(label);
    //				panelD.add(label2);
    //				panelD.add(label3);
    				label.setVisible(true);
    				label.setVisible(true);
    				label.setVisible(true);
    			}
    			panelD.repaint();
    			main.repaint();
    			validate ();
    		}
    
    	}
    	
    	public static void setUp(){
    		
    		LibraryThing one = new LibraryThing();
    		
    		Font f = new Font("Monospaced", Font.BOLD, 16);
    		main.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		main.setPreferredSize(new Dimension(400,400));
    		
    		register = new JMenuItem("Register");
    		logout = new JMenuItem("Logout");
    		search = new JMenuItem("Book Search");
    		list = new JMenuItem("Book List");
    		inout = new JMenuItem("Check In/Out");
    		i = new JMenuItem("Image");
    		
    		panelD.setLayout(new GridLayout (9,1));
    		panelA.setLayout(new GridLayout (6,1));
    		panelB.setLayout(new GridLayout (4,1));
    		panelC.setLayout(new GridLayout (2,1));
    		//testpic.setLayout(new GridLayout (4,5));
    	//	paneli.setLayout(new GridLayout (2,1));
    	//	tp.add(image, BorderLayout.CENTER);
    		
    		Border tb = BorderFactory.createTitledBorder("Register");
    		((TitledBorder) tb).setTitleJustification (TitledBorder.LEFT);
    		panelD.setBorder(tb);
    		
    		Border tb2 = BorderFactory.createTitledBorder("Check In/Out");
    		((TitledBorder) tb2).setTitleJustification (TitledBorder.LEFT);
    		panelB.setBorder(tb2);
    		
    		Border tb3 = BorderFactory.createTitledBorder("Book Search");
    		((TitledBorder) tb3).setTitleJustification (TitledBorder.LEFT);
    		panelC.setBorder(tb3);
    		
    		panelm.setLayout(new GridLayout (6,1));
    		
    		pat.add(register);
    		pat.add(logout);
    		register.addMouseListener(one);
    		logout.addMouseListener(one);
    		
    		l.add(search);
    		l.add(list);
    		search.addMouseListener(one);
    		list.addMouseListener(one);
    		
    		l.add(inout);
    		inout.addMouseListener(one);
    		
    		l.add(i);
    		i.addMouseListener(one);
    		
    		mB.add(pat);
    		mB.add(l);
    		mB.add(l);
    		
    		main.setJMenuBar(mB);
    		
    		panelA.setFont(f);
    		main.setPreferredSize(new Dimension(500,300));
    		
    			lblIntro.setForeground(Color.BLACK);
    			lblIntro.setFont(f);
    			
    			lblIntro2.setForeground(Color.BLACK);
    			lblIntro2.setFont(f);
    			
    			lblIntro3.setForeground(Color.BLACK);
    			lblIntro3.setFont(f);
    			
    			lblIntroa.setForeground(Color.BLACK);
    			lblIntroa.setFont(f);
    			
    			lblSearch.setForeground(Color.BLACK);
    			lblSearch.setFont(f);
    			
    			lblName.setForeground(Color.BLACK);
    			lblName.setFont(f);
    			
    			lblAddress.setForeground(Color.BLACK);
    			lblAddress.setFont(f);
    			
    			lblCardNum.setForeground(Color.BLACK);
    			lblCardNum.setFont(f);
    			
    			lblBookName.setForeground(Color.BLACK);
    			lblBookName.setFont(f);
    			
    			lblCheckIn.setForeground(Color.black);
    			lblCheckIn.setFont(f);
    			
    			lblCheckOut.setForeground(Color.black);
    			lblCheckOut.setFont(f);
    			
    			txtSearch.setForeground(Color.black);
    			txtSearch.setFont(f);
    			
    			txtCheckIn.setForeground(Color.black);
    			txtCheckIn.setFont(f);
    			
    			txtCheckOut.setForeground(Color.black);
    			txtCheckOut.setFont(f);
    			
    			txtBookNameC.setForeground(Color.black);
    			txtBookNameC.setFont(f);
    			
    			output.setFont(f);
    			output.setBackground(new Color(0,255,0));
    			output.setForeground(Color.white);
    			output.setFont(f);
    			
    			panelB.add(lblIntrob);
    			panelB.add(lblCheckIn);
    			panelB.add(txtCheckIn);
    			panelB.add(lblCheckOut);
    			panelB.add(txtCheckOut);
    			
    			panelC.add(lblBookName);
    			panelC.add(txtBookNameC);
    
    			//panelD.add(lblIntrob);
    			panelD.add(lblName);
    			panelD.add(txtName);
    			panelD.add(lblAddress);
    			panelD.add(txtAddress);
    			panelD.add(lblCardNum);
    			panelD.add(txtCardNum);
    			panelD.add(label);
    			panelD.add(label2);
    			panelD.add(label3);
    			
    			panelm.add(lblIntrob);
    			panelm.add(lblIntro);
    			panelm.add(lblIntro2);
    			panelm.add(lblIntroa);
    			panelm.add(lblIntro3);
    			
    //			testpic.add(testtext);
    //			testpic.add(txtCardNum);
    //			testpic.add(lblName);
    //			testpic.add(lblCheckIn);			
    			
    			primary.add(panelm);
    			primary.add(panelA);
    			primary.add(panelB);
    			primary.add(panelC);
    			primary.add(panelD);
    //			primary.add(testpic);
    
    			main.getContentPane().add(primary);
    			
    			panelA.setVisible(false);
    			panelB.setVisible(false);
    			panelC.setVisible(false);
    			panelD.setVisible(false);
    			panelm.setVisible(true);
    //			testpic.setVisible(false);
    		//	paneli.setVisible(false);
    		
    			main.pack();
    			main.setVisible(true);
    			
    	}//setUp
    	
    	@Override
    	public void mouseClicked(MouseEvent arg0) {
    		// TODO Auto-generated method stub
    		
    	}
    	@Override
    	public void mouseEntered(MouseEvent e) {
    		
    	}
    	@Override
    	public void mouseExited(MouseEvent arg0) {
    		// TODO Auto-generated method stub
    		
    	}
    	@Override
    	public void mousePressed(MouseEvent e) {
    		
    	}
    	@Override
    	public void mouseReleased(MouseEvent e) {
    		if(e.getSource() == logout){
    			panelA.setVisible(false);
    			panelB.setVisible(false);
    			panelC.setVisible(false);
    			panelm.setVisible(true);
    			panelD.setVisible(false);
    //			testpic.setVisible(false);
    		}	
    		if(e.getSource() == register){
    			panelA.setVisible(false);
    			panelB.setVisible(false);
    			panelC.setVisible(false);
    			panelm.setVisible(false);
    			panelD.setVisible(true);
    //			testpic.setVisible(false);
    		}	
    		if(e.getSource() == inout){
    			panelA.setVisible(false);
    			panelB.setVisible(true);
    			panelC.setVisible(false);
    			panelm.setVisible(false);
    			panelD.setVisible(false);
    //			testpic.setVisible(false);
    		}	
    		if(e.getSource() == search){
    			panelA.setVisible(false);
    			panelB.setVisible(false);
    			panelC.setVisible(true);
    			panelm.setVisible(false);
    			panelD.setVisible(false);
    		}	
    		if(e.getSource() == list){
    			panelA.setVisible(true);
    			panelB.setVisible(false);
    			panelC.setVisible(false);
    			panelm.setVisible(false);
    			panelD.setVisible(false);
    //			testpic.setVisible(false);
    
    		}
    		if(e.getSource() == i){
    			panelA.setVisible(false);
    			panelB.setVisible(false);
    			panelC.setVisible(false);
    			panelm.setVisible(false);
    			panelD.setVisible(false);
    //			testpic.setVisible(true);
    			//paneli2.setVisible(true);
    		}
    	}
    }

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

    Default Re: Cant get my background image to cover the whole panel

    That's a heck of a lot of code you've posted up there, most of it is unrelated to the problem. For me I find it helpful when trying to solve a coding problem to first isolate and simplify the problem before trying to solve it, and that's what I suggest you do. Just create a simple GUI that has a background image, and then try to add more things to it, and see what happens. Then if you fail, you can post a much simpler program here that we will have time to read (I don't have time to read the huge code volume above). Some suggestions:

    • I usually draw my background image in a JPanel and then make it the JFrame's contentPane.
    • I draw the image in the JPanel's paintComponent method, usually right after calling the super method.
    • I've seen some who have done the same but have used a JLabel and added the image to the label as an ImageIcon. If you do that, make sure to set the JLabel's opaque property to true, and make sure to give it a decent layout manager.
    • Make sure that any panels added on top of the image drawing JPanel have their opaque property set to false. Same for many components that you add.

  3. #3
    Lanuk is offline Member
    Join Date
    Dec 2011
    Posts
    8
    Rep Power
    0

    Default Re: Cant get my background image to cover the whole panel

    Yeah, I guess it was a bad idea of me to post all that code (dunno why I did that), but the code I mainly needed help with is the code between the two long comments up at the top... I guess ill just delete everything around that then?

    EDIT: Ah, guess I cant edit my post can I :P

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

    Default Re: Cant get my background image to cover the whole panel

    You can post more code down here, but I suggest creating and posting an SSCCE (check out the link to see what this is).
    DarrylBurke likes this.

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default Re: Cant get my background image to cover the whole panel

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Replies: 1
    Last Post: 10-24-2010, 08:25 PM
  2. Replies: 4
    Last Post: 10-14-2010, 08:18 AM
  3. change panel background at runtime
    By cahyadiakbar in forum AWT / Swing
    Replies: 2
    Last Post: 08-26-2010, 08:16 AM
  4. How to set Image in Panel ?
    By sudmitra in forum New To Java
    Replies: 0
    Last Post: 09-12-2009, 10:07 PM
  5. add image on panel
    By samiksha.goel in forum AWT / Swing
    Replies: 4
    Last Post: 08-02-2008, 07:38 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
  •