Question

I am attempting to overlay images on top of a background image using the standard java utilities. See the pictures below...

I have code that seems to create the background image (can you verify that it really works?) And I have created an extension of JPanel that I use to display images (the class is called ImagePanel)

However, when the program launches, the JFrame is only showing the second image, which then gets moved around as the window is resized.

I'd like to have the window open initially with the background image taking up the entirety of the window's space. I'd then like to have the second image displayed on top, at the location that I specify.

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

public class ImageTest {




public static void main(String[] args) {
    JFrame frame = new JFrame();
    frame.getContentPane().setLayout(null);

    JPanel backgroundPanel = new JPanel();
    ImageIcon backgroundImage = new ImageIcon("C:\\Documents and Settings\\Robert\\Desktop\\ClientServer\\Poker Table Art\\TableAndChairs.png");
    JLabel background = new JLabel(backgroundImage);
    background.setBounds(0, 0, backgroundImage.getIconWidth(), backgroundImage.getIconHeight());
    frame.getLayeredPane().add(background, new Integer(Integer.MIN_VALUE));
    backgroundPanel.setOpaque(false);
    frame.setContentPane(backgroundPanel);

    ImagePanel button = new ImagePanel("C:\\Documents and Settings\\Robert\\Desktop\\ClientServer\\Poker Table Art\\button.png");
    JPanel cardContainer = new JPanel(new FlowLayout());


    frame.getContentPane().add(cardContainer);

    cardContainer.add(button);
    cardContainer.setBounds(100, 600, 200, 200);

    frame.pack();
    frame.setVisible(true);
  }
}

alt text http://img189.imageshack.us/img189/9739/image1qi.jpg

alt text http://img186.imageshack.us/img186/1082/image2ll.jpg

Was it helpful?

Solution

You can set the preferred size of the background panel to the image's size:

backgroundPanel.setPreferredSize(new Dimension(
    backgroundImage.getIconWidth(), backgroundImage.getIconHeight()));

I would advocate following @camickr's approach. Not having previously experimented with setBounds() myself, here is a simple example to see the effect:

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

public class ImageTest {

    public static void main(String[] args) {

        JFrame frame = new JFrame();
        JLabel label = new JLabel(new ElliptIcon(380, 260, Color.red));
        label.setLayout(new GridLayout(2, 2));
        frame.setContentPane(label);

        for (int i = 0; i < 4; i++) {
            label.add(new JLabel(new ElliptIcon(100, 60, Color.blue)));
        }

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
    }

    private static class ElliptIcon implements Icon {

        private int w, h;
        private Color color;

        public ElliptIcon(int w, int h, Color color) {
            this.w = w;
            this.h = h;
            this.color = color;
        }

        @Override
        public void paintIcon(Component c, Graphics g, int x, int y) {
            g.setColor(color);
            g.fillOval(x, y, w, h);
        }

        @Override
        public int getIconWidth() { return w; }

        @Override
        public int getIconHeight() { return h; }
    }
}

OTHER TIPS

I'd like to have the window open initially with the background image taking up the entirety of the window's space.

Then simply create a JLabel with an Icon and use the label as the content pane of the frame. When you pack the frame it will be assume the size of the image.

I'd then like to have the second image displayed on top, at the location that I specify.

Use a null layout for the above label. Now you can create additional JLabels with Icons and add them to the content pane and postion them using setBounds.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top