Results 1 to 2 of 2
  1. #1
    Goerky is offline Member
    Join Date
    Dec 2015
    Rep Power

    Default Overlapping Pictures in a JFrame -- How?

    I would like to overlap 2 or more Pictures in an JFrame with coordinats.
    The Pictures (will) have transparency.

    I have found complicated programs, which do the thing, but they set
    transparency in the code or are just to complicated for me to figure out,
    which code-fragments i need and which not.

    Like this:

    I have just this not-working program:

    Java Code:
    import javax.swing.ImageIcon;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    public class Test {
      public static void main(String[] args) {
        //Without JPanel images would be added to JFrame on top of each other.
        //That way only last image would be visible.
        JPanel panel = new JPanel();
        JLabel l1 = new JLabel();
        JLabel l2 = new JLabel();
        l1.setLocation(100, 100);
        l2.setLocation(50, 300);
        l1.setIcon(new ImageIcon("C:/Users/-USERNAME-/Desktop/test/1.png"));
        l2.setIcon(new ImageIcon ("C:/Users/-USERNEME-/Desktop/test/2.png" ));
        JFrame frame = new JFrame ("Display multiple images from files.");
        frame.getContentPane().add (panel);
    But the pictures arent reacting to a change of the coordinats, the always are displayed in
    the upper, right corner of the Frame...
    I have read something about panels in an Forum (can't remember the name). The
    Code was an Example that should work, but ist'n working for me....
    Last edited by Goerky; 12-21-2015 at 11:50 AM.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Voorschoten, the Netherlands
    Blog Entries
    Rep Power

    Default Re: Overlapping Pictures in a JFrame -- How?

    If you create a new JPanel without arguments in the constructor, a FlowLayout is created for the JPanel; you can't move any of the child components then; create your Jpanel as: new JPanel(null) so that no layout manager will be created. For the transparency issue: a JLabel isn't transparent; I'd do it like this: extend from the JPanel class and directly paint those two images in the overridden paintComponent(Graphics g) method.

    kind regards,

    Build a wall around Donald Trump; I'll pay for it.

Similar Threads

  1. Mac: Can't read input file, when adding pictures to a JFrame
    By swedishfished in forum New To Java
    Replies: 4
    Last Post: 08-27-2014, 03:42 AM
  2. Replies: 3
    Last Post: 04-25-2013, 07:47 PM
  3. Replies: 22
    Last Post: 07-25-2012, 10:51 PM
  4. Overlapping JLabels
    By littledrummerboy in forum New To Java
    Replies: 4
    Last Post: 05-24-2011, 05:47 PM
  5. overlapping forms
    By keerthi_y19 in forum New To Java
    Replies: 4
    Last Post: 08-26-2008, 11:30 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts