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 4 of 4

Thread: Adding JLabel on other Jlabel

  1. #1
    Junior Member
    Join Date
    Mar 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Red face Adding JLabel on other Jlabel

    I am trying to add a car on a race track,
    both are image and car must be placed as a JLabel to move it on track,
    like car racing
    Please view on code and help me on this
    code i did was: but it doesnot overlap

    import javax.swing.ImageIcon;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.*;

    public class ImageTest {

    public static void main(String[] args) {
    JPanel backgnd= new JPanel();
    JPanel panel = new JPanel();
    ImageLabel label = new ImageLabel(new ImageIcon("C:/Projects/BasicLevel/src/car.png"));
    ImageLabel label1 = new ImageLabel(new ImageIcon("C:/Projects/BasicLevel/src/WithTrack/RacingTrack.jpg"));
    backgnd.add(label1);
    label.setLocation(2, 3);
    panel.add(label);

    JFrame frame = new JFrame();
    frame.setLayout(new OverlayLayout(panel));
    frame.add(label);
    frame.getContentPane().add(backgnd);
    frame.pack();
    frame.setVisible(true);
    }
    }

    class ImageLabel extends JLabel {

    public ImageLabel(String img) {
    this(new ImageIcon(img));
    }

    public ImageLabel(ImageIcon icon) {
    setIcon(icon);
    // setMargin(new Insets(0,0,0,0));
    setIconTextGap(0);
    // setBorderPainted(false);
    setBorder(null);
    setText(null);
    setSize(icon.getImage().getWidth(null), icon.getImage().getHeight(null));
    }

    }


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Adding JLabel on other Jlabel

    When you say "it doesn't overlap", what does it do instead? Does only one display at a time? Does the wrong one show up on top? Something else?

    When posting code, please use the highlight tags. Unformatted code makes my eyes bleed.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Mar 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Adding JLabel on other Jlabel

    Quote Originally Posted by KevinWorkman View Post
    When you say "it doesn't overlap", what does it do instead? Does only one display at a time? Does the wrong one show up on top? Something else?

    When posting code, please use the highlight tags. Unformatted code makes my eyes bleed.
    hi Kevin,
    This displays only one image Label, but not both,
    I have tried with JLayeredPane, though it dint work, only one label is displayed
    I am not wanting to add as Graphics image every time while running a car in track path, it flickered ..
    What i want is adding Jlabel on top of car track, and moving that label on road track...for this i need to find coordinates of that road track too..
    appreciating your help,
    Last edited by mike416; March 29th, 2012 at 11:45 AM.

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Adding JLabel on other Jlabel

    Well, I'm confused by what your code is supposed to do. Let me see if I can step through it...

    You have a JPanel backgnd, which you add a JLabel label1 to.
    You have a JPanel panel, which you add a JLabel label to.
    There is a third JPanel, the JFrame's ContentPane, which you give an OverlayLayout, but you pass panel into that instead. Why?
    You then add label to the frame, which actually adds it to the ContentPane, although label has already been added to panel. Why?
    Then you add backgnd to the frame's ContentPane, although you have already added the label.

    None of this makes much sense to me. I would think you want a single JPanel (probably the ContentPane) that you use an OverlayLayout with and add both JLabels to that JPanel. Not three JPanels which you add the JLabels to multiple times.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. JLabel
    By Poseidon in forum AWT / Java Swing
    Replies: 2
    Last Post: February 18th, 2012, 04:39 PM
  2. adding text to JLabel during actionPerformed
    By that_guy in forum AWT / Java Swing
    Replies: 1
    Last Post: February 12th, 2012, 04:08 PM
  3. Everything but JLabel is displaying
    By Jacksontbh in forum What's Wrong With My Code?
    Replies: 2
    Last Post: July 3rd, 2011, 10:30 PM
  4. A problem with JLabel!
    By niklas in forum AWT / Java Swing
    Replies: 5
    Last Post: December 19th, 2010, 05:51 AM
  5. A problem with JLabel!
    By niklas in forum Member Introductions
    Replies: 1
    Last Post: December 14th, 2010, 06:03 PM

Tags for this Thread