java - How to change automatically a Jlabel -


hi trying create java desktop application can move images automatically every 5 seconds. able this. problem use own image , put images on single jlabel. did following code

how can 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("http://i.stack.imgur.com/ncshu.png");                urls[1] = new url("http://i.stack.imgur.com/uvhn4.png");                urls[2] = new url("http://i.stack.imgur.com/s89on.png");                urls[3] = new url("http://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

" problem want use own image , want put image on single jlabel."

the program seems working web urls. guess problem how use own file images.

what should place images in package in project, like

projectroot          src             resources                    image1.png                    image2.png 

then can replace url this

//urls[0] = new url("http://i.stack.imgur.com/ncshu.png"); urls[0] = imagepanel.class.getresource("/resources/image1.png"); urls[1] = imagepanel.class.getresource("/resources/image2.png"); 

see more @ embedded-resource wiki info learn more how use embedded resources.


this code looks familar ;-)


edit

as per question title , question jlabel, code doesn't use jlabel. paints image. if want use jlabel instead, can use imagelabel.seticon(images[randon()]) change label icon. can in timer listener


Comments

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -