omegaFlame
omegaFlame

Reputation: 255

Java Server Faces Loading images from array

I have a class ImageBean and I want to use it to load images from an array. I have preloaded the array with images (9 in total) and want to display one image consecutively every time I refresh the browser. The images are named '1.jpg, 2.jpg' etc (i.e image '1' -hit refresh, image '2' hit refresh, ... image '9'). Any help would be greatly appreciated. Thanks.

import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import java.io.Serializable;
import java.util.ArrayList;
import javax.imageio.ImageIO;

@ManagedBean(name="ImageBean")
@SessionScoped
public class ImageBean implements Serializable 
{
    private static final long serialVersionUID = 1L;
    ArrayList<Image> myArr = new ArrayList<Image>();
    BufferedImage img = null;

    public ImageBean()
    {
        for(int i = 1; i < 10; i++)
        {
            try 
            {
                img = ImageIO.read(new File(i + ".jpg"));
            } 
            catch (IOException ex) {}

            myArr.add(img);
        }
    }

    public BufferedImage getImage(int i) 
    {
        return (BufferedImage) myArr.get(i);
    }
}

Edit 1

I have edited the code slightly so I use an array of BufferedImage instead of an ArrayList. Now I don't get an error, but the image is not displayed in the browser.

public class ImageBean implements Serializable 
{
    private static final long serialVersionUID = 1L;
    BufferedImage[] myArr = new BufferedImage[9];

    public ImageBean()
    {
        for(int i = 0; i < 9; i++)
        {
            try 
            {
                myArr[i] = (ImageIO.read(new File(i+1 + ".jpg")));
            } 
            catch (IOException ex) {}
        }
    }

    public BufferedImage getImage(int i) 
    {
        return (BufferedImage) myArr[i];
    }
}

Upvotes: 0

Views: 1349

Answers (2)

FMQ
FMQ

Reputation: 418

A simple solution would be to add a variable to hold the current image and just get the next image... (just keep in mind to reset for the last image)

@SessionScoped
public class ImageBean implements Serializable 
{
    private static final long serialVersionUID = 1L;
    ArrayList<Image> myArr = new ArrayList<Image>();
    BufferedImage img = null;

    private int currImgId = 1;

    public ImageBean()
    {
        for(int i = 1; i < 10; i++)
        {
            try 
            {
                img = ImageIO.read(new File(i + ".jpg"));
            } 
            catch (IOException ex) {}

            myArr.add(img);
        }
    }

  public BufferedImage getImage() 
  {
    // Reset logic would go here...probably best with array.size...
    return (BufferedImage) myArr.get(currImgId++);
  }

}

Upvotes: 1

user1181445
user1181445

Reputation:

public ImageBean()
    {
        for(int i = 1; i < 10; i++)
        {
            try 
            {
                myArr.add(ImageIO.read(new File(i + ".jpg")));
            } 
            catch (IOException ex) {}
        }
    }

originally if it failed it would keep adding the previous image. This may have made it 'freeze' as it would really be going through the images, but actually just be displaying the same.

Upvotes: 0

Related Questions