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));
}
}
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.
Re: Adding JLabel on other Jlabel
Quote:
Originally Posted by
KevinWorkman
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,
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.