Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Page 1 of 2 12 LastLast
Results 1 to 25 of 40

Thread: HELP - placing picture on top of another picture in Java - JLayerPane

  1. #1
    Member
    Join Date
    Sep 2011
    Posts
    30
    My Mood
    Nerdy
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question HELP - placing picture on top of another picture in Java - JLayerPane

    Hi ..

    I am trying to place a picture on top of another one in java ..

    I have a Tshirt picture which I am going to place it in the center of my GUI
    Also I have a logo I want to put it on top of that picture

    I used JLayerPane but I guess I made mistake which i couldn't notes and solve.

    this is part of my code.

     
    static JComboBox colours, sizes, logos;   							// Variables for drop box 
    static JLabel picture, logo; 			
     
    private static String[] fileName = {"src/white.gif", "src/green.gif", // array of the file names will 
    					     "src/blue.gif", "src/brown.gif",  //
        					     "src/black.gif", "src/red.gif",   //
    					   "src/gray.gif"}; 	           // store the T-shirt pics.                      
     
    private static String[] logosList = {"src/likeMe.gif", "src/followMe.gif",
    	 							   "src/youtube.gif"};
     
     
    private Icon[] pics = { new ImageIcon(fileName[0]), new ImageIcon(fileName[1]),                     // array of images will store 
              new ImageIcon(fileName[2]), new ImageIcon(fileName[3]), new ImageIcon(fileName[4]),  // the the all images.
    		 			new ImageIcon(fileName[5]), new ImageIcon(fileName[5]), new ImageIcon(fileName[6])};
     
    private Icon[] logs = { new ImageIcon(logosList[0]), new ImageIcon(logosList[1]),  //To hold logos for T-shirts.
    				 new ImageIcon(logosList[2])};
     
     
    inside my constructor
     
    			pnlNorth = new JPanel ( new GridLayout ( 3, 3));
     
     pnlNorth.add(colours = new JComboBox (fileName));                      // put the colours list in the ( fileName array ) in list for us.
    			pnlNorth.add(sizes = new JComboBox ( sizeList )); // put the sizes list in the ( sizeList array ) in list for us.
    			pnlNorth.add(logos = new JComboBox ( logosList ));// put the logos list in the ( logoList array ) in list for us.
    			pnlNorth.add(lblCustomizeYourOrder);
     
    			// Then add it to the container. 
    			add(pnlNorth, BorderLayout.NORTH);
     
     
     
     
    	// JLayerPane to put 2ed layer "logo" on top of the picture.
    			jlayerPane = new JLayeredPane();
    			jlayerPane.setPreferredSize(new Dimension (300,300));
    			jlayerPane.setLayout(new FlowLayout());
     
    			picture = new JLabel (pics[0]);
    			logo = new JLabel (logs [0]);
     
    			// Manually set layout the components, and add it.
    			jlayerPane.add(picture, new Integer (50));
    			jlayerPane.add(logo, new Integer (49)); 
    			// Add the JLayerPane to the container.
    			add(jlayerPane);
     
    			// Manually set layout the components.
    			picture.setBounds(600, 100, picture.getWidth(), picture.getHeight()); 
    			logo.setBounds(600, 100, logo.getWidth(), logo.getHeight());


    everything perfect except placing the logo on the top of the tshirt
    how can I fix my code to place the logo on the tshirt.
    the output it attached as picture ..

    thanks guys ..

    TshirtProblem.JPG


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: HELP - placing picture on top of another picture in Java - JLayerPane

    Can you make a small, complete program that compiles, executes and shows the problem?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Sep 2011
    Posts
    30
    My Mood
    Nerdy
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: HELP - placing picture on top of another picture in Java - JLayerPane

    import java.awt.*;
    import java.awt.event.*;
    import java.util.HashMap;
    import javax.swing.*;
     
     
    public class SmallProgram extends JFrame
    {
     
    	// These variables for the main window.
    	static JTextField txtName, txtCustomerName, txtEmail, txtAddress;
    	static JLabel lblName, lblCustomerName, lblEmail, lblAddress, lblEmpty3,
    			      lblCustomizeYourOrder,lblEmpty, lblEmpty2 , lblTypeInfo, lblEmpty1;
    	static  JButton   btnCheckOut, btnAdd, btnQuit;
    	static JComboBox colours, sizes, logos;   							// Variables for drop box 
    	static JLabel picture, logo; 										// Labels.
    	static JPanel pnlNorth, pnlSouth, pnlWest, pnlInWest;
    	static JLayeredPane jlayerPane;										// To make layer on top of other one. 
     
     
    	private static String[] fileName = {"src/white.gif", "src/green.gif", // array of the images names will 
    										"src/blue.gif", "src/brown.gif",  //
    										"src/black.gif", "src/red.gif",   //
    										"src/gray.gif"}; 				  // store the T-shirt pics.                      
    	private static String[] logosList = {"src/likeMe.gif", "src/followMe.gif",
    										"src/youtube.gif"};
    	private static String[] sizeList = {"XS", "S", "M", "L", "XL", "XXL"};  // To hold Sizes for T-shirts.
    	private Icon[] pics = { new ImageIcon(fileName[0]), new ImageIcon(fileName[1]),      // array of images will store 
    			 new ImageIcon(fileName[2]), new ImageIcon(fileName[3]), new ImageIcon(fileName[4]),  // the the all images.
    	 			new ImageIcon(fileName[5]), new ImageIcon(fileName[5]), new ImageIcon(fileName[6])};
     
    	private Icon[] logs = { new ImageIcon(logosList[0]), new ImageIcon(logosList[1]),  //To hold logos for T-shirts.
    			 new ImageIcon(logosList[2])};
     
     
     
    	// "Window" GUI.
    	public SmallProgram ()
    	{
     
     
    		 ///////////////////////////////////////////////////////////////////////////////
    		/////// ALSO HERE I CANN'T CHANGE THE NAME OF THE FILES USING HASH MAP ////////
    	   ///////////////////////////////////////////////////////////////////////////////
     
    		// Hash map to change the files name to the colours names.
    		HashMap <String, String> fileMap = new HashMap <String, String>();
    		fileMap.put("src/white.gif", "WHITE");
     
     
    		// Set the panels settings.
     
    		pnlNorth = new JPanel ( new GridLayout ( 3, 3));
    		pnlSouth =  new JPanel(new GridLayout(1,4,8,8));
    		pnlWest = new JPanel(new GridLayout(10,2));
     
     
    		// Adding the components to the panels.
    		// add the dropList to the panel.
     
    		pnlNorth.add(colours = new JComboBox (fileName)); // put the colours list in the ( fileName array ) in list for us.
    		pnlNorth.add(sizes = new JComboBox ( sizeList )); // put the sizes list in the ( sizeList array ) in list for us.
    		pnlNorth.add(logos = new JComboBox ( logosList ));// put the logos list in the ( logoList array ) in list for us.
     
     
    		// Then add it to the container. 
    		add(pnlNorth, BorderLayout.NORTH);
     
    		// Then add it to the container.
    		add(pnlSouth, BorderLayout.SOUTH);
     
    		// Then add it to the west panel.
    		add(pnlWest, BorderLayout.EAST);
     
     
    		   ///////////////////////////////////////
    		  ///////////////////////////////////////
    		 ///////// HERE IS MY PROBLEM //////////
    		///////////////////////////////////////
    	   ///////////////////////////////////////
     
    		// JLayerPane to put 2ed layer "logo" on top of the picture.
    		jlayerPane = new JLayeredPane();
    		jlayerPane.setPreferredSize(new Dimension (300,300));
    		jlayerPane.setLayout(new FlowLayout());
     
    		picture = new JLabel (pics[0]);
    		logo = new JLabel (logs [0]);
     
    		// Manually set layout the components, and add it.
    		jlayerPane.add(picture, new Integer (50));
    		jlayerPane.add(logo, new Integer (49)); 
    		// Add the JLayerPane to the container.
    		add(jlayerPane);
     
    		// Manually set layout the components.
    		picture.setBounds(600, 100, picture.getWidth(), picture.getHeight()); 
    		logo.setBounds(600, 100, logo.getWidth(), logo.getHeight());
     
     
    		 /////////////////////////
    		//// Action Listeners ///
    	   /////////////////////////
     
     
    		// Add an action listener to buttons.
    		// The add button.
     
     
    		colours.addActionListener
    		(new ActionListener()
    		{
    			public void actionPerformed(ActionEvent e)
    			{
    				colours.getSelectedItem();
     
    			}
    		});
     
    		sizes.addActionListener
    		(new ActionListener()
    		{
    			public void actionPerformed(ActionEvent e)
    			{
    				sizes.getSelectedItem();
     
    			}
    		});
     
    		logos.addActionListener
    		(new ActionListener()
    		{
    			public void actionPerformed(ActionEvent e)
    			{
    				logos.getSelectedItem();
     
    			}
    		});
     
     
    		//
     
     
    		// Add an action listener to the colours JComboBox.
    		colours.addItemListener(
    			new ItemListener()
    			{
    				// will choose a picture if the button has been selected.
    				public void itemStateChanged (ItemEvent event)
    				{
    					// if button selected pick the picture.
    					if(event.getStateChange() == ItemEvent.SELECTED)
    						picture.setIcon(pics[colours.getSelectedIndex()]);
    				}
    			}
    		);
     
     
    		// Add an action listener to the logos to JComboBox.
    		logos.addItemListener(
    				new ItemListener()
    				{
    					// will choose a logo if the button has been selected.
    					public void itemStateChanged (ItemEvent event)
    					{
    						// if button selected pick the logo.
    						if(event.getStateChange() == ItemEvent.SELECTED)
    							logo.setIcon(logs[logos.getSelectedIndex()]);
    					}
    				}
    			);
     
     
    }// End the main GUI contractor.
     
    	// The Main method.  
    	public static void main (String []args)
    	{
     
    		SmallProgram gui = new SmallProgram();
    		gui.setSize(550,420);
    		gui.setResizable(false);
    		gui.setVisible(true);
    		gui.setLocationRelativeTo(null);
    		gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
     
    	}// End Main.
     
     
    }

    here is a part of my code ...
    having two problems .. the one that I just mentioned .. which is the layer on top of the other one ..
    also .. changing the name of the file names using hash map...
    thanks

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: HELP - placing picture on top of another picture in Java - JLayerPane

    You have too many image file references in the code. Can you minimize the number of images to one or two for testing?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Sep 2011
    Posts
    30
    My Mood
    Nerdy
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: HELP - placing picture on top of another picture in Java - JLayerPane

    Sure .. you can delete whatever you want if you want to test the program ...
    these names are in the drop list box ..
    I tried to test it in different ways ..

    you mean that I have used JLayerPane correctly ??

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: HELP - placing picture on top of another picture in Java - JLayerPane

    It be better if you made the test code so it is doing what you want and shows the problem.

    Have you looked at and experimented with the sample code in the tutorial?
    Go to the API doc for the JLayeredPane and click on the link to the "How To Use .." tutorial.
    Last edited by Norm; April 22nd, 2012 at 01:39 PM.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Member
    Join Date
    Sep 2011
    Posts
    30
    My Mood
    Nerdy
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: HELP - placing picture on top of another picture in Java - JLayerPane

    Yes I did .. and I just did it again ..
    I used JLayerPane as it suppose to be used, however, it keeps giving me the two pictures beside each other...
    Honestly this error drove me crazy ....

    I changed in my code now but the same exactly the same ..

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: HELP - placing picture on top of another picture in Java - JLayerPane

    Compare your code to the tutorial's code. You are doing some things differently.

    One problem I see it the value for x of 600? where is that?
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Member
    Join Date
    Sep 2011
    Posts
    30
    My Mood
    Nerdy
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: HELP - placing picture on top of another picture in Java - JLayerPane

    I followed up again my code line by line and I changed the numbers many times ...
    I don't think that the problem is the numbers maybe i miss some codes or so ..??
    I changed x to 100 and less and more ..


    here is the following up ...

    First :

    calling the pics and logos by their names

    private static String[] fileName = {"src/white.gif"}; // store the T-shirt pics.
    private static String[] logosList = {"src/likeMe.gif"}; // store the logos.


    Secondly:

    final Icon[] pics = { new ImageIcon(fileName[0])};

    final Icon[] logs = { new ImageIcon(logosList[0])};


    After that :

    Create JLayerPane ...

    jlayerPane = new JLayeredPane();
    jlayerPane.setPreferredSize(new Dimension (300,300));
    jlayerPane.setLayout(new FlowLayout());

    // The JLabel for pictures and logos.
    picture = new JLabel (pics[0]);
    logo = new JLabel (logs [0]);

    // Add the pics and logos to the JLayerPane.
    jlayerPane.add(picture );
    jlayerPane.add(logo );

    // Manually set layout the components.
    picture.setBounds(15, 100, picture.getWidth(), picture.getHeight());
    logo.setBounds(15, 100, logo.getWidth(), logo.getHeight());


    // Add the JLayerPane to the container.
    add(jlayerPane);


    But i ended up with the same problem ...

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: HELP - placing picture on top of another picture in Java - JLayerPane

    giving me the two pictures beside each other...
    Think about that. What code would put the pictures besides each other?
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Member
    Join Date
    Sep 2011
    Posts
    30
    My Mood
    Nerdy
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: HELP - placing picture on top of another picture in Java - JLayerPane

    Quote Originally Posted by Norm View Post
    Think about that. What code would put the pictures besides each other?
    If i am not mistaken

    this line of code ...

    picture.setBounds(15, 100, picture.getWidth(), picture.getHeight());
    logo.setBounds(15, 100, logo.getWidth(), logo.getHeight());


    in this line of code i am manually setting the layout for the components.

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: HELP - placing picture on top of another picture in Java - JLayerPane

    Where do those statements place the components? How are their locations "besides each other"?
    If you don't understand my answer, don't ignore it, ask a question.

  13. #13
    Member
    Join Date
    Sep 2011
    Posts
    30
    My Mood
    Nerdy
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: HELP - placing picture on top of another picture in Java - JLayerPane

    ( X 15, Y 100 )
    And the pictures and logos JLabel has been created ..

    These two Labels has been added to the JLayerPane ..

    The LayerPane has been created and we sat the the layout of it { setLayout(new FlowLayout() } ..

    After that we added it to the window { add(jlayerPane); } ...

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: HELP - placing picture on top of another picture in Java - JLayerPane

    Do you want the two images besides each other? The code you are using does that.

    Did you compare your code to how the tutorial's sample code: LayeredPaneDemo does it?
    If you don't understand my answer, don't ignore it, ask a question.

  15. #15
    Member
    Join Date
    Sep 2011
    Posts
    30
    My Mood
    Nerdy
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: HELP - placing picture on top of another picture in Java - JLayerPane

    yes I tried to get from that page what I need ... that page has been opened in my browser since last Thu ..
    I honestly could not fine the mistake .. maybe because I have been working too much and i left this small thing that missed my code..

  16. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: HELP - placing picture on top of another picture in Java - JLayerPane

    The clue is: what would cause the two components to be besides each other instead of at the location where you set their bounds? That cause can be seen in a difference between your code and the code in the tutorial's example.
    If you don't understand my answer, don't ignore it, ask a question.

  17. #17
    Member
    Join Date
    Sep 2011
    Posts
    30
    My Mood
    Nerdy
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: HELP - placing picture on top of another picture in Java - JLayerPane

    you mean the 15, 100 ?

    I tried using the other way which is

    // Manually set layout the components, and add it.
    jlayerPane.add(picture, new Integer (1));
    jlayerPane.add(logo, new Integer (0));

  18. #18
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: HELP - placing picture on top of another picture in Java - JLayerPane

    Shouldn't the setBounds() with the same x,y locations put the components on top of each other?
    But they are not on top of each other. What other code is putting them where they are going?
    What code controls where components added to a container goes?
    If you don't understand my answer, don't ignore it, ask a question.

  19. #19
    Member
    Join Date
    Sep 2011
    Posts
    30
    My Mood
    Nerdy
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: HELP - placing picture on top of another picture in Java - JLayerPane

    I sat both of their X, Y same numbers ... is not that right ?
    I don't know any other code that will do the same as i found in google..
    your last question i did not quit understand it .. you mean BorderLayout, GridLayout .... ??

  20. #20
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: HELP - placing picture on top of another picture in Java - JLayerPane

    Yes, the layout manager will position components. Does the tutorial use a layout manager for the layered pane?
    If you don't understand my answer, don't ignore it, ask a question.

  21. #21
    Member
    Join Date
    Sep 2011
    Posts
    30
    My Mood
    Nerdy
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: HELP - placing picture on top of another picture in Java - JLayerPane

    Quote Originally Posted by Norm View Post
    Yes, the layout manager will position components. Does the tutorial use a layout manager for the layered pane?
    Some examples uses others no..
    I tried all of them nothing change except the place of them .. Up or down left or right ..
    That's all

  22. #22
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: HELP - placing picture on top of another picture in Java - JLayerPane

    Look at the LayerPaneDemo.java program.

    What examples with a layered pane used a layout manager? I only looked at that one.
    If you don't understand my answer, don't ignore it, ask a question.

  23. #23
    Member
    Join Date
    Sep 2011
    Posts
    30
    My Mood
    Nerdy
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: HELP - placing picture on top of another picture in Java - JLayerPane

    I just looked at it ... and tried to delete my layout manager to match it with the example .. but my luck today is too bad ..
    I am sorry my english is lill bad so i have to read once and twice .... you mean i should not usee the layout ??

  24. #24
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: HELP - placing picture on top of another picture in Java - JLayerPane

    What happens when you remove setting the layout manager?
    If you don't understand my answer, don't ignore it, ask a question.

  25. #25
    Member
    Join Date
    Sep 2011
    Posts
    30
    My Mood
    Nerdy
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: HELP - placing picture on top of another picture in Java - JLayerPane

    Quote Originally Posted by Norm View Post
    What happens when you remove setting the layout manager?
    The tshirt will not show on the window

Page 1 of 2 12 LastLast

Similar Threads

  1. [SOLVED] Directions Buttons needs picture
    By Darkcore123 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: December 14th, 2011, 11:19 AM
  2. Java Picture Processor with PGM files and Multi Arrays
    By snow_mac in forum Object Oriented Programming
    Replies: 2
    Last Post: November 28th, 2011, 09:55 PM
  3. Converting a picture made up of 0s and 1s into a colored picture??
    By Kranti1992 in forum Java Theory & Questions
    Replies: 10
    Last Post: November 21st, 2011, 06:25 PM
  4. Showing a picture in a GUI
    By joachim89 in forum AWT / Java Swing
    Replies: 1
    Last Post: February 15th, 2010, 02:42 PM
  5. Need help with a Picture!
    By Scout in forum Java Theory & Questions
    Replies: 1
    Last Post: October 12th, 2009, 05:33 PM