views:

153

answers:

3

How do I add background image on this code? i tried everything but the image just won't show.

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class sampleProg extends JFrame {

private Image img;

public void sampleProg(){


ImageIcon icon = new ImageIcon("bg.jpg");

       img=icon.getImage();
}

public void paint (Graphics g){

    g.drawImage(img,0,0,getSize().width,getSize().height,this);
    super.paint(g);

}



JButton button = new JButton ("Result");
JButton button2 = new JButton ("Clear");


JLabel label = new JLabel ("Full Name");
JLabel label2 = new JLabel ("Age");
JLabel label3 = new JLabel ("English");
JLabel label4 = new JLabel ("Mathematics");
JLabel label5 = new JLabel ("Science");
JLabel label6 = new JLabel ("Social Studies");
JLabel label7 = new JLabel ("Height in cm");
JLabel label8 = new JLabel ("Weight in lbs");
JLabel label9 = new JLabel ("Message");
JLabel label10 = new JLabel ("Average");
JLabel label11 = new JLabel ("Remarks");
JLabel label12 = new JLabel ("Laurize Albarracin");



JTextField text = new JTextField ("");
JTextField text2 = new JTextField ("");
JTextField text3 = new JTextField ("");
JTextField text4 = new JTextField ("");
JTextField text5 = new JTextField ("");
JTextField text6 = new JTextField ("");
JTextField text7 = new JTextField ("");
JTextField text8 = new JTextField ("");
JTextField text9 = new JTextField ("");
JTextField text10 = new JTextField ("");
JTextField text11 = new JTextField ("");

int average;

JPanel background = new JPanel();
JFrame frame = new JFrame();

public sampleProg (String str){
    super(str);

    background.setLayout (null);
    background.setBounds (30,50,90,20);

    button.setBounds(350,170,90,20);
    button2.setBounds(450,170,90,20);


    label.setBounds(30,90,90,20);
    label2.setBounds(30,130,90,20);
    label3.setBounds(30,170,90,20);
    label4.setBounds(30,210,90,20);
    label5.setBounds(30,250,90,20);
    label6.setBounds(30,290,90,20);
    label7.setBounds(350,90,90,20);
    label8.setBounds(350,130,90,20);
    label9.setBounds(350,210,90,20);
    label10.setBounds(350,250,90,20);
    label11.setBounds(350,290,90,20);
    label12.setBounds(300,35,150,20);




    text.setBounds(130,90,190,20);
    text2.setBounds(130,130,190,20);
    text3.setBounds(130,170,190,20);
    text4.setBounds(130,210,190,20);
    text5.setBounds(130,250,190,20);
    text6.setBounds(130,290,190,20);
    text7.setBounds(450,90,250,20);
    text8.setBounds(450,130,250,20);
    text9.setBounds(450,210,250,20);
    text10.setBounds(450,250,250,20);
    text11.setBounds(450,290,250,20);

    button.addActionListener (new ActionListener () {
        public void actionPerformed (ActionEvent e){

            text9.setText("Hi! "+text.getText()+"you are"+text2.getText()+"years of age and now you stand"+text7.getText()+"in cm while you're weight is"+text8.getText()+"in lbs. These are your remark and average");
            int English = Integer.parseInt (text3.getText());
            int Mathematics = Integer.parseInt (text4.getText());
            int Science = Integer.parseInt (text5.getText());
            int SocialStudies = Integer.parseInt(text6.getText());

            average = (English+Mathematics+Science+SocialStudies)/4;
            text10.setText(Integer.toString(average)); 

        }   
    }
    );

    button2.addActionListener (new ActionListener(){
        public void actionPerformed(ActionEvent e){
            text.setText("");
            text2.setText("");
            text3.setText("");
            text4.setText("");
            text5.setText("");
            text6.setText("");
            text7.setText("");
            text8.setText("");
            text9.setText("");
            text10.setText("");
            text11.setText("");

        }
    });

    background.add(button);
    background.add(button2);
    background.add(label);
    background.add(label2);
    background.add(label3);
    background.add(label4);
    background.add(label5);
    background.add(label6);
    background.add(label7);
    background.add(label8);
    background.add(label9);
    background.add(label10);
    background.add(label11);
    background.add(label12);

    background.add(text);       
    background.add(text2);
    background.add(text3);
    background.add(text4);
    background.add(text5);
    background.add(text6);
    background.add(text7);
    background.add(text8);
    background.add(text9);
    background.add(text10);
    background.add(text11);

    getContentPane().add(background);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    pack();
     }

    public sampleProg(){

    setLayout(new FlowLayout());
}

     public static void main (String[]args){    

     sampleProg frame = new sampleProg("Swing Application");
     frame.setSize(730,350);
     frame.show();

}


}
+1  A: 

This may have the answer you are looking for: http://stackoverflow.com/questions/523767/how-to-set-background-image-in-java

Mondain
i tried that but the background still doesn't show..
laurize
+1  A: 

Your code never sets the img field. Move the code from the void sampleProg() method into your constructor. You should also set the background panel's background to a transparent color for the image to show through. Also, you should paint the image on the background panel rather than on the frame, otherwise it will always be covered up. You can create an anonymous panel and move your paint() code into it instead.

There are cleaner ways to do this found in the link Mondain provided.

public class sampleProg extends JFrame {
    JPanel background = new JPanel() {
        public void paint (Graphics g){
            g.drawImage(img,0,0,getSize().width,getSize().height,this);
            super.paint(g);
        }
    };

    public sampleProg(string str) {
        //...

        ImageIcon icon = new ImageIcon("bg.jpg");
        img=icon.getImage();

        //...

        Color transparent = new Color(0, true);
        background.setBackground(transparent);
    }
}
Jeff M
i tried the code but the image only blinks once and appears only if i resize the window
laurize
I forgot to mention a change I did in my own tests. Please see my updated answer.
Jeff M
A: 

Background Panel gives a couple of solutions depending on your requirements.

camickr