A String of Images


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: A String of Images

Hybrid View

  1. #1
    Join Date
    Oct 2005
    Posts
    15

    A String of Images

    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!!

  2. #2
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560
    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) {
        }
      }
    }
    Last edited by sjalle; 01-09-2006 at 12:06 PM.
    eschew obfuscation

Similar Threads

  1. Input string was not in a correct format
    By mdengler in forum ASP.NET
    Replies: 0
    Last Post: 11-26-2002, 02:32 PM
  2. Replies: 1
    Last Post: 06-05-2001, 06:12 AM
  3. Please help me -- urgent -- deadlock error
    By chandra in forum VB Classic
    Replies: 0
    Last Post: 06-22-2000, 07:36 AM
  4. Replies: 0
    Last Post: 06-22-2000, 07:30 AM
  5. Database problems
    By Robert Rieth in forum VB Classic
    Replies: 1
    Last Post: 04-11-2000, 03:21 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
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center