Background Image question...

Hi,

I am creating a program where I need a background image to be scrolled.

I have a class that extends JFrame and in that class, I have several JPanels.

For one of the JPanels, I want a background picture. However, the JPanel will be a smaller size than the picture and I want the user to be able to scroll over to the right or left and view the rest of the image.

Also, I am not doing any animation in this program.

Any suggestions would be greatly appreciated.

For the JPanel that you have your image on, make it a custom JPanel. Override paintComponent() to paint the image. Override getPreferredSize(), getMaximumSize() and getMinimumSize() to return the size of your image. Place your custom JPanel into a JScrollpane. The JScroll pane will take care of all the scrolling work for you.

Thanks for the tip.

I actually have another question too…

Is it possible to have the JFrame be set as
setUndecorated(true)

while also having a JMenuBar with JMenu’s?

I tried putting it on, but there is a wierd effect where a white box appears above teh JMenuBar?

Any ideas?

No idea. I tried it and did not have a problem. Maybe you have something extra there. Here is what I used.


import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
 
public class Test extends JFrame { 
 
    public Test() {
        JMenuBar b = new JMenuBar();
        b.add(new JMenu("File"));
        setJMenuBar(b);
        setUndecorated(true);
        setSize(400,300);
    } 
    public static void main(String args[]) throws Exception {
        Test t = new Test();
        t.show();
    } 
}  

Hmmm… that is basically what I have too (although I have my MenuBar as an extended class.

Maybe I do have something extra.

Do you think having JPanels with a setbounds (a,b,c,d) would make a difference?

I’ll try it…

I figured it out…

I was trying to add the MenuBar and was also setting it. Getting rid of the ‘add’ fixed the problem.