Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Best approach to load .gif pictures into an Array then selecting few of them and attach to a lable

  1. #1
    Junior Member
    Join Date
    Apr 2009
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Best approach to load .gif pictures into an Array then selecting few of them and attach to a lable

    I wish to load several Gif pictures into an array and then later select some of these gifs and attach to a label. What is the best approach?


    ImageIcon orange = new ImageIcon ("orange.gif");
    ImageIcon apple = new ImageIcon ("apple.gif");
    ImageIcon banana = new ImageIcon ("banana.gif");

    Image[] images = new Image[3];

    How do a load into array and then from array to Label

    thanks

    Craig Delehanty


  2. #2
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: Loading Gifs into an array

    Hello Dele and welcome to the Java Programming Forums

    I haven't tested this code but to load these into the array you could try:

            JLabel label;
     
            ImageIcon orange = new ImageIcon ("orange.gif");
            ImageIcon apple = new ImageIcon ("apple.gif");
            ImageIcon banana = new ImageIcon ("banana.gif");
     
            ImageIcon[] images = new ImageIcon[3];
     
            images[0] = orange;
            images[1] = apple;
            images[2] = banana;
     
            label = new JLabel(images[2]);
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  3. #3
    Junior Member
    Join Date
    Apr 2009
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Loading Gifs into an array

    Thanks for your reply.

    This is the code I am actually working with. The compiler complains about the array structure. I don't know why as it is the same as in your example.

    Dele

    import javax.swing.*;
    import java.awt.event.*;
    import java.awt.*;
     
     
    public class PictureDemo extends JFrame
    {
     
    	// Create an ImageIcon Object
    	ImageIcon pic1 = new ImageIcon("background2.gif");
     
    	// Create a JLabel and display image
    	JLabel label1 = new JLabel(pic1);
    	JLabel label2 = new JLabel(pic1);
    	JLabel label3 = new JLabel(pic1);
     
    	JPanel pnl = new JPanel();
     
    	ImageIcon pic4 = new ImageIcon("7.gif");	
    	ImageIcon[] images = new ImageIcon[3];
     
    	images[0] = pic4;
     
    	// Constructor
    	public PictureDemo()
    	{
    		super("Picture Display");
    		setSize(400,200);
    		setDefaultCloseOperation(EXIT_ON_CLOSE);
    		pnl.add(label1);
    		pnl.add(label2);
    		pnl.add(label3);
    		add(pnl);
    		setVisible(true);
     
    	}
    	public static void main(String[] args) 
    	{
    		PictureDemo picdemo = new PictureDemo();
    	}
     
    }
    Attached Images Attached Images

  4. #4
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: Loading Gifs into an array

    The Array setup is correct but the problem is you cannot declare the Array values there.

    You could try this:

    import javax.swing.*;
    import java.awt.event.*;
    import java.awt.*;
     
     
    public class PictureDemo extends JFrame
    {
     
        // Create an ImageIcon Object
        ImageIcon pic1 = new ImageIcon("background2.gif");
     
        // Create a JLabel and display image
        JLabel label1 = new JLabel(pic1);
        JLabel label2 = new JLabel(pic1);
        JLabel label3 = new JLabel(pic1);
     
        JPanel pnl = new JPanel();
     
        public static ImageIcon pic4 = new ImageIcon("7.gif");
        public static ImageIcon[] images = new ImageIcon[3];
     
        // Constructor
        public PictureDemo()
        {
            super("Picture Display");
            setSize(400,200);
            setDefaultCloseOperation(EXIT_ON_CLOSE);
            pnl.add(label1);
            pnl.add(label2);
            pnl.add(label3);
            add(pnl);
            setVisible(true);
     
        }
        public static void main(String[] args) 
        {
            images[0] = pic4;
            PictureDemo picdemo = new PictureDemo();
        }
     
    }

    This compiles fine but obviousally pic4 isnt displayed yet in your code.
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.