hi can someone please tell me how to insert a image into an image array.
The image is currently in the same folder as the class file.

8 Years
Discussion Span
Last Post by Alex Edwards
//assuming img is the reference-variable for an actual image

final short AMOUNT = <insert amount of images you want>;
Image images[] = new Image[AMOUNT]; // can now hold AMOUNT number of images

images[0] = img; // assigns the address of img to the first indice of array images

// images[0] points to the same object as img so changes can be done to the object from either reference

Hopefully this helps.


thanks for the quick reply i have tried typing the url in as shown below but is say that it is expecting a semi colon and i dont think it needs another one

theImages[0] = C:\Documents and Settings\Imran Sabir\My Documents\Computing Work\Picture Silder;


That probably wont work for a number of reasons.

First of all, you're specifying a URL as a combination of of values that compiler doesn't know how to parse. You will need to wrap those letters in a String--

String location = "C:\Documents and Settings\Imran Sabir\My Documents\Computing Work\Picture Silde";

--even so, this is still inappropriate because \ is considered a special character for expressions like "\n" (newline) or "\s" (space), so you will either need to provide a hint to the compiler by double-forward slashing or using the backslash in place of the forward slash.

EVEN THEN you are still not being 100% compliant to the platform you're invoking the command from, since the back and forward slash are file-system dependent. For simplicity, stick to the above suggestion and do this--

String location = "C:/Documents and Settings/Imran Sabir/My Documents/Computing Work/Picture Silde";


String location = "C:\\Documents and Settings\\Imran Sabir\\My Documents\\Computing Work\\Picture Silde";

-- and you should be ok, but at the moment we're storing the location in a string and not exactly extracting the much needed image @_@.

To achieve this, you'll need to consider looking into API's that allow you to easily retrieve an image from a File. The Image abstraction is a good place to start--

import javax.swing.JFrame;
import java.awt.Image;
import java.awt.Graphics;
import javax.imageio.ImageIO;
import java.io.File;

public class MyFrame2 extends JFrame{

	Image img = null; // placeholder for an Image object

	public MyFrame2(String location) throws Exception{
		img = ImageIO.read(new File(location)); // getting an Image from a File
		setSize(500, 500);

	public static void main(String... args){
			new MyFrame2("F:/123456789/8-cell-simple.gif");
		}catch(Exception e){

	@Override public void paint(Graphics g){
		g.drawImage(img, 100, 100, this);
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.