Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Swing Background Picture  RSS feed

 
Ranganath .S.Junpal
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I have just created a JFrame with a JPanel. i have welcome class which extends JPanel, in its paintcomponent(Grapics g),method i have painted a Picture as a background to the JPanel. My problem is that when i start the JFrame it doesnt have that pic painted there.. when ever i resize it.. then that picture will appear .. Please if anybody know the soln for this problem..Pls let me know..The codes is as follows..

Code
---------------------------------------------------------------------------
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.image.ImageObserver;
import java.net.URL;
import javax.swing.JPanel;

public class RemoteMainPanel extends JPanel implements ImageObserver {

private Image bgImage;
RemoteMainPanel()
{
URL ImgUrl = getClass().getResource("bg.jpg");
bgImage =Toolkit.getDefaultToolkit().getImage(ImgUrl);
}
public void paint(Graphics g) {
g.drawImage(bgImage, 0, 0, this);
}
}

Thank you
Regards
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> when ever i resize it.. then that picture will appear .

symptom of

frame.setVisible(true);
frame.add(panel);

which should be the other way around
frame.add(panel);
frame.setVisible(true);


also you should be using paintComponent() for a JPanel

public void paintComponent()(Graphics g) {
super.paintComponent(g);//<------also should have this line
g.drawImage(bgImage, 0, 0, this);
}
 
Jeff Albertson
Ranch Hand
Posts: 1780
 
Ranganath .S.Junpal
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hI guys,

Thanks a lot for your reply.. Actually i am using Eclipse IDe with VEP plugin.. Here in this Frame.. I am extending the JFrame.. SO i dont have any thing like this.setVisible(true with in the constructer.. and another thing is that.. This JPanel is on a TabbedPane.. So still the PIC is not visible till i resize it.. plz let mek now the soln

thank you
Regards
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if you don't have
setVisible(true);
how do you display it when run outside of the IDE?

if this is the job of 'VEP plugin' (whatever that is), perhaps you might be
better off posting your question in a forum that deals with VEP plugins
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!