0

Basically I am fairly new to java still and imported a simple png over another, It's a proof of concept for myself and I would assume is very informal coding. None the less I don't know how to move the second png as it puts it self at (0,0,0) in the top left once the application opens.

public class DisplayImage extends JFrame {

    public DisplayImage() {
        initUI();
    }

    private ImageIcon loadImage() {
        ImageIcon ii = new ImageIcon("/Users/980057130/Desktop/pixil-frame-0.png");
        return ii;
    }

    private ImageIcon loadImage1() {
        ImageIcon iii = new ImageIcon("/Users/980057130/Desktop/Dynamic-Dungeon.png");
        return iii;
    }

    private void initUI() {                   
        ImageIcon ii = loadImage();
        ImageIcon iii = loadImage1();

        JLabel label = new JLabel(iii);
        JLabel label1 = new JLabel(ii);

        createLayout(label);
        createLayout(label1);

        label = new JLabel();

        Container contentPane = getContentPane();
        contentPane.add(new JScrollPane(label), "Center");

        setTitle("Dynamic Dungeon");
        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }
}

Any help is greatly appreciated.

Rajeev Atmakuri
  • 888
  • 1
  • 10
  • 22
  • 1) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 2) One way to get image(s) for an example is to hot link to images seen in [this Q&A](http://stackoverflow.com/q/19209650/418556). E.G. [This answer](https://stackoverflow.com/a/10862262/418556) hot links to an image embedded in [this question](https://stackoverflow.com/q/10861852/418556). – Andrew Thompson Nov 03 '18 at 00:01

2 Answers2

0

Welcome to stack overflow and to the friendly java community. Few pointers for you on this. If you are interested in drawing images then looking in to painting on a canvas might be a better approach. Using Labels is more in to the realms of laying out a GUI and when you are doing that you won't typically be trying to draw labels on top of one another.

You could check out this question Drawing Canvas on JFrame

which involves someone drawing on to a canvas, which you could try to use to draw your images.

Link19
  • 586
  • 1
  • 18
  • 47
0

If you want to center one image on top of another the easiest way is to let a layout manager do this for you.

So the basic logic would be:

JLabel foreground = new JLabel( new ImageIcon() );
JLabel background = new JLabel( new ImageIcon(...) );
background.setLayout( new GridBagLayout() );
background.add(foreground, new GridBagConstraints() );
JScrollPane scrollPane = new JScrollPane( background );
add(scrollPane, BorderLayout.CENTER);

When using the GridBagLayout with default constraints any component added will automatically be centered in the space available.

Note, a JLabel is not really designed to be a container, so this approach will only work if the foreground image is smaller than the background image.

camickr
  • 321,443
  • 19
  • 166
  • 288