Search code examples
javaeclipseurlbackgroundimageicon

how to add a background (URL) to a JPanel in java eclipse


we just started using java. we want to use an image from the internet and make it the background of our Jpanel. can somebody help us please? we tried using the code below :

 public class Achtergrond3 extends JPanel { 
        private ImageIcon img;
        private JLabel label;

        public Achtergrond3() {
             img = new ImageIcon("res/textures/newbackground.jpg"); 
             label= new JLabel(img);

             this.add(label);
        }

        public void paint(Graphics g) {
            super.paint(g);
            Graphics2D g2d = (Graphics2D) g;
        }
}

Solution

  • I think this link will help you...

    Simplest way to set image as JPanel background

    It talks about how to set image as JPanel background