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.

Results 1 to 2 of 2

Thread: Bringing an image in front of another

  1. #1
    Junior Member
    Join Date
    Oct 2013
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Bringing an image in front of another

    import javax.swing.*;
     
    import java.awt.*;
     
    public class ImageTutorial extends JFrame	{
     
    		private ImageIcon image;
    		private JLabel label;
    		private ImageIcon mario;
    		private JLabel mariolabel;
    		private ImageIcon luige;
    		private JLabel luigelabel;
     
    		ImageTutorial()	{
     
     
    		    setLayout(new GridBagLayout());
     
     
     
    			image = new ImageIcon(getClass().getResource("main/castle.png"));
    			label = new JLabel(image);
    			label.setHorizontalAlignment(JLabel.CENTER);
     
     
    			mario = new ImageIcon(getClass().getResource("main/mario.jpg"));
    			mariolabel = new JLabel(mario);
    			label.add(mariolabel);
     
     
    			luige = new ImageIcon(getClass().getResource("main/luige.jpg"));
    			luigelabel = new JLabel(luige);
    			label.add(luigelabel);
     
    			add(label);
    			pack();
    			setVisible(true);
     
     
    		}
     
     
    		public static void main(String [] args)
    		{
    			ImageTutorial gui = new ImageTutorial();
    			gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    			gui.setVisible(true);
    			gui.pack();
    			gui.setTitle("Mario vs Luige Quiz Board Game");
     
    		}
     
     
     
    }

    How do I make it so that my luigelabel and mariolabel are infront of my label. Currently i can only see my label (background image) :S


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 262 Times in 231 Posts

    Default Re: Bringing an image in front of another

    A label does not work the way it appears you are trying to make it work. The add method comes from the Component class up the hierarchy and is intended for other uses.
    (I now make the assumption that you intend to move mario and luige around):
    I think what would work better is some custom painting. This tutorial on custom painting should help.

Similar Threads

  1. vertical, front to back and string into decimal
    By arl13i1 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 3rd, 2013, 06:15 PM
  2. Trying to create a GUI front end for a TELNET application
    By arkroan in forum What's Wrong With My Code?
    Replies: 1
    Last Post: July 7th, 2013, 06:43 PM
  3. How to place a number in front of a string?!!
    By WantHelp in forum What's Wrong With My Code?
    Replies: 8
    Last Post: July 8th, 2011, 11:15 PM
  4. Stupid thing can't find image icon right in front of it!!!
    By javapenguin in forum What's Wrong With My Code?
    Replies: 14
    Last Post: July 9th, 2010, 01:02 AM