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.

Recommended Answers

All 3 Replies

//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";

--or--

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);
		setVisible(true);
	}

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

	@Override public void paint(Graphics g){
		super.paint(g);
		g.drawImage(img, 100, 100, this);
	}
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.