1

I'm trying to follow this answer to add a background picture to a JFrame and I'm getting a weird error. While debugging my url is coming back null and I get a window that pops up saying "Class File Editor" source not found the source attachment does not contain the source for the file Launcher.class you can change the source attachment by clicking Chang Attached Source below. What does that mean?

here's the code that I have so far:

import java.awt.Graphics;
import java.awt.image.BufferedImage;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;

public class DeluxKenoMainWindow extends JFrame 
{


   public DeluxKenoMainWindow()
   {
    initUI();   
   }

   public final void initUI()
   {
     setLayout(null);
     getContentPane().add(new BackgroundImage());
     int xCoord = 10;
     int yCoord = 10;
     Button[] button = new Button[80];
     for(int i = 0; i<80; i++)
     {
         String buttonName = "button" + i;
        if(i % 10 == 0)
        {
            xCoord = 10;
            yCoord +=40;
        }

        xCoord += 40;
        if(i % 40 == 0)
            yCoord += 8;


         button[i] = new Button(buttonName, xCoord, yCoord, i+1);

         getContentPane().add(button[i]);
     }


     setTitle("Delux Keno");
     setSize(500,500);

     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     setLocationRelativeTo(null);

   }



   public static void main(String[] args)
   {
    SwingUtilities.invokeLater(new Runnable()
    {
        public void run()
        {
            System.setProperty("DEBUG_UI", "true");
            DeluxKenoMainWindow ex = new DeluxKenoMainWindow();
            ex.setVisible(true);
        }
    });
   }
   }


import javax.imageio.ImageIO;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import java.io.*;

public class Button extends JButton {


    private String name;
    private int xCoord;
    private int yCoord;
    private final int xSize = 40;
    private final int ySize = 40;
    private int buttonNumber;
    private String picture;


    public Button(String inName, int inXCoord, int inYCoord, int inButtonNumber)
    {


      xCoord = inXCoord;
      yCoord = inYCoord;
      buttonNumber = inButtonNumber;
      picture = "graphics\\" + buttonNumber + "normal.png";



      super.setName(name);    
      super.setIcon(new ImageIcon(picture));
      super.setBounds(xCoord, yCoord, xSize, ySize);

    }



    }


import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.URL;


import javax.imageio.ImageIO;
import javax.swing.JPanel;


public class BackgroundImage extends JPanel{

    private BufferedImage img;
    private URL rUrl;
    public BackgroundImage()
    {
        super();

        try{
            rUrl = getClass().getResource("formBackground.png");
            img = ImageIO.read(rUrl);
        }
        catch(IOException ex)
        {
            ex.printStackTrace();
        }

    }

    @Override
    protected void paintComponent(Graphics g)
    {
        //super.paintComponent(g);
        g.drawImage(img, 0, 0, getWidth(), getHeight(), this);

    }

}

any sugesstions will be appriciated!!

Community
  • 1
  • 1
Craig Smith
  • 583
  • 3
  • 9
  • 26
  • To draw a background image, check [this answer](http://stackoverflow.com/questions/13401109/java-add-background-image-to-frame/13401871#13401871). The rest of your problem is related to your IDE but I don't know which one you are using. – Guillaume Polet Nov 17 '12 at 08:11
  • Do you have the formBackground.png in your classpath? or do you in the same directory where DeluxKenoMainWindow.class is available while running. – shazin Nov 17 '12 at 08:12
  • the picture file is in under Delux Keno-> graphics -> picture file I am using the eclipse ide – Craig Smith Nov 17 '12 at 08:40

3 Answers3

4
Community
  • 1
  • 1
mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • as from what I can tell I'm not doing any file IO in the paintComponent, the buffered image is being read before hand and stored as a variable img. After playing with the getClass().getResource() in my Button class, this seems to be where I have the problem, if I use the same method there I get a bunch of buttons which don't have any pictrues. Also after stepping thru the code I see that the method paintComponent(Graphics g) never gets called, why is this? – Craig Smith Nov 17 '12 at 12:07
  • [see this thread](http://stackoverflow.com/questions/13429219/variable-name-of-swing-component-in-netbeans), [use GridLayout instead of AbsoluteLayout](http://docs.oracle.com/javase/tutorial/uiswing/layout/grid.html) – mKorbel Nov 17 '12 at 12:59
  • For Mine Sweeper, I prefer to use a JPanel that holds a JButton and JLabel all controlled by a CardLayout. – Hovercraft Full Of Eels Nov 17 '12 at 19:21
0

went at it a different way, using a JLabel. Here's my final code:

import java.awt.Graphics;
import java.awt.image.BufferedImage;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;

public class DeluxKenoMainWindow extends JFrame 
{


   public DeluxKenoMainWindow()
   {
    initUI();   
   }

   public final void initUI()
   {
     setLayout(null);
     JLabel background = new JLabel(new ImageIcon ("graphics\\formBackground.png"));

     background.setBounds(0,0,600,600);

     //getContentPane().add(new BackgroundImage());
    int xCoord = 85;
     int yCoord = 84;
     Button[] button = new Button[80];
     for(int i = 0; i<80; i++)
     {
         String buttonName = "button" + i;
        if(i % 10 == 0)
        {
            xCoord = 12;
            yCoord +=44;
        }


        if(i % 40 == 0)
            yCoord += 10;


         button[i] = new Button(buttonName, xCoord, yCoord, i+1);
         xCoord += 42;
         getContentPane().add(button[i]);
     }

     add(background);
     setTitle("Delux Keno");
     setSize(600,600);

     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     setLocationRelativeTo(null);

   }



   public static void main(String[] args)
   {
    SwingUtilities.invokeLater(new Runnable()
    {
        public void run()
        {
            System.setProperty("DEBUG_UI", "true");
            DeluxKenoMainWindow ex = new DeluxKenoMainWindow();
            ex.setVisible(true);
        }
    });
   }
   }
Craig Smith
  • 583
  • 3
  • 9
  • 26
0

another problem I found is that you need to use setBounds() for the JPanel for it to have any size. To do it the first way I tried this is the updated BackgroundImage class:

import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;

import javax.imageio.ImageIO;
import javax.swing.JPanel;


public class BackgroundImage extends JPanel{

    private BufferedImage img;
    private URL rUrl;
    public BackgroundImage()
    {
        super();

        try{
            rUrl = getClass().getResource("formBackground.png");

            img = ImageIO.read(rUrl);
        }
        catch(IOException ex)
        {
            ex.printStackTrace();
        }
        super.setBounds(0,0,600,600);

    }

    @Override
    protected void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        g.drawImage(img, 0, 0, getWidth(), getHeight(), this);

    }

}
Craig Smith
  • 583
  • 3
  • 9
  • 26