rampriya_sb
asked on
Is it possible to put Panel on the Image
I have a doubt. Can i place a panel on top of the image in the frame?
I have a JFrame where i want the image to be displayed... and i have a JPanel where i want it to be displayed but on top of the image. I am talking about the skin of the GUI... In VC++, you can do that.. is there a way to do in java??
Thanks in advance
Ram
I have a JFrame where i want the image to be displayed... and i have a JPanel where i want it to be displayed but on top of the image. I am talking about the skin of the GUI... In VC++, you can do that.. is there a way to do in java??
Thanks in advance
Ram
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
public void paintComponent(Graphics g) {
super.paintComponent(g);
// call drawImage here
}
You'll still be able to add components leaving the image there
super.paintComponent(g);
// call drawImage here
}
You'll still be able to add components leaving the image there
(oh that's overriding the paintComponent of JPanel btw)
ASKER
is it JPanel u r talking about or the JFrame?
JPanel
If you want the image drawn behind the panel, you need to override paintComponent() (see CEHJ's comment) in your JFrame instance.
You can't do that actually - JFrame has no paintComponent. Easiest to add another JPanel to the frame and then place the final one on that
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
>>Set BorderLayout as your layout manager for the JFrame's content pane...
Well of course, it's still not clear what the intention is, so this is probably a little bit too assumptive ;-)
Well of course, it's still not clear what the intention is, so this is probably a little bit too assumptive ;-)
>> probably a little bit too assumptive
possibly, but not if he wants the image to cover the complete area of the JFrame
possibly, but not if he wants the image to cover the complete area of the JFrame
ASKER
I want the image to cover the entire area and place a Panel on the image...
Should be OK then
yep, I think that's it! ;-)
ASKER
As suggested, i overrode paintComponent method and put the image
class TestPanel extends JPanel
{
JPanel pnlAnotherPanel;
public TestPanel()
{
pnlAnotherPanel = new JPanel();
pnlAnotherPanel.setBackgro und(Color. white);
pnlAnotherPanel.setBounds( new Rectangle(30,30,60,80));
this.add(pnlAnotherPanel,n ull);
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
Image img=Toolkit.getDefaultTool kit().getI mage("test img.jpg");
g.drawImage(img,0,0,this);
}
}
When i try to put this JPanel class to the Frame and try to runthe program, only the image is displayed and not the panel(pnlAnotherPanel).... . so what shud i do for that? i tried setting the panel to opaque, but didnt help...
Then i tried putting
class TestPanel extends JPanel
{
JPanel pnlAnotherPanel;
public TestPanel()
{
pnlAnotherPanel = new JPanel();
pnlAnotherPanel.setBackgro
pnlAnotherPanel.setBounds(
this.add(pnlAnotherPanel,n
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
Image img=Toolkit.getDefaultTool
g.drawImage(img,0,0,this);
}
}
When i try to put this JPanel class to the Frame and try to runthe program, only the image is displayed and not the panel(pnlAnotherPanel)....
Then i tried putting
ASKER
i m not finding any luck still.... shud i put the panel in the paintComponent method??
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
If you instead want the panel to float above your existing component hierarchy then add the panel to the glass pane or a layered pane.
http://java.sun.com/docs/books/tutorial/uiswing/components/rootpane.html
http://java.sun.com/docs/books/tutorial/uiswing/components/rootpane.html
Three way point split
ASKER