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

    Default Overlapping Pictures in a JFrame -- How?

    Hi
    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: http://www.java-forums.org/awt-swing...ap-images.html


    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" ));
        
        panel.add(l1);
        panel.add(l2);
        
        
        JFrame frame = new JFrame ("Display multiple images from files.");
        frame.getContentPane().add (panel);
        frame.pack();
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);   
        
      }
      
    }
    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 10:50 AM.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,423
    Blog Entries
    7
    Rep Power
    27

    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,

    Jos
    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, 02:42 AM
  2. Replies: 3
    Last Post: 04-25-2013, 06:47 PM
  3. Replies: 22
    Last Post: 07-25-2012, 09:51 PM
  4. Overlapping JLabels
    By littledrummerboy in forum New To Java
    Replies: 4
    Last Post: 05-24-2011, 04:47 PM
  5. overlapping forms
    By keerthi_y19 in forum New To Java
    Replies: 4
    Last Post: 08-26-2008, 10: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
  •