Java tutorial: Slideshow Applet


hey,

lets create a simple applet for slideshow of photos.

because in my redhat there is no image viewer that gives me slideshow option and i don’t want to search anywhere because i actually have it here.

first take a look at my whole code and i’m explaining each part below…


import java.awt.*;
import java.awt.image.*;
import java.io.*;
import java.applet.*;
public class slideshow extends Applet implements Runnable {
String imgpath;
Thread t;
Image img;
int j=0;
File folder=new File("/mnt/wallpaper/wallpapers");
File[] listoffile=folder.listFiles();

public void init()
{
this.setSize(1400,800);
setBackground(Color.red);
}</pre>
<!--more-->
<pre>
public void start()
{
t=new Thread(this);
t.start();
}

public void run()
{

for(int i=0;i<listoffile.length;i++)
{
if(listoffile[i].isFile())
{
String name=listoffile[i].getName();
int l=name.length();
System.out.println("File "+listoffile[i].getName());

//if(name.substring(l-2, l)=="jpg")
//{
img=getImage(getDocumentBase(),listoffile[i].getAbsolutePath());
repaint();
System.out.println("jpg file is "+name);
//}
try{
t.sleep(2000);

}catch(InterruptedException e)
{
System.out.println("error");
}
}
else
System.out.println("no "+listoffile[i].getName());
}

}
public void paint(Graphics g)
{

g.drawImage(img,0,0,this);
}
}

IMPORT FILES

import java.awt.*;
import java.awt.image.*;
import java.io.*;
import java.applet.*;

just import this files in your program.

START

in File folder=new File(“……IF YOU ARE USING WINDOWS THEN  USE ‘\\’……”);

In windows just go to Folder you want to show slideshow

copy address and paste it and after each \ give one more \   because character after \ has special meaning such as \n ,\b , \t.

public class slideshow extends Applet implements Runnable {
String imgpath;
Thread t;
Image img;
int j=0;
File folder=new File(“/mnt/wallpaper/wallpapers/verulphoto”);
File[] listoffile=folder.listFiles();

}

INTIALIZE APPLET

public void init()
{
this.setSize(1400,800);
setBackground(Color.red);
}

You can set size of applet by changing value in this.setSize(1400,800);

START THREAD

public void start()
{
t=new Thread(this);
t.start();
}

RUN

public void run()
{

for(int i=0;i<listoffile.length;i++)
{
if(listoffile[i].isFile())
{
String name=listoffile[i].getName();
int l=name.length();
System.out.println(“File “+listoffile[i].getName());

//if(name.substring(l-2, l)==”jpg”)
//{
   img=getImage(getDocumentBase(),listoffile[i].getAbsolutePath());
repaint();
System.out.println(“jpg file is “+name);
//}
try{
t.sleep(2000);

}catch(InterruptedException e)
{
System.out.println(“error”);
}
}
else
System.out.println(“no “+listoffile[i].getName());
}

img = getimage(getDocumentBase(),listoffile[i].getAbsolutePath())

will return image to img.

listoffile[i].getAbsolutePath()  will return address of file so Windows users You will get bug over here.

WINDOWS USER

While setting address to folder give ..\\

and instead of

   img=getImage(getDocumentBase(),listoffile[i].getAbsolutePath());

write

   img=getImage(getDocumentBase(),listoffile[i].getName);

But now You will have to copy image files to your project folder to play slideshow.

suppose my project name in slideshow

In slideshow folder there will be src,bin folder.

paste all photos there

or better way

String fol_ad=”I:\\wallpapers\\”;

take a string fol_ad then,

File folder=new File(fol_ad);

and before inputting image to img write

String a=fol_add+”\\”+listoffile[i].getName();

and

img=getImage(getDocumentBase(),a);

finally, PAINT

 public void paint(Graphics g)
{
g.drawImage(img,0,0,this);
}

Test this applet.

soon i will post good user interface image viewer application with lots of options.

//

//

//

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s