Help with SlideShow

Im making one

  • (2 Pages)
  • +
  • 1
  • 2

21 Replies - 2041 Views - Last Post: 17 November 2009 - 10:15 PM Rate Topic: -----

#1 theautokustomizer  Icon User is offline

  • D.I.C Regular

Reputation: 14
  • View blog
  • Posts: 250
  • Joined: 20-September 09

Help with SlideShow

Posted 17 November 2009 - 03:32 PM

Hello all, how is everyone doing? I am creating a slide show program/application that does the following:
-The slides are 800 X 600
-Write an application that automatically displays the slides repeatly
- Each slide is shown for a second
- The slides are shown in order
- When the last slide shows, the first slide shows again and repeats process

Here is what I have:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class SlideShow extends JFrame
{
	public SlideShow()
	{
		JLabel[] label = new JLabel[25];
		
		setLayout(new GridLayout(1, 1));
		
		Icon label0 = new ImageIcon("C:\\User\\Christopher\\Desktop\\CIS111\\evennumberedexercise\\evennumberedexercise\\slide0");
		Icon label1 = new ImageIcon("C:\\User\\Christopher\\Desktop\\CIS111\\evennumberedexercise\\evennumberedexercise\\slide1");
		Icon label2 = new ImageIcon("C:\\User\\Christopher\\Desktop\\CIS111\\evennumberedexercise\\evennumberedexercise\\slide2");
		Icon label3 = new ImageIcon("C:\\User\\Christopher\\Desktop\\CIS111\\evennumberedexercise\\evennumberedexercise\\slide3");
	   Icon label4 = new ImageIcon("C:\\User\\Christopher\\Desktop\\CIS111\\evennumberedexercise\\evennumberedexercise\\slide4");
		Icon label5 = new ImageIcon("C:\\User\\Christopher\\Desktop\\CIS111\\evennumberedexercise\\evennumberedexercise\\slide5");
		Icon label6 = new ImageIcon("C:\\User\\Christopher\\Desktop\\CIS111\\evennumberedexercise\\evennumberedexercise\\slide6");
		Icon label7 = new ImageIcon("C:\\User\\Christopher\\Desktop\\CIS111\\evennumberedexercise\\evennumberedexercise\\slide7");
		Icon label8 = new ImageIcon("C:\\User\\Christopher\\Desktop\\CIS111\\evennumberedexercise\\evennumberedexercise\\slide8");
		Icon label9 = new ImageIcon("C:\\User\\Christopher\\Desktop\\CIS111\\evennumberedexercise\\evennumberedexercise\\slide9");
		Icon label10 = new ImageIcon("C:\\User\\Christopher\\Desktop\\CIS111\\evennumberedexercise\\evennumberedexercise\\slide10");
		Icon label11 = new ImageIcon("C:\\User\\Christopher\\Desktop\\CIS111\\evennumberedexercise\\evennumberedexercise\\slide11");
		Icon label12 = new ImageIcon("C:\\User\\Christopher\\Desktop\\CIS111\\evennumberedexercise\\evennumberedexercise\\slide12");
		Icon label13 = new ImageIcon("C:\\User\\Christopher\\Desktop\\CIS111\\evennumberedexercise\\evennumberedexercise\\slide13");
		Icon label14 = new ImageIcon("C:\\User\\Christopher\\Desktop\\CIS111\\evennumberedexercise\\evennumberedexercise\\slide14");
		Icon label15 = new ImageIcon("C:\\User\\Christopher\\Desktop\\CIS111\\evennumberedexercise\\evennumberedexercise\\slide15");
		Icon label16 = new ImageIcon("C:\\User\\Christopher\\Desktop\\CIS111\\evennumberedexercise\\evennumberedexercise\\slide16");
		Icon label17 = new ImageIcon("C:\\User\\Christopher\\Desktop\\CIS111\\evennumberedexercise\\evennumberedexercise\\slide17");
		Icon label18 = new ImageIcon("C:\\User\\Christopher\\Desktop\\CIS111\\evennumberedexercise\\evennumberedexercise\\slide18");
		Icon label19 = new ImageIcon("C:\\User\\Christopher\\Desktop\\CIS111\\evennumberedexercise\\evennumberedexercise\\slide19");
		Icon label20 = new ImageIcon("C:\\User\\Christopher\\Desktop\\CIS111\\evennumberedexercise\\evennumberedexercise\\slide20");
		Icon label21 = new ImageIcon("C:\\User\\Christopher\\Desktop\\CIS111\\evennumberedexercise\\evennumberedexercise\\slide21");
		Icon label22 = new ImageIcon("C:\\User\\Christopher\\Desktop\\CIS111\\evennumberedexercise\\evennumberedexercise\\slide22");
		Icon label23 = new ImageIcon("C:\\User\\Christopher\\Desktop\\CIS111\\evennumberedexercise\\evennumberedexercise\\slide23");
		Icon label24 = new ImageIcon("C:\\User\\Christopher\\Desktop\\CIS111\\evennumberedexercise\\evennumberedexercise\\slide24");
		
		for (int pics = 0; pics < 999999; pics++)
		{
			label[pics] = new JLabel();

			add(new JLabel(new ImageIcon("Image" + label[pics] + ".jpg")));
			
			Timer timer = new Timer(1000, new TimerListener());			
		}
	}
	
	public static void main(String[] args)
	{
		SlideShow frame = new SlideShow();
		
		frame.setTitle("Display 25 images");
		frame.setSize(850, 650);
		//frame.pack();
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setLocationRelativeTo(null);
		frame.setVisible(true);
	}
	
	class TimerListener implements ActionListener
	{
		public void actionPerformed(ActionEvent e)
		{
			repaint();
		}
	}		
}


It compiles fines, but when I go to run it, I get an error message..

----jGRASP exec: java SlideShow

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 25
at SlideShow.<init>(SlideShow.java:47)
at SlideShow.main(SlideShow.java:57)

----jGRASP wedge2: exit code for process is 1.
----jGRASP: operation complete.


I don't know why it isn't working... I have gone over it, and I think it is right, but obviously not...any ideas?

This post has been edited by theautokustomizer: 17 November 2009 - 03:33 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Help with SlideShow

#2 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2696
  • View blog
  • Posts: 10,556
  • Joined: 15-July 08

Re: Help with SlideShow

Posted 17 November 2009 - 03:43 PM

Here, you declare you array with a size of 25:
JLabel[] label = new JLabel[25];



So you cant add 99999999... images to it.
for (int pics = 0; pics < 999999; pics++)
		{
			label[pics] = new JLabel();



I would rotate instead of increment in this case.

This means that you increment if i is between 0 and 24 (inclusive) but before i = 25, set i back to 0 somehow. Good luck!
Was This Post Helpful? 1
  • +
  • -

#3 theautokustomizer  Icon User is offline

  • D.I.C Regular

Reputation: 14
  • View blog
  • Posts: 250
  • Joined: 20-September 09

Re: Help with SlideShow

Posted 17 November 2009 - 03:43 PM

I am not trying to add 999999 to it, that is how many times it should run... At least that is what I thought...

This post has been edited by theautokustomizer: 17 November 2009 - 03:44 PM

Was This Post Helpful? 0
  • +
  • -

#4 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2696
  • View blog
  • Posts: 10,556
  • Joined: 15-July 08

Re: Help with SlideShow

Posted 17 November 2009 - 03:46 PM

With this code:

for (int pics = 0; pics < 999999; pics++)
		{
			label[pics] = new JLabel();



if at any time i >= 25, you access the array at that value, and you cannot because the array is not long enough. It's like trying to pick another apple after they have all already been picked.
Was This Post Helpful? 1
  • +
  • -

#5 prajayshetty  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 31
  • View blog
  • Posts: 920
  • Joined: 27-April 07

Re: Help with SlideShow

Posted 17 November 2009 - 03:46 PM

he is right youractual size array is 25 but you are trying to insert it 999999 times which cant be
Was This Post Helpful? 1
  • +
  • -

#6 theautokustomizer  Icon User is offline

  • D.I.C Regular

Reputation: 14
  • View blog
  • Posts: 250
  • Joined: 20-September 09

Re: Help with SlideShow

Posted 17 November 2009 - 03:52 PM

Okie dokey then, TY both of you...
So I edited it, and I now have my blank window coming up... I think I am missing a line of code to get the pictures/slides to show??? Am I? Or....

This post has been edited by theautokustomizer: 17 November 2009 - 03:54 PM

Was This Post Helpful? 0
  • +
  • -

#7 sakshamkum  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 19
  • View blog
  • Posts: 232
  • Joined: 09-June 09

Re: Help with SlideShow

Posted 17 November 2009 - 04:10 PM

for this you can try a different technique that is memory saving and fast
you need class File
// create a File object of the directory where you have your images
//and
File arr[]=file.listDirectories();
//and run a for
for(int i=0;i<25;i++)
{
Icon img=new ImageIcon(arr[i].getAbsolutePath());
//display
//and delay
if(i=24)
{
i=0;
}
}


Was This Post Helpful? 0
  • +
  • -

#8 theautokustomizer  Icon User is offline

  • D.I.C Regular

Reputation: 14
  • View blog
  • Posts: 250
  • Joined: 20-September 09

Re: Help with SlideShow

Posted 17 November 2009 - 04:20 PM

View Postsakshamkum, on 17 Nov, 2009 - 03:10 PM, said:

for this you can try a different technique that is memory saving and fast
you need class File
// create a File object of the directory where you have your images
//and
File arr[]=file.listDirectories();
//and run a for
for(int i=0;i<25;i++)
{
Icon img=new ImageIcon(arr[i].getAbsolutePath());
//display
//and delay
if(i=24)
{
i=0;
}
}



Im not sure on how to create a File object of the directory where they are... We haven't done alot on importing files and such... That is why I declared them in here...

This post has been edited by theautokustomizer: 17 November 2009 - 04:22 PM

Was This Post Helpful? 0
  • +
  • -

#9 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2696
  • View blog
  • Posts: 10,556
  • Joined: 15-July 08

Re: Help with SlideShow

Posted 17 November 2009 - 05:27 PM

EDIT: Anybody reading this, make note that this code does NOT do as auto wishes and so it doesn't work

This is an example with how you could use another overridden JPanel to display an image with. All this class does is imitate a JPanel and paints the image onto itself when called by the Timer in the next code segment.

import java.awt.Graphics;
import java.awt.Image;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JPanel;

public class Main extends JPanel
{

	private ImageIcon curIcon;

	void setImage(ImageIcon ic) {
		 curIcon = ic;
		 repaint();
	}

   // draw the image
   public void paintComponent( Graphics g )
   {

	  super.paintComponent( g ); // call superclass's paintComponent
	  g.drawImage(curIcon.getImage(),0,0,null);

   } // end method paintComponent
}



You just add it as a Panel to your Slideshow class and turn the array of JLabels to an array of Icons.
However, this new Main class should help you learn how to implement this. In this code, you, every time the event fires, you increase the picture by one. The only thing you must do is to add all of the pictures to the array.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class ScreenSaver extends JFrame implements ActionListener
{
	public ScreenSaver()
	{
		ScreenSaverPanel frame = new ScreenSaverPanel();
		
		ImageIcon[] label = new ImageIcon[25];
		int currentImage = 0;
	   
		setLayout(new GridLayout(1, 1));
	  
		for (int i = 0; i < 25; i++) {
			label[2] = new ImageIcon("Location to file");
		   
			Timer timer = new Timer(1000, this);   
		}
		
		
	}
	
	public void actionPerformed(ActionEvent arg0) {
		if (currentImage ==25)
			currentImage = 0;
		frame.setImage(label[currentImage]);
				currentImage++;
		frame.repaint();
	}
   
	public static void main(String[] args)
	{
		ScreenSaver frame = new ScreenSaver();
	   
		frame.setTitle("Display 25 images");
		frame.setSize(850, 650);
		//frame.pack();
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setLocationRelativeTo(null);
		frame.setVisible(true);
	}

		   
}



You may have to change this program a little bit because I could not see the actual result of running the program because I don't have the images you need. But, I hope you learned something.

This post has been edited by Dogstopper: 17 November 2009 - 07:14 PM

Was This Post Helpful? 1
  • +
  • -

#10 theautokustomizer  Icon User is offline

  • D.I.C Regular

Reputation: 14
  • View blog
  • Posts: 250
  • Joined: 20-September 09

Re: Help with SlideShow

Posted 17 November 2009 - 05:52 PM

Ok, I'm confused... I mean I did learn soemthing, but you have confused with both sets of code you put up... I also have never used the package statement...
Was This Post Helpful? 0
  • +
  • -

#11 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2696
  • View blog
  • Posts: 10,556
  • Joined: 15-July 08

Re: Help with SlideShow

Posted 17 November 2009 - 05:54 PM

Oh ignore the package statement...my compiler did that...although you may wish to learn about packages too: http://java.sun.com/...e/packages.html sorry!
Was This Post Helpful? 1
  • +
  • -

#12 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2696
  • View blog
  • Posts: 10,556
  • Joined: 15-July 08

Re: Help with SlideShow

Posted 17 November 2009 - 06:00 PM

Ok the first code that I put up overrides a JPanel, so it works just like a normal JPanel. The only thing that I did, is that everytime repaint is called on it, it does that customized paint method. The other method just sets the current image to display.

The second code snippet that I gave you is merely a "fixed" version of your code. You have to load the ImageIcons into the array for storage. Then you set up the Timer. The variable "this" means that the current class implements ActionListener, and when the timer expires, it will call the actionPerformed method. Then we tell the code in part 1 the image to use and then call it's repaint method. That does it's special thing with the graphics class... and over and over again.
Was This Post Helpful? 1
  • +
  • -

#13 theautokustomizer  Icon User is offline

  • D.I.C Regular

Reputation: 14
  • View blog
  • Posts: 250
  • Joined: 20-September 09

Re: Help with SlideShow

Posted 17 November 2009 - 06:23 PM

Alright, this is where I am at now...
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class SlideShow extends JFrame 
{
	public SlideShow()
	{
		ImageIcon[] label = new ImageIcon[25];
		
		setLayout(new GridLayout(1, 1));
		
		Icon label0 = new ImageIcon("C:\\User\\Christopher\\Desktop\\CIS111\\evennumberedexercise\\evennumberedexercise\\slide0");
		Icon label1 = new ImageIcon("C:\\User\\Christopher\\Desktop\\CIS111\\evennumberedexercise\\evennumberedexercise\\slide1");
		Icon label2 = new ImageIcon("C:\\User\\Christopher\\Desktop\\CIS111\\evennumberedexercise\\evennumberedexercise\\slide2");
		Icon label3 = new ImageIcon("C:\\User\\Christopher\\Desktop\\CIS111\\evennumberedexercise\\evennumberedexercise\\slide3");
	   Icon label4 = new ImageIcon("C:\\User\\Christopher\\Desktop\\CIS111\\evennumberedexercise\\evennumberedexercise\\slide4");
		Icon label5 = new ImageIcon("C:\\User\\Christopher\\Desktop\\CIS111\\evennumberedexercise\\evennumberedexercise\\slide5");
		Icon label6 = new ImageIcon("C:\\User\\Christopher\\Desktop\\CIS111\\evennumberedexercise\\evennumberedexercise\\slide6");
		Icon label7 = new ImageIcon("C:\\User\\Christopher\\Desktop\\CIS111\\evennumberedexercise\\evennumberedexercise\\slide7");
		Icon label8 = new ImageIcon("C:\\User\\Christopher\\Desktop\\CIS111\\evennumberedexercise\\evennumberedexercise\\slide8");
		Icon label9 = new ImageIcon("C:\\User\\Christopher\\Desktop\\CIS111\\evennumberedexercise\\evennumberedexercise\\slide9");
		Icon label10 = new ImageIcon("C:\\User\\Christopher\\Desktop\\CIS111\\evennumberedexercise\\evennumberedexercise\\slide10");
		Icon label11 = new ImageIcon("C:\\User\\Christopher\\Desktop\\CIS111\\evennumberedexercise\\evennumberedexercise\\slide11");
		Icon label12 = new ImageIcon("C:\\User\\Christopher\\Desktop\\CIS111\\evennumberedexercise\\evennumberedexercise\\slide12");
		Icon label13 = new ImageIcon("C:\\User\\Christopher\\Desktop\\CIS111\\evennumberedexercise\\evennumberedexercise\\slide13");
		Icon label14 = new ImageIcon("C:\\User\\Christopher\\Desktop\\CIS111\\evennumberedexercise\\evennumberedexercise\\slide14");
		Icon label15 = new ImageIcon("C:\\User\\Christopher\\Desktop\\CIS111\\evennumberedexercise\\evennumberedexercise\\slide15");
		Icon label16 = new ImageIcon("C:\\User\\Christopher\\Desktop\\CIS111\\evennumberedexercise\\evennumberedexercise\\slide16");
		Icon label17 = new ImageIcon("C:\\User\\Christopher\\Desktop\\CIS111\\evennumberedexercise\\evennumberedexercise\\slide17");
		Icon label18 = new ImageIcon("C:\\User\\Christopher\\Desktop\\CIS111\\evennumberedexercise\\evennumberedexercise\\slide18");
		Icon label19 = new ImageIcon("C:\\User\\Christopher\\Desktop\\CIS111\\evennumberedexercise\\evennumberedexercise\\slide19");
		Icon label20 = new ImageIcon("C:\\User\\Christopher\\Desktop\\CIS111\\evennumberedexercise\\evennumberedexercise\\slide20");
		Icon label21 = new ImageIcon("C:\\User\\Christopher\\Desktop\\CIS111\\evennumberedexercise\\evennumberedexercise\\slide21");
		Icon label22 = new ImageIcon("C:\\User\\Christopher\\Desktop\\CIS111\\evennumberedexercise\\evennumberedexercise\\slide22");
		Icon label23 = new ImageIcon("C:\\User\\Christopher\\Desktop\\CIS111\\evennumberedexercise\\evennumberedexercise\\slide23");
		Icon label24 = new ImageIcon("C:\\User\\Christopher\\Desktop\\CIS111\\evennumberedexercise\\evennumberedexercise\\slide24");
		
		for (int pics = 0; pics < 25; pics++)
		{
			label[pics] = new ImageIcon();

			add(new JLabel(new ImageIcon("Image" + label[pics] + ".jpg")));
			
			Timer timer = new Timer(1000, this);
			
			if (pics == 24)
			{
				pics = 0;
			}				
		}
	}
	
	public static void main(String[] args)
	{
		SlideShow frame = new SlideShow();
		
		frame.setTitle("Display 25 images");
		frame.setSize(850, 650);
		//frame.pack();
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setLocationRelativeTo(null);
		frame.setVisible(true);
	}
	
	public void paintComponent( Graphics g )
	{

		super.paintComponent(g); 
		g.drawImage(curIcon.getImage(), 0, 0, null);

	}
	
	class TimerListener implements ActionListener
	{
		public void actionPerformed(ActionEvent e)
		{
			repaint();
		}
	}
}


And these are my errors...


----jGRASP exec: javac -g C:\Users\Christopher\Documents\SlideShow.java

SlideShow.java:51: cannot find symbol
symbol : constructor Timer(int,SlideShow)
location: class javax.swing.Timer
Timer timer = new Timer(1000, this);
^
SlideShow.java:75: cannot find symbol
symbol : method paintComponent(java.awt.Graphics)
location: class javax.swing.JFrame
super.paintComponent(g);
^
SlideShow.java:76: cannot find symbol
symbol : variable curIcon
location: class SlideShow
g.drawImage(curIcon.getImage(), 0, 0, null);
^
3 errors

----jGRASP wedge2: exit code for process is 1.
----jGRASP: operation complete.
Was This Post Helpful? 0
  • +
  • -

#14 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2696
  • View blog
  • Posts: 10,556
  • Joined: 15-July 08

Re: Help with SlideShow

Posted 17 November 2009 - 06:32 PM

#1:
label[pics] = new ImageIcon();



This is where you should load your image like this:
for (int pics = 0; pics < 25; pics++)
		{
			 String picName = "C:\\User\\Christopher\\Desktop\\CIS111\\evennumberedexercise\\evennumberedexercise\\slide" + pics;
			 label[pics] = new ImageIcon(picName);



That will allow you to load all those images.

This post has been edited by Dogstopper: 17 November 2009 - 07:15 PM

Was This Post Helpful? 1
  • +
  • -

#15 theautokustomizer  Icon User is offline

  • D.I.C Regular

Reputation: 14
  • View blog
  • Posts: 250
  • Joined: 20-September 09

Re: Help with SlideShow

Posted 17 November 2009 - 06:38 PM

I would like to use JLabels because I think if I could learn how to apply them, I would have more functionability in the future...TY
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2