participate


Swing - JFRAME background jpeg image [Locked]
This topic is locked
<<   Back to Forum  |   Give us Feedback
This topic has 10 replies on 1 page.
alungroome
Posts:53
Registered: 1/21/05
JFRAME background jpeg image   
Feb 22, 2005 8:36 AM

 
Hi,

I am trying to add a jpeg as a packground to my JFrame. I have read quite a few posts on how to do it in here, but none seem to work . Has anybody tried this and goit it working? If so some tips would be good.

Thanks
 
camickr
Posts:32,726
Registered: 2/27/98
Re: JFRAME background jpeg image   
Feb 22, 2005 11:38 AM (reply 1 of 10)  (In reply to original post )

 
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
 
public class BackgroundImage extends JFrame
{
	JScrollPane scrollPane;
	ImageIcon icon;
	Image image;
 
	public BackgroundImage()
	{
		icon = new ImageIcon("???.jpg");
 
		JPanel panel = new JPanel()
		{
			protected void paintComponent(Graphics g)
			{
				//  Dispaly image at at full size
				g.drawImage(icon.getImage(), 0, 0, null);
 
				//  Scale image to size of component
//				Dimension d = getSize();
//				g.drawImage(icon.getImage(), 0, 0, d.width, d.height, null);
 
				//  Fix the image position in the scroll pane
//				Point p = scrollPane.getViewport().getViewPosition();
//				g.drawImage(icon.getImage(), p.x, p.y, null);
 
				super.paintComponent(g);
			}
		};
		panel.setOpaque( false );
		panel.setPreferredSize( new Dimension(400, 400) );
		scrollPane = new JScrollPane( panel );
		getContentPane().add( scrollPane );
 
		JButton button = new JButton( "Hello" );
		panel.add( button );
	}
 
	public static void main(String [] args)
	{
		BackgroundImage frame = new BackgroundImage();
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setSize(300, 300);
		frame.setLocationRelativeTo( null );
		frame.setVisible(true);
	}
}

I've also been playing around with an ImagePanel which allows you do to much the same thing without overriding any methods. Its just got some basic funtionality, but you may find it usefull:

http://www.discoverteenergy.com/files/ImagePanel.java
 
santoshjava
Posts:5
Registered: 6/16/05
Re: JFRAME background jpeg image   
Feb 7, 2006 1:15 AM (reply 2 of 10)  (In reply to #1 )

 
hi,

thanks for code, it is working fine.
 
PhHein
Posts:10,710
Registered: 8/16/01
Re: JFRAME background jpeg image   
Feb 7, 2006 5:30 AM (reply 3 of 10)  (In reply to #2 )

 
hi,

thanks for code, it is working fine.

And for that you woke up a mummy?!
 
acupofjadaykeepsmicrofostaway
Posts:10
Registered: 6/5/06
made a mistake   
Jun 9, 2006 9:26 PM (reply 4 of 10)  (In reply to #1 )

 
deleted by poster

Message was edited by:
acupofjadaykeepsmicrofostaway
 
SnowFalcon
Posts:20
Registered: 5/26/07
Re: made a mistake   
Jun 3, 2007 2:28 AM (reply 5 of 10)  (In reply to #4 )

 
I have a problem with background image when i use internal frames.

I use a JDesktopPane which i puts into a container (Which has the ContentPane of the frame) and my internal frames are not displayed (Only the buttons and the textfields are displayed).

An idea ?

Message was edited by:
SnowFalcon
 
camickr
Posts:32,726
Registered: 2/27/98
Re: made a mistake   
Jun 3, 2007 3:08 PM (reply 6 of 10)  (In reply to #5 )

 
The code posted here works.

Since you have customized the code we don't know what your are doing.

So create your own postiing (no need to clutter this posting) and post your own demo code. That is, create a [url http://homepage1.nifty.com/algafield/sscce.html]Short, Self Contained, Compilable and Executable, Example Program[/url] (SSCCE) that demonstrates the incorrect behaviour, because I can't guess exactly what you are doing based on the information provided.

Don't forget to use the [url http://forum.java.sun.com/help.jspa?sec=formatting]Code Formatting Tags[/url] so the posted code retains its original formatting.
 
Brugal
Posts:1
Registered: 6/18/08
Re: JFRAME background jpeg image   
Jun 18, 2008 7:47 AM (reply 7 of 10)  (In reply to #1 )

 
I have a question about this piece. Im guessing you are overriding the method as soon as you create the object.
Is that whats going on?

JPanel panel = new JPanel()
		{
			protected void paintComponent(Graphics g)
			{
				//  Dispaly image at at full size
				g.drawImage(icon.getImage(), 0, 0, null);
 
				//  Scale image to size of component
//				Dimension d = getSize();
//				g.drawImage(icon.getImage(), 0, 0, d.width, d.height, null);
 
				//  Fix the image position in the scroll pane
//				Point p = scrollPane.getViewport().getViewPosition();
//				g.drawImage(icon.getImage(), p.x, p.y, null);
 
				super.paintComponent(g);
			}
		};
 
DarrylBurke
Posts:15,569
Registered: 7/2/07
Re: JFRAME background jpeg image   
Jun 18, 2008 8:07 AM (reply 8 of 10)  (In reply to #7 )

 
It's called an anonymous inner class.

Next time, create your own topic, Your question is not even related to the original post.

db
 
Faucheux
Posts:1
Registered: 7/27/09
Re: JFRAME background jpeg image   
Jul 27, 2009 5:32 AM (reply 9 of 10)  (In reply to #1 )

 
With some changes, the JPanel resize to the image size:

import javax.swing.*;
import java.awt.*;
import java.net.URL;
 
/ A <code>JFrame</code> with a background image */ 
public class BackgroundImage extends JFrame
{
	final ImageIcon icon;
	final boolean isRedimensionable;
 
	public BackgroundImage(URL url, boolean isRedimensionable) {
		icon = new ImageIcon(url.getFile());
		this.isRedimensionable = isRedimensionable;
 
		JPanel panel = new JPanel()
		{
			protected void paintComponent(Graphics g)
			{
				if (BackgroundImage.this.isRedimensionable) {
					//  Scale image to size of component
					Dimension d = getSize();
					g.drawImage(icon.getImage(), 0, 0, d.width, d.height, null);
 
				} else  {
					//  Dispaly image at at full size
					g.drawImage(icon.getImage(), 0, 0, null);
				}
 
				super.paintComponent(g);
			}
		};
		panel.setOpaque( false );
		getContentPane().add( panel );
 
		Dimension dim = new Dimension(icon.getIconWidth()  +getInsets().left+  getInsets().right, icon.getIconHeight()  +getInsets().top+  getInsets().bottom) ;
		panel.setPreferredSize(dim);
		setResizable(isRedimensionable);
		pack();
 
		JButton button = new JButton( "Hello" );
		panel.add( button );
	}
 
	public static void main(String [] args)
	{
		BackgroundImage frame = new BackgroundImage(BackgroundImage.class.getResource("loginDialogBackground.png"), true);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setLocationRelativeTo( null );
		frame.setVisible(true);
	}
}
 
PhHein
Posts:10,710
Registered: 8/16/01
Re: JFRAME background jpeg image   
Jul 27, 2009 11:39 AM (reply 10 of 10)  (In reply to #9 )

 
Faucheux , thanks, but please don't post to years old threads. Locking.
 
This topic has 10 replies on 1 page.
Back to Forum
 
Read the Developer Forums Code of Conduct

Click to email this message Email this Topic

Edit this Topic
  
 
 
Forums Statistics
    Users Online : 27
  • Guests : 138

About Sun forums
  • Sun Forums is a large collection of user generated discussions. It is here to help you ask questions, find answers, and participate in discussions.

    Check out our guide on Getting started with Sun Forums for a full walkthrough of how to best leverage the benefits of this community.

Powered by Jive Forums