Results 1 to 3 of 3
  1. #1
    Jeffrod is offline Member
    Join Date
    Apr 2011
    Rep Power

    Default Change background image

    Im doing a school assignment which requires me to make a small program with Swing. Basically it asks for a image to use as a background and then allows me to add JComponents on top of it (think notes on a map).
    The problem i have is that i cant get the background image to work properly. I can get it to work just the way i want it until i load a new image and then it wont allow me to add JComponents on top of it.
    The way i figured id fix this just stops the image from painting at all and i cant figure out why.

    Code for choosing background image:
    Java Code:
    public void actionPerformed(ActionEvent ave){			
    			int confirm = jfc.showOpenDialog(PathFinder.this);
    			if(confirm != JFileChooser.APPROVE_OPTION)
    			File file = jfc.getSelectedFile();
    			String filePath = file.getAbsolutePath();
    			if(mapPanel != null)
    			if(nodePanel != null)
    			nodePanel = new JPanel();
    			mapPanel = new ImagePanel(filePath);
    After this is done the image just wont show and the panel is tiny or non-existant. I have a check before adding components on the nodePanel and it usually passes it, i have tried so many things by now i cant remember when it works, and allows me to add components (altho in a very tiny area in the top right corner).

    Both nodePanel and mapPanel are declared as null from the start:
    Java Code:
    	ImagePanel mapPanel = null;
    	JPanel nodePanel = null;
    nodePanel is as you see just a straight up JPanel. I got this to work by adding it directly into the JFrame but then it wouldnt work after loading a new image.
    mapPanel is basically a JPanel but with a changed paintComponent:
    Java Code:
    public class ImagePanel extends JPanel{
    	private ImageIcon image;
    	ImagePanel(String file){
    		image = new ImageIcon(file);
    		int w = image.getIconWidth();
    		int h = image.getIconHeight();
    		setPreferredSize(new Dimension(w, h));
    		setMinimumSize(new Dimension(w, h));
    		setMaximumSize(new Dimension(w, h));
    	protected void paintComponent(Graphics g){
    The idea is that i load a image and then "paint" on it. When i load a new image everything i have "painted" is removed and i start from scratch. Any help is deeply appreciated.
    Last edited by Jeffrod; 08-10-2011 at 05:39 PM.

  2. #2
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Rep Power


    and the panel is tiny or non-existant.
    When you do custom painting you need to override the getPreferredSize() method of the panel to return the preferred size. In this case I guess the preferred size would be the size of the image.

    Also, when you change the image you don't want to create a whole new panel. Instead create a setImage(...) method. This method would just update the class variable for the image in then invoke repaint. (Your should store the image, not the ImageIcon).

  3. #3
    Jeffrod is offline Member
    Join Date
    Apr 2011
    Rep Power

Similar Threads

  1. Change JPanel background color
    By snaquetime in forum AWT / Swing
    Replies: 5
    Last Post: 06-04-2011, 11:03 PM
  2. Replies: 4
    Last Post: 10-14-2010, 08:18 AM
  3. Change JPanel background after its been set once
    By mevets in forum AWT / Swing
    Replies: 4
    Last Post: 04-14-2010, 01:07 AM
  4. unable to change the TextArea.background !
    By Y. Progammer in forum New To Java
    Replies: 2
    Last Post: 02-28-2010, 04:55 PM
  5. how to change windows xp background image through java
    By erosenthil in forum Advanced Java
    Replies: 1
    Last Post: 08-18-2009, 12:57 PM

Posting Permissions

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