8 Replies - 2855 Views - Last Post: 19 December 2011 - 11:58 AM

#1 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8379
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Background image in a JComponent

Posted 22 July 2010 - 03:46 PM

Description: Just cut and paste it You will have to provide your own image .jpg, .gif or .png This will work with JPanel but you can chanche the JComponent for the one you wantHow to put a background image in a JComponent
/*
 * To Paint a background on any JComponent
 */
import javax.swing.*;
import java.awt.*;

/*
 * We have to extend the JComponent into which you want a background
 * Here we extends JPanel but you can extend JTextArea, JLabel or what you want
 */
public class BackPanel extends JPanel {

	// the image you want in the background
	private Image img;
	
	// constructor (you may have to overload the one you want to use
	// for example receiving a String as parameter for the a JLabel)
	BackPanel() {
		// read the background image
		ImageIcon icon = new ImageIcon("EuroStyle3.jpg");
		// extract the image out of it
		img = icon.getImage();
	}
	
	// the paint method toi be overload
	public void paint(Graphics g) {
		// draw the background image
		// here we use the simplest g.drawImage() method that draw as much of the image it can
		// multiple different drawImage() methods permit to shrink or extend the image 
		g.drawImage(img, 0, 0, this);
		// we declare the component as not opaque so
		setOpaque(false);
		// the standard paint() method won't redraw the background
		super.paint(g);
		setOpaque(true);
	}
	
	/*
	 * The tester
	 */
	public static void main(String[] args) {
		JFrame f = new JFrame("Test back");
		f.setLayout(new BorderLayout());
		// we instantiate our JComponent with background
		BackPanel p = new BackPanel();
		// as in this example it is a JPanel we set it's layout
		p.setLayout(new GridLayout(4,4));
		// and we fill the JPanel with JLabel
		for(int i = 0; i < 4; i++) {
			for(int j = 0; j < 4; j++) {
				JLabel label = new JLabel(" i = " + i + " j = " + j); 
				p.add(label);
			}
		}
		// standard JFrame stuff to display it
		f.add(p, BorderLayout.CENTER);
		f.setBounds(50, 50, 400, 300);
		f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		f.setVisible(true);
	}
}



Is This A Good Question/Topic? 0
  • +

Replies To: Background image in a JComponent

#2 bravo659   User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 181
  • Joined: 18-May 08

Re: Background image in a JComponent

Posted 03 October 2010 - 06:11 AM

This snippet only works by using j's and i's in a gridlayout. Now what does those letters mean if you want to display an image from a file to a JFrame window. You only want to show what you know what is needed in the forum. Sorry to say.
Was This Post Helpful? 0
  • +
  • -

#3 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8379
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Background image in a JComponent

Posted 03 October 2010 - 03:24 PM

No does not have anything to do with the layout used. I used a GridLayout just as example
Was This Post Helpful? 0
  • +
  • -

#4 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8379
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Background image in a JComponent

Posted 03 October 2010 - 08:34 PM

Be careful if you want to apply this code to a JFrame. A JFrame is not really a JComponent and the JPanel contained in the JFrame will hide the background image of the JFrame anyhow.
Was This Post Helpful? 0
  • +
  • -

#5 billy81   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 24-February 09

Re: Background image in a JComponent

Posted 01 December 2011 - 10:48 AM

i've just tried this code and i'm getting an error..it says "void is an invalid type for the variable paint"...i just copied exactly what was here...the error is at line 26
Was This Post Helpful? 0
  • +
  • -

#6 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8379
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Background image in a JComponent

Posted 01 December 2011 - 11:01 AM

You should cut & paste the code not copy it as you obvioulsly didn'c copy corectly. Based on your error, paint is considered as a variable not a method. You probably have the () missing
Was This Post Helpful? 0
  • +
  • -

#7 billy81   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 24-February 09

Re: Background image in a JComponent

Posted 01 December 2011 - 11:24 AM

yes, sorry pbl. I was missing a bracket, it works perfectly. It's very well written and easy to understand. Thanks for the information
Was This Post Helpful? 0
  • +
  • -

#8 TheJim0   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 19-December 11

Re: Background image in a JComponent

Posted 19 December 2011 - 09:45 AM

I can't seem to be able to set the image i want, where do I put the file for the image?
Was This Post Helpful? 0
  • +
  • -

#9 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8379
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Background image in a JComponent

Posted 19 December 2011 - 11:58 AM

Thats a complete different story. In an Application in the same directory as your .class. Within an Applet there are a lot of references in the forum on how to read an image for an Applet.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1