Hii All I am a biggner with java programming Now I assigned with a arogram
to "display multiple images and its name on particular location on a frame. (Hint Use Image class and ToolKit classes of java.awt.* package) "
plese help me to do this

Post your code and specific questions.

import java.awt.*;
import java.awt.event.*;

public class Viewer extends Frame {
	private Image image;

	public Viewer(String fileName) {
		Toolkit toolkit = Toolkit.getDefaultToolkit();
		image = toolkit.getImage(fileName);
		MediaTracker mediaTracker = new MediaTracker(this);
		mediaTracker.addImage(image, 0);
		try
		{
			mediaTracker.waitForID(0);
		}
		catch (InterruptedException ie)
		{
			System.err.println(ie);
			System.exit(1);
		}
		addWindowListener(new WindowAdapter() {
      		public void windowClosing(WindowEvent e) {
        		System.exit(0);
      		}
		});
		setSize(image.getWidth(null), image.getHeight(null));
		setTitle(fileName);
		show();
	}

	public void paint(Graphics graphics) {
		graphics.drawImage(image, 0, 0, null);
	}

	public static void main(String[] args) {
		new Viewer(args[0]);
	}
}

with the above program we can load single image on a frme .. I want to load multiple images on a singlefrme ...Can you help me to do this???

Just create array of images...

private Image[] image;
private String[] imgNames = {"img01.gif", "img02.gif", "img03.gif"};

image = new Image[imgNames.length];
for(int i = 0; i < imgNames.length; i++)
{
image[i] = toolkit.getImage(imgNames[i]);
}

Just create array of images...

private Image[] image;
private String[] imgNames = {"img01.gif", "img02.gif", "img03.gif"};

image = new Image[imgNames.length];
for(int i = 0; i < imgNames.length; i++)
{
image[i] = toolkit.getImage(imgNames[i]);
}

Hi Thank you for sending this help .. I did'nt yet solved my real problem My problem is to create a frame and load some pictures from the local disk. I don't Know How to create our own picture array( jpeg,gif, etc) Can you help me to solve this problem? It is very urgent.
Hope you help ..

Hi Thank you for sending this help .. I did'nt yet solved my real problem My problem is to create a frame and load some pictures from the local disk. I don't Know How to create our own picture array( jpeg,gif, etc) Can you help me to solve this problem? It is very urgent.
Hope you help ..

In your own code you're already creating one Image, just create several and show them next to each other.

I had changed my code like the following

import java.awt.*;
import java.lang.*;
import java.awt.event.*;
public class DispMulImage extends Frame{
  Image image;
  Panel p=new Panel();
    Panel p1=new Panel();
    Panel p2=new Panel();
    Button b1=new Button("Open");


  public static void main(String[] args) {
      new DispMulImage();
  }
  public DispMulImage(){
    setTitle("Displaying Multiple Images");
    setSize(300, 300);
    //p.setBackground(Color.BLUE);
    p.setVisible(true);
    setLayout(new GridLayout(3,3,10,15));
    p.setSize(50,50);
    p1.setBackground(Color.BLUE);
    p1.setVisible(true);
    setLayout(new GridLayout(3,3,10,15));
    p1.setSize(50,50);
    p2.setBackground(Color.BLUE);
    p2.setVisible(true);
    setLayout(new GridLayout(3,3,10,15));
    p2.setSize(50,50);

    addWindowListener(new WindowAdapter(){
      public void windowClosing(WindowEvent we){
        System.exit(0);
      }
    });
    setVisible(true);
  }
  public void paint(Graphics g){
    Toolkit tool = Toolkit.getDefaultToolkit();
   
    image = tool.getImage("E:\\mammoos\\beebalsam2.jpg");
  
    g.drawString("Image 1:",20,40);


    g.drawImage(image,20,45,p);

     add(p);
    add(p1);
    add(p2);
    add(b1);
  image = tool.getImage("E:\\sad.gif");
    g.drawString("Image 2:",20,80);
    g.drawImage(image,20,90,this);
    image = tool.getImage("E:\\wave.gif");
    g.drawString("Image 3:",20,130);
    g.drawImage(image,20,150,this);   
  }
}

But I can allign and load the images to the panel.. The Pint() run continuously and a flickering was happened. what is the solution to this .. please help me...........

Use [code]

[/code] tags around code that you post to retain formatting and preserve readability if you want other to take the time to read it.

That said, you are doing too much in your paint method. You should not be adding components to the frame or loading images in paint(). All of that should be done in some other method so that it is already available to be drawn in paint().

import java.awt.*;
import java.lang.*;
import java.awt.event.*;
public class DispMulImage extends Frame{
Image image;
Panel p=new Panel();
Panel p1=new Panel();
Panel p2=new Panel();
Button b1=new Button("Open");


public static void main(String[] args) {
new DispMulImage();
}
public DispMulImage(){
setTitle("Displaying Multiple Images");
setSize(300, 300);
//p.setBackground(Color.BLUE);
p.setVisible(true);
setLayout(new GridLayout(3,3,10,15));
p.setSize(50,50);
p1.setBackground(Color.BLUE);
p1.setVisible(true);
setLayout(new GridLayout(3,3,10,15));
p1.setSize(50,50);
p2.setBackground(Color.BLUE);
p2.setVisible(true);
setLayout(new GridLayout(3,3,10,15));
p2.setSize(50,50);

addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent we){
System.exit(0);
}
});
setVisible(true);
}
public void paint(Graphics g){
Toolkit tool = Toolkit.getDefaultToolkit();

image = tool.getImage("E:\\mammoos\\beebalsam2.jpg");

g.drawString("Image 1:",20,40);


g.drawImage(image,20,45,p);

add(p);
add(p1);
add(p2);
add(b1);
image = tool.getImage("E:\\sad.gif");
g.drawString("Image 2:",20,80);
g.drawImage(image,20,90,this);
image = tool.getImage("E:\\wave.gif");
g.drawString("Image 3:",20,130);
g.drawImage(image,20,150,this); 
}
}

Is there any other methods to load image on frame(or panels)??????

This article has been dead for over six months. Start a new discussion instead.