I have 15 images that I want to display one after the other with a time gap in between, does anybody know of the code I can use to do this??
Thanks for your help!!
Printable View
I have 15 images that I want to display one after the other with a time gap in between, does anybody know of the code I can use to do this??
Thanks for your help!!
This applet loads a text file from the server containing a list of image file names. It then displays one after the other while "folding" the images during each transition. Currently it doesn't use all the parameters it loads.
It uses double buffering (to avoid flickering).
Code:import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.*;
import java.net.*;
import java.util.*;
import java.awt.image.*;
/**
*
*/
class BoardPanel extends JPanel implements Runnable {
static Color bgColor=new Color(Integer.parseInt("B0C4DE",16));
BufferedImage memImg=null;
Graphics memG=null;
private Board applet=null;
private int hSpace=0;
private int freq=5000;
private int currWidth=0;
private int currHeight=0;
private int maxImgs=1;
private String listName=null;
private boolean running=false;
private Thread spinner=null;
private int imgIx=-1;
private int xPos = 0;
private ArrayList imgList=new ArrayList();
private Image currImg=null;
private String msg="no data";
private boolean imagesLoaded=false;
public void paintComponent(Graphics g) {
if (memG==null) {
g.drawString("Loading...",20,20);
return;
}
Color c=memG.getColor();
memG.setColor(bgColor);
memG.fillRect(0,0,getWidth(),getHeight());
memG.setColor(Color.black);
if (imagesLoaded) {
drawImage(memG);
} else {
if (msg != null)
memG.drawString(msg, 20, 20);
}
g.drawImage(memImg,0,0,applet);
memG.setColor(c);
}
public void stopAnimation () {
this.running=false;
}
public void startAnimation () {
imgIx=0;
spinner=new Thread(this,"spinner");
this.running=true;
stepImage();
spinner.start();
}
public void drawImage(Graphics g) {
//for (int i=0; i<imgList.size(); i++) {
//if (i==imgIx) {
//Image img = (Image) imgList.get(i);
g.drawImage(currImg, xPos, 0, currWidth, currHeight, applet);
//}
//}
}
public void loadImages(Board applet, String listName) {
this.applet = applet;
this.listName = listName;
this.hSpace=Integer.parseInt(applet.getParameter("sh_dist"));
this.freq=Integer.parseInt(applet.getParameter("sh_freq"));
this.maxImgs=Integer.parseInt(applet.getParameter("sh_maximgs"));
Thread t=new Thread(this,"loader");
this.running=true;
t.start();
}
public void run() {
if (Thread.currentThread().getName().equals("loader")) {
try {
msg="Loading Images....";
repaint();
loadImageSequence();
imagesLoaded=true;
startAnimation();
System.out.println("startAnimation");
}
catch (Exception ex) {
repaint();
return;
}
} else if (Thread.currentThread()==spinner) {
try {
while (running) {
spinner.sleep(freq);
System.out.println("spinImage");
spinImage();
}
}
catch (Exception ex) {
repaint();
return;
}
}
}
private void stepImage() {
if (imgList.size()==0) {
currImg=null;
return;
}
imgIx++;
if (imgIx == imgList.size())
imgIx = 0;
currImg=(Image)imgList.get(imgIx);
}
private void spinImage() throws Exception {
xPos=0;
currWidth=currImg.getWidth(applet);
currHeight=currImg.getHeight(applet);
System.out.println("currWidth:"+currWidth);
while (currWidth > 0 && this.running) {
repaint();
currWidth -= 2;
spinner.sleep(15);
}
stepImage();
int w=currImg.getWidth(applet);
System.out.println("w:"+w);
currHeight=currImg.getHeight(applet);
while (currWidth < w) {
repaint();
currWidth += 2;
Thread.currentThread().sleep(15);
}
}
private void loadImageSequence() throws Exception {
String hostName=applet.getDocumentBase().getHost();
String p=applet.getDocumentBase().getPath();
int ix=p.lastIndexOf("/");
String pth="http://"+hostName+"/"+p.substring(0,ix)+"/system/"+listName;
URL url = new URL(pth);
System.out.println("url: "+url.toString());
SiteConn sc = new SiteConn(url);
String s = sc.getContents().toString();
System.out.println("content: "+s);
String [] ss=s.split("\n");
MediaTracker mt=new MediaTracker(applet);
int n=0;
imgList.clear();
for (int i=0; i<ss.length && this.running; i++) {
url=new URL("http://"+hostName+"/"+p.substring(0,ix)+"/images/"+ss[i]);
Image img=applet.getImage(url);
mt.addImage(img,n++);
imgList.add(img);
}
memImg=new BufferedImage(applet.getWidth(), applet.getHeight(),BufferedImage.TYPE_INT_RGB);
mt.addImage(memImg,n++);
try {
mt.waitForAll();
} catch (Exception ex) {
msg="Image load fail: "+ex.getMessage();
}
memG=memImg.getGraphics();
repaint();
}
}
/**
*
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2005</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/
public class Board extends JApplet {
private boolean isStandalone = false;
String boardList;
BoardPanel boardPan = new BoardPanel();
//Construct the applet
public Board() {
}
//Initialize the applet
public void init() {
boardList = this.getParameter("boardList");
if (boardList == null || boardList.length() == 0)
return;
try {
jbInit();
boardPan.repaint();
boardPan.loadImages(this,boardList);
}
catch(Exception e) {
e.printStackTrace();
}
}
public void stop () {
boardPan.stopAnimation();
System.out.println("stopAnimation");
}
public void start () {
}
//Component initialization
private void jbInit() throws Exception {
this.setSize(new Dimension(800,100));
this.getContentPane().add(boardPan, BorderLayout.CENTER);
}
//static initializer for setting look & feel
static {
try {
//UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
//UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
}
catch(Exception e) {
}
}
}