1

Hi I create a class ImageSlide2 and I have thread and only one time a picture change why ?

I don't know why only time a picture change. The slide show have to all time display a changed picture This is my code :

public class ImageSlide2 extends JLabel {

    private Timer tm;
    private int xx = 0;
    String[] list = {
        "C:/Users/022/workspace22/EkranLCD/res/images/1.png", //0
        "C:/Users/022/workspace22/EkranLCD/res/images/3.png" //1     
    };

    public ImageSlide2(int x, int y, int width, int height) {
        setBounds(x, y, width, height);
        //Call The Function SetImageSize
        SetImageSize(list.length - 1);

        try {
            Thread.sleep(1000);
        } catch (InterruptedException e1) {
            e1.printStackTrace();
        }

        new Thread(new Runnable() {
            public void run() {
                try {
                    System.out.println(xx);
                    SetImageSize(xx);
                    xx += 1;
                    if (xx >= list.length) {
                        xx = 0;
                    }
                } catch (Exception ie) {
                }
            }
        }).start();

    }
    //create a function to resize the image 

    public void SetImageSize(int i) {

        ImageIcon icon = new ImageIcon(list[i]);
        Image img = icon.getImage();
        Image newImg = img.getScaledInstance(Config.xSize / 2, Config.ySize / 2, Image.SCALE_SMOOTH);
        ImageIcon newImc = new ImageIcon(newImg);
        setIcon(newImc);
    }
}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • Thread.sleep(1000); locked current JVM, read more in Oracle tutorial Concurencz in Swing, to use Swing Timer instead of Thread.sleep(1000); and new Thread(new Runnable() { – mKorbel Sep 26 '16 at 07:20
  • Hint: method names go camelCase - always. They never start uppercase. – GhostCat Sep 26 '16 at 07:20
  • 1
    Please read about [concurrency in swing](https://docs.oracle.com/javase/tutorial/uiswing/concurrency/) and about [swing timer](https://docs.oracle.com/javase/tutorial/uiswing/misc/timer.html). – Sergiy Medvynskyy Sep 26 '16 at 07:20
  • @SergiyMedvynskyy Yeap, I looked. It is written "The programmer **does not need** to provide code that explicitly creates these threads" and not "The programmer is forbidden to use threads on her/his own creation" – Adrian Colomitchi Sep 26 '16 at 07:36
  • all the usual crap advise – gpasch Sep 26 '16 at 07:44
  • @gpasch maybe you have good idea ? –  Sep 26 '16 at 07:54
  • `setBounds(x, y, width, height);` 1) Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). .. – Andrew Thompson Sep 26 '16 at 10:14
  • .. 2) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 3) One way to get image(s) for an example is to hot link to images seen in [this Q&A](http://stackoverflow.com/q/19209650/418556). – Andrew Thompson Sep 26 '16 at 10:14

2 Answers2

1

Try to change your method by using javax.swing.Timer. Something like this:

public ImageSlide2(int x, int y, int width, int height) {
    setBounds(x, y, width, height);
    //Call The Function SetImageSize
    SetImageSize(list.length - 1);

    final Timer t = new Timer(1000, new ActionListener() {
        public void actionPerformed(ActionEvent ae) {
            try {
                System.out.println(xx);
                SetImageSize(xx);
                xx += 1;
                if (xx >= list.length) {
                    xx = 0;
                }
            } catch (Exception ie) {
            }
        }
    });
    // t.setRepeats(false); // when you want to execute it at once
    t.start();
}
Sergiy Medvynskyy
  • 11,160
  • 1
  • 32
  • 48
  • That's work ok but I want to do this on thread becouse when I added this class from my project I see that my project don't work good –  Sep 26 '16 at 10:11
  • @jpok sorry but I cannot debug your project. Probably you should stop the timer somewhere. – Sergiy Medvynskyy Sep 26 '16 at 10:17
1

A solution based on thread is the following - you need to add the sleep in a loop that continuously paints and then sleeps etc:

public ImageSlide2(int x, int y, int width, int height) {
    setBounds(x, y, width, height);
    //Call The Function SetImageSize
    SetImageSize(list.length - 1);


    new Thread(new Runnable() {
        public void run() {
            while(true)
            try {
                System.out.println(xx);
                SetImageSize(xx);
                xx += 1;
                if (xx >= list.length) {
                    xx = 0;
                }
                Thread.sleep(1000);
            } catch (Exception ie) { ie.printStackTrace();
            }

        }
    }).start();

}
gpasch
  • 2,672
  • 3
  • 10
  • 12