0

Hi I am trying to create a Java desktop application where I can move images automatically every 5 seconds. I am able to do this. The problem is that I would like to use my own image and I would like to put all the images on a single Jlabel. I did following code

how can I get this?

private static class ImagePanel extends JPanel {

        URL[] urls;
        ImageIcon[] image;
        BufferedImage[] images;
        Random rand = new Random();
        JLabel imagelabel;

        public ImagePanel() {
            urls = new URL[4];

            try {


               urls[0] = new URL("https://i.stack.imgur.com/NCsHu.png");
               urls[1] = new URL("https://i.stack.imgur.com/UvHN4.png");
               urls[2] = new URL("https://i.stack.imgur.com/s89ON.png");
               urls[3] = new URL("https://i.stack.imgur.com/QEK2o.png");


               images = new BufferedImage[5];
               images[0] = ImageIO.read(urls[0]);
               images[1] = ImageIO.read(urls[1]);
               images[2] = ImageIO.read(urls[2]);
               images[3] = ImageIO.read(urls[3]);

            } catch (MalformedURLException ex) {
                ex.printStackTrace();
            } catch (IOException ex) {
                ex.printStackTrace();
            }

            setBackground(Color.BLACK);

            Timer timer = new Timer(5000, new ActionListener(){
                @Override
                public void actionPerformed(ActionEvent e) {
                    repaint();
                }
            });
            timer.start();
        }

        private int random() {
            int index = rand.nextInt(4);
            return index;
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            BufferedImage img = images[random()];
            g.drawImage(img, 0, 0, 400, 400, 0, 0,
                    img.getWidth(), img.getHeight(), this);
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(400, 400);
        }
    }
}

Thanks in advance

Anto
  • 4,265
  • 14
  • 63
  • 113
user3456343
  • 252
  • 3
  • 7
  • 21

1 Answers1

3

" but problem this i want to use my own image and i want to put all image on single jlabel."

The program seems to be working with the web URLs. So i guess your problem is just how to use your own file images.

What you should do is place the images in a package in your project, like

ProjectRoot
         src
            resources
                   image1.png
                   image2.png

Then you can replace the URL with something like this

//urls[0] = new URL("https://i.stack.imgur.com/NCsHu.png");
urls[0] = ImagePanel.class.getResource("/resources/image1.png");
urls[1] = ImagePanel.class.getResource("/resources/image2.png");

See more at the embedded-resource wiki info to learn more about how to use embedded resources.


This code looks familar ;-)


EDIT

As per your question title and question about the JLabel, the code doesn't actually use a JLabel. It paints the image. If you do want to use the JLabel instead, then what you can do is just use imageLabel.setIcon(images[randon()]) to change the label icon. You can do that in the Timer listener

Community
  • 1
  • 1
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720