-1

I just started Java GUI and I want to set a background image to JFrame by over-riding the paintComponent(Graphics g) method so that I will be able to add the child components over the image. I have looked at other answers but the code is too complicated for a beginner

Please use the following code to explain how this is done:

public class staffGUI extends JFrame {

public staffGUI(){
    super("Staff Management");

    this.setVisible(true);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setLocationRelativeTo(null);
    this.setResizable(false);
    this.pack();
}       
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Charlie
  • 3,113
  • 3
  • 38
  • 60
  • *"I have looked at other answers but the code is too complicated for a beginner"* Beginners should be consulting the [tutorial on custom painting](http://docs.oracle.com/javase/tutorial/uiswing/painting/) for this task. *"Please use the following code to explain how this is done:"* So.. you want *us* to finish the task, and explain it to you in a way so simple that (even) you can understand it? Voting to close - 'too broad'.. – Andrew Thompson Mar 17 '15 at 10:17
  • I know this question has been asked many times but every a user posts their own source code which make the code difficult to read – Charlie Mar 17 '15 at 11:13
  • I already know how to do this using JLabel but then I cant place the child components over the image – Charlie Mar 17 '15 at 11:13
  • I have also read many textbooks on Java GUI but none explains how to use this function specifically – Charlie Mar 17 '15 at 11:15
  • Simply put, don't use a `JPanel` as the primary surface. Override it's `paintComponent` method and apply it as the frames content pane and then treat the frame as normal. You will end up with a far more flexible solution. For [example](http://stackoverflow.com/questions/13791984/add-an-background-image-to-a-panel/13792503#13792503) – MadProgrammer Mar 17 '15 at 12:01

1 Answers1

3
public class staffGUI extends JFrame {

    public staffGUI(){
        super("Staff Management");

        this.setContentPane(new MyContentPane("C://somePath//image.jpg"));

        this.setVisible(true);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setLocationRelativeTo(null);
        this.setResizable(false);
        this.pack();
    }

    private class MyContentPane extends JPanel
    {
         private BufferedImage image;

         public MyContentPane(String path){
               try{
                     image = ImageIO.read(new File(path));
               }catch(IOException e){
                     e.printStackTrace();

                     image = new BufferedImage(100 , 100 , BufferedImage.TYPE_INT_ARGB);
                     Graphics g = image.getGraphics();
                     g.setColor(Color.blue);
                     g.fillRect(0 , 0 , 100 , 100);
                     g.dispose();
         }

         public void paintComponent(Graphics g){
              super.paintComponent(g);

              g.drawImage(image , 0 , 0 , getWidth() , getHeight() , null);
         }
    }
}       

Painting in the frame is always done on the content pane of the frame. The content pane is also where components are added. MyContentPane loads a BufferedImage from a given path. If loading fails, a blue rectangle is created as image. By setting the content pane in your example code to an instance of MyContentPane, you make MyContentPane render the complete inner area of the frame.

  • So where do I load the image? – Charlie Mar 17 '15 at 11:25
  • oops, sry my mistake, i thought you just needed a backgroundcolor. one sec, i'll edit it to load an image –  Mar 17 '15 at 12:23
  • ok, i corrected my mistake, now it loads an image - atleast as soon as someone enters a valid path –  Mar 17 '15 at 12:36