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
Re: Loading Gifs into an array
Hello Dele and welcome to the Java Programming Forums :D
I haven't tested this code but to load these into the array you could try:
Code :
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]);
2 Attachment(s)
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
Code :
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();
}
}
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:
Code :
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.