java - How to image shuffle on jlabel? -


i creating java desktop application want shuffle image in every 3 sec. able this, problem want use single jlabel image shuffle in every 3 sec , have code multiple jlabel

here code found here. want use single jlabel. how can achieve this?

/**  * @see https://stackoverflow.com/a/22423511/230513  * @see https://stackoverflow.com/a/12228640/230513  */ public class imageshuffle extends jpanel {      private list<icon> list = new arraylist<icon>();     private list<jlabel> labels = new arraylist<jlabel>();     private timer timer = new timer(1000, new actionlistener() {          @override         public void actionperformed(actionevent e) {             update();         }     });      public imageshuffle() {         this.setlayout(new gridlayout(1, 0));         list.add(uimanager.geticon("optionpane.erroricon"));         list.add(uimanager.geticon("optionpane.informationicon"));         list.add(uimanager.geticon("optionpane.warningicon"));         list.add(uimanager.geticon("optionpane.questionicon"));         (icon icon : list) {             jlabel label = new jlabel(icon);             labels.add(label);             this.add(label);         }         timer.start();     }      private void update() {         collections.shuffle(list);         int index = 0;         (jlabel label : labels) {             label.seticon(list.get(index++));         }     }      private void display() {         jframe f = new jframe("imageshuffle");         f.setdefaultcloseoperation(jframe.exit_on_close);         f.add(this);         f.pack();         f.setlocationrelativeto(null);         f.setvisible(true);     }      public static void main(string[] args) {         eventqueue.invokelater(new runnable() {              @override             public void run() {                 new imageshuffle().display();             }         });     } } 

this variation of original example has unfortunate (but perhaps instructive) problems:

  • a new instance of random created @ each iteration; 1 required.

  • the expression r.nextint(3) + 1 never selects first or last element of list.

  • the use of numeric literals may cause program fail if size of list changes.

instead, shuffle() list , choose first element.

private void update() {     collections.shuffle(list);     label.seticon(list.get(0)); } 

as tested:

import java.awt.eventqueue; import java.awt.gridlayout; import java.awt.event.actionevent; import java.awt.event.actionlistener; import java.util.arraylist; import java.util.collections; import java.util.list; import javax.swing.icon; import javax.swing.jframe; import javax.swing.jlabel; import javax.swing.jpanel; import javax.swing.timer; import javax.swing.uimanager;  /**  * @see https://stackoverflow.com/a/22631012/230513  * @see https://stackoverflow.com/a/22423511/230513  * @see https://stackoverflow.com/a/12228640/230513  */ public class imageshuffle extends jpanel {      private list<icon> list = new arraylist<icon>();     private jlabel label = new jlabel();     private timer timer = new timer(1000, new actionlistener() {          @override         public void actionperformed(actionevent e) {             update();         }     });      public imageshuffle() {         this.setlayout(new gridlayout(1, 0));         list.add(uimanager.geticon("optionpane.erroricon"));         list.add(uimanager.geticon("optionpane.informationicon"));         list.add(uimanager.geticon("optionpane.warningicon"));         list.add(uimanager.geticon("optionpane.questionicon"));         label.seticon(uimanager.geticon("optionpane.informationicon"));          timer.start();     }      private void update() {         collections.shuffle(list);         label.seticon(list.get(0));     }      private void display() {         jframe f = new jframe("imageshuffle");         f.setdefaultcloseoperation(jframe.exit_on_close);         f.add(this);         f.add(label);         f.pack();         f.setlocationrelativeto(null);         f.setvisible(true);     }      public static void main(string[] args) {         eventqueue.invokelater(new runnable() {              @override             public void run() {                 new imageshuffle().display();             }         });     } } 

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 -