Results 1 to 14 of 14

Thread: SlideShow help

  1. #1
    aortell24 is offline Member
    Join Date
    Jun 2012
    Location
    St.louis, Missouri
    Posts
    49
    Rep Power
    0

    Default SlideShow help

    Hello, guys I am new to this forum and to programing I am currently in my second term of my first programming class which is Java.After the first day of class I fell in love and changed my major to software engineering . Any way I am trying to make a slide show for an exercise in my class I have been at this for two days and all I can get it to run is the last slide. It will wait for the timer to run up and display the last slide.If someone could take a look at my code and let me know what I did wrong that would be greatly appreciated. I am not looking for some one to write my code for me,
    just some pointers. Any help will be greatly appreciated thanks.

    Sorry I am not sure how to post code here!

    Java Code:
    import java.util.*;
    import javax.swing.event.*;
    import javax.swing.*;
    import java.awt.*;
    import java.awt.image.*;
    import java.awt.event.*;
    import java.util.Random;
    import javax.swing.Timer;
    /**
     * Created with IntelliJ IDEA.
     * User: root
     * Date: 6/2/12
     * Time: 7:58 PM
     * To change this template use File | Settings | File Templates.
     */
    public class SlideShow extends JFrame
    {
        private ImageIcon image0;
        private ImageIcon image1;
        private ImageIcon image2;
        private ImageIcon image3;
        private ImageIcon image4;
        private ImageIcon image5;
        private ImageIcon image6;
        private ImageIcon image7;
        private ImageIcon image8;
        private ImageIcon image9;
        private ImageIcon image10;
        private ImageIcon image11;
        private ImageIcon image12;
        private ImageIcon image13;
        private ImageIcon image14;
        private ImageIcon image15;
        private JPanel panel;
        private JLabel label;
    
        public SlideShow()
        {
            setSize(800,600);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setTitle("Slide Show");
    
            label= new JLabel();
            panel = new JPanel();
    
            Timer timer = new Timer(13000,new TimerListener());
            timer.start();
    
            panel.add(label);
            add(panel);
            setVisible(true);
        }
        private class TimerListener implements ActionListener
        {
            public void actionPerformed(ActionEvent e)
            {
                    image0= new ImageIcon("/root/Images/Slides/slide0.jpg");
                    label.setIcon(image0);
                    repaint();
    
                    image1 = new ImageIcon("/root/Images/Slides/slide1.jpg");
                    label.setIcon(image1);
                    repaint();
    
                    image2= new ImageIcon("/root/Images/Slides/slide2.jpg");
                    label.setIcon(image2);
                    repaint();
    
                    image3= new ImageIcon("/root/Images/Slides/slide3.jpg");
                    label.setIcon(image3);
                    repaint();
    
                    image4 = new ImageIcon("/root/Images/Slides/slide4.jpg");
                    label.setIcon(image4);
                    repaint();
    
                    image5= new ImageIcon("/root/Images/Slides/slide5.jpg");
                    label.setIcon(image5);
                    repaint();
    
                    image6= new ImageIcon("/root/Images/Slides/slide6.jpg");
                    label.setIcon(image6);
                    repaint();
    
                    image7 = new ImageIcon("/root/Images/Slides/slide7.jpg");
                    label.setIcon(image7);
                    repaint();
    
                    image8= new ImageIcon("/root/Images/Slides/slide8.jpg");
                    label.setIcon(image8);
                    repaint();
             }
        }
        public static void main(String args[])
        {
            new SlideShow();
        }
    }
    Last edited by Fubarable; 06-03-2012 at 03:53 AM. Reason: code tags added

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: SlideShow help

    Hello and welcome. Please feel free to post your code at any time.

    Also, you'll likely want to sprinkle your code with debug statements of some kind, such as System.out.println(...) statements that tell you what is happening at various points in your code as your code runs. Also, you'll want to make sure that you don't squash your GUI's event thread by putting Thread.sleep(...) or a long loop in this thread.
    Last edited by Fubarable; 06-03-2012 at 03:48 AM.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: SlideShow help

    OK, I see your problem. Your Timer will repeatedly call its actionPerformed method every 13 seconds. That means that the whole actionPerformed method will run each 13 seconds. All of the images will swoop by too fast to see until the last one which will stay.

    A solution is:
    • Create the ImageIcons just once, and in the class's constructor.
    • Place all the ImageIcons in an array of ImageIcon[], let's say this is called iconArray.
    • Create an int array index field, say called iconIndex, and set it to 0.
    • In your Timer's actionPerformed method, add just one ImageIcon to the JLabel, the iconArray[iconIndex] icon.
    • After doing this, in actionPerformed, increment the iconIndex by 1, but check to make sure that it isn't equal to or larger than the length of the array.
    • If it is, then set it to 0.
    • A short-cut way of doing the last 2 steps is to use the mod (%) operator.


    Edit: please note that I added [code] [/code] tags to your original post to help the code retain its formatting.

  4. #4
    aortell24 is offline Member
    Join Date
    Jun 2012
    Location
    St.louis, Missouri
    Posts
    49
    Rep Power
    0

    Default Re: SlideShow help

    Thank you very much.

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: SlideShow help

    You're welcome!

  6. #6
    aortell24 is offline Member
    Join Date
    Jun 2012
    Location
    St.louis, Missouri
    Posts
    49
    Rep Power
    0

    Default Re: SlideShow help

    One more question if you dont mind how do I tie the iconIndex to the iconArray.

  7. #7
    aortell24 is offline Member
    Join Date
    Jun 2012
    Location
    St.louis, Missouri
    Posts
    49
    Rep Power
    0

    Default Re: SlideShow help

    Here is what I came up with
    Java Code:
    import sun.rmi.runtime.NewThreadAction;
    
    import javax.swing.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    /**
     * Created with IntelliJ IDEA.
     * User: root
     * Date: 6/2/12
     * Time: 9:41 PM
     * To change this template use File | Settings | File Templates.
     */
    public class Exercise16_13 extends JFrame
    {
        ImageIcon[] iconArray = new ImageIcon[25];
        int iconIndex = 0;
        JLabel label;
        JPanel panel;
    
    
        Exercise16_13 ()
        {
            panel = new JPanel();
            label = new JLabel();
            add(panel);
             setTitle("Slide Show");
            panel.add(label);
    
    
    
            label = new JLabel();
            panel = new JPanel();
    
             for(int i =0; i < iconArray.length; i++)
             {
                 iconArray[i] = new ImageIcon("/root/Images/Slides/slide" + i + ".jpg");
             }
           
            Timer timer = new Timer(1000, new TimerListener());
            timer.start();
    
        }
        private class TimerListener implements ActionListener
        {
            public void actionPerformed(ActionEvent actionEvent)
            {
                label.setIcon(iconArray[iconIndex]);
                iconIndex++;
            }
        }
        public static void main(String[] args)
        {
             Exercise16_13 frame = new Exercise16_13();
            frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
            frame.setVisible(true);
            frame.setSize(800, 600);
            frame.setLocationRelativeTo(null);
        }
    }
    Last edited by aortell24; 06-03-2012 at 05:21 PM.

  8. #8
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,184
    Rep Power
    19

    Default Re: SlideShow help

    You forgot to post the error and stack trace.

    Also, you could assign the array elements in a loop, concatenating the loop index to assemble the path.
    Java Code:
    iconArray[0] = new ImageIcon("/root/Images/Slides/slide" + i + ".jpg");
    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  9. #9
    aortell24 is offline Member
    Join Date
    Jun 2012
    Location
    St.louis, Missouri
    Posts
    49
    Rep Power
    0

    Default Re: SlideShow help

    Sorry not quite sure what a stack trace is and its not showing any errors just not displaying any images. Just an empty frame.
    Thanks

  10. #10
    aortell24 is offline Member
    Join Date
    Jun 2012
    Location
    St.louis, Missouri
    Posts
    49
    Rep Power
    0

    Default Re: SlideShow help

    It is working now but giving me an Array Index Out Of Bounds exception pointing to line 49.

  11. #11
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,184
    Rep Power
    19

    Default Re: SlideShow help

    Quote Originally Posted by aortell24 View Post
    It is working now but giving me an Array Index Out Of Bounds exception pointing to line 49.
    Which isn't line 49 in the code you posted here.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  12. #12
    aortell24 is offline Member
    Join Date
    Jun 2012
    Location
    St.louis, Missouri
    Posts
    49
    Rep Power
    0

    Default Re: SlideShow help

    I fixed it It was suppose to display the 25 slides and then start over again.I added the if statement in the actionPerformed method.
    And it now works perfectly I just wanna thank all of you for the help. I couldn't have done it with out you!
    Java Code:
    import javax.swing.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    /**
     * Created with IntelliJ IDEA.
     * User: root
     * Date: 6/2/12
     * Time: 9:41 PM
     * To change this template use File | Settings | File Templates.
     */
    public class Exercise16_13 extends JFrame
    {
        ImageIcon[] iconArray = new ImageIcon[25];
        int iconIndex = 0;
        JLabel label;
        JPanel panel;
    
        Exercise16_13 ()
        {
            panel = new JPanel();
            label = new JLabel();
    
            add(panel);
             setTitle("Slide Show");
            panel.add(label);
    
    
            for(int i =0; i < iconArray.length; i++)
            {
                iconArray[i] = new ImageIcon("/root/Images/Slides/slide" + i + ".jpg");
            }
            Timer timer = new Timer(1000, new TimerListener());
            timer.start();
        }
        private class TimerListener implements ActionListener
        {
            public void actionPerformed(ActionEvent actionEvent)
            {
                label.setIcon(iconArray[iconIndex]);
                iconIndex++ ;
                if(iconIndex == 25)
                    iconIndex = 0;
    
            }
        }
        public static void main(String[] args)
        {
             Exercise16_13 frame = new Exercise16_13();
            frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
            frame.setVisible(true);
            frame.setSize(800, 600);
            frame.setLocationRelativeTo(null);
        }
    }
    Last edited by aortell24; 06-03-2012 at 11:19 PM. Reason: Fixed it

  13. #13
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,184
    Rep Power
    19

    Default Re: SlideShow help

    Quote Originally Posted by aortell24 View Post
    I fixed it It was suppose to display the 25 slides and then start over again.I added the if statement in the actionPerformed method.
    Quote Originally Posted by Fubarable View Post
    • After doing this, in actionPerformed, increment the iconIndex by 1, but check to make sure that it isn't equal to or larger than the length of the array.
    • If it is, then set it to 0.
    • A short-cut way of doing the last 2 steps is to use the mod (%) operator.
    The condition works, but using the modulus operator is a more elegant solution.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  14. #14
    aortell24 is offline Member
    Join Date
    Jun 2012
    Location
    St.louis, Missouri
    Posts
    49
    Rep Power
    0

    Default Re: SlideShow help

    Could you please show me I am not quite sure how to implement that ? Thanks alot.

Similar Threads

  1. Javascript slideshow - help!!!
    By irina793 in forum New To Java
    Replies: 2
    Last Post: 01-30-2012, 06:05 AM
  2. Need Help For Jave Slideshow
    By defanci in forum New To Java
    Replies: 0
    Last Post: 01-06-2011, 07:15 AM
  3. Java Slideshow in BlueJ...Help!
    By MarkL410 in forum New To Java
    Replies: 5
    Last Post: 06-24-2010, 04:16 AM
  4. Applet Slideshow in web browser
    By Urgle in forum New To Java
    Replies: 0
    Last Post: 12-01-2008, 04:30 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •