Adding Background Image

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 4827 Views - Last Post: 02 September 2010 - 06:33 AM Rate Topic: -----

#1 rize  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 30-August 10

Adding Background Image

Posted 30 August 2010 - 08:03 PM

Hey i have this code and im not really sure what code to use to add background image to it, any help would do thanks!

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


public class sampleProg extends JFrame {

	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 static void main (String[]args){
		 	
		 
		 
		 sampleProg frame = new sampleProg("Swing Application");
		 frame.setSize(730,350);
		 frame.show();

	}
	}



Is This A Good Question/Topic? 0
  • +

Replies To: Adding Background Image

#2 b0ng01  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 38
  • View blog
  • Posts: 169
  • Joined: 16-July 10

Re: Adding Background Image

Posted 30 August 2010 - 08:09 PM

Override paintComponent in your JFrame and use graphics2d .drawImage()

http://download-llnw...Graphics2D.html
Was This Post Helpful? 0
  • +
  • -

#3 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2695
  • View blog
  • Posts: 10,556
  • Joined: 15-July 08

Re: Adding Background Image

Posted 30 August 2010 - 08:11 PM

Actually, it would probably be better if you added everything to a JPanel, not directly to the JFrame. That's only because it's easier to mess with graphics on a JPanel than it is on a JFrame.
Was This Post Helpful? 0
  • +
  • -

#4 giuseppe105  Icon User is offline

  • D.I.C Regular

Reputation: 9
  • View blog
  • Posts: 438
  • Joined: 15-May 08

Re: Adding Background Image

Posted 30 August 2010 - 08:22 PM

why use graphics 2d? graphics is just as fast is it not?
Was This Post Helpful? 0
  • +
  • -

#5 rize  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 30-August 10

Re: Adding Background Image

Posted 30 August 2010 - 08:30 PM

View PostDogstopper, on 30 August 2010 - 07:11 PM, said:

Actually, it would probably be better if you added everything to a JPanel, not directly to the JFrame. That's only because it's easier to mess with graphics on a JPanel than it is on a JFrame.


so ill change this:

public class sampleProg extends JFrame {


to

public class sampleProg extends JPanel {


?
Was This Post Helpful? 0
  • +
  • -

#6 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2695
  • View blog
  • Posts: 10,556
  • Joined: 15-July 08

Re: Adding Background Image

Posted 30 August 2010 - 08:38 PM

Not quite...You still need a JFrame. You just need to have a JPanel in addition.
Was This Post Helpful? 0
  • +
  • -

#7 b0ng01  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 38
  • View blog
  • Posts: 169
  • Joined: 16-July 10

Re: Adding Background Image

Posted 30 August 2010 - 08:55 PM

I would make my sampleProg class extend JPanel and where ever that is being called from create a JFrame to add that panel to.
There is no advantage in this instance of using graphics2d over graphics. I just have a habit of always casting my graphics to graphics2d.
Was This Post Helpful? 0
  • +
  • -

#8 rize  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 30-August 10

Re: Adding Background Image

Posted 30 August 2010 - 10:59 PM

View Postb0ng01, on 30 August 2010 - 07:55 PM, said:

I would make my sampleProg class extend JPanel and where ever that is being called from create a JFrame to add that panel to.
There is no advantage in this instance of using graphics2d over graphics. I just have a habit of always casting my graphics to graphics2d.


ok got it. but where can i add the JFrame?
can i use this code to add the background image for jpanel?:
http://robbamforth.w...ge-to-a-jpanel/
Was This Post Helpful? 0
  • +
  • -

#9 b0ng01  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 38
  • View blog
  • Posts: 169
  • Joined: 16-July 10

Re: Adding Background Image

Posted 30 August 2010 - 11:09 PM

Wherever your main class is the one with the main method create your JFrame there. And yes, that is how you can go about it.
Was This Post Helpful? 0
  • +
  • -

#10 pbl  Icon User is online

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

Reputation: 8019
  • View blog
  • Posts: 31,127
  • Joined: 06-March 08

Re: Adding Background Image

Posted 31 August 2010 - 04:49 PM

Asked so many times, I wrote a Snippet about it

http://www.dreaminco...snippet5599.htm
Was This Post Helpful? 0
  • +
  • -

#11 rize  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 30-August 10

Re: Adding Background Image

Posted 31 August 2010 - 10:34 PM

View Postpbl, on 31 August 2010 - 03:49 PM, said:

Asked so many times, I wrote a Snippet about it

http://www.dreaminco...snippet5599.htm


thanks but im still not sure how to integrate it with the code i posted, any help? :)
Was This Post Helpful? 0
  • +
  • -

#12 Mercurial  Icon User is offline

  • D.I.C Head

Reputation: 18
  • View blog
  • Posts: 178
  • Joined: 06-November 09

Re: Adding Background Image

Posted 31 August 2010 - 11:58 PM

Your class extends a JPanel. Meaning everything you add on the screen, you add to that panel(this.add()). Now, you want a background image... Look at Pbl's snippet, he's overriding paint() method. Paint method paints your component( your JPanel in this case), it's borders and it's children(everything on your panel) - meaning when you override paint, besides doing the three things up mentioned, you're giving instructions to Paint() about what else it needs to draw(in your case it's a background image).

What you should do is override the paint method and let it do it's work(super.paint()), and just add the drawimage code (just like in pbl's snippet).
Was This Post Helpful? 0
  • +
  • -

#13 rize  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 30-August 10

Re: Adding Background Image

Posted 01 September 2010 - 12:47 AM

i tried this code but the image is appearing behind, like it only blinks when i resize the window:

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


import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.FlowLayout;

	
public class sampleProg extends JFrame {
	
	public void paint (Graphics g){
		
		Image a=Toolkit.getDefaultToolkit().getImage("C:\\Documents and Settings\\user\\Desktop\\bg.jpg");
		g.drawImage(a,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();

	}
	

	}



Was This Post Helpful? 0
  • +
  • -

#14 pbl  Icon User is online

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

Reputation: 8019
  • View blog
  • Posts: 31,127
  • Joined: 06-March 08

Re: Adding Background Image

Posted 01 September 2010 - 07:21 PM

A paint() method should be executed, ideally, in less than 1/60 of a second
really not a good place to do Disk I/O (reading from disk your bg.jpg file)
Read it in your constructor only once and then use your already read Image in memory in your paint() method
Was This Post Helpful? 0
  • +
  • -

#15 rize  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 30-August 10

Re: Adding Background Image

Posted 02 September 2010 - 02:56 AM

i tried this code but the background image doesn'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();

	}
	

	}



This post has been edited by rize: 02 September 2010 - 03:37 AM

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2