plz tell me the type of program(application etc.)
and the source, hopefully not difficult to understand
from C:/Magix
also can add //description

My preferred way: Have an external class that extends either Canvas or JPanel and use the paint method there.

Here is a quick & simple example of showing an image in java. The most import code is in the paint method. In this case, we are redefining the way a usual panel draws itself. The class ImagePanel does just this.

The other class just uses this panel in a frame and asks the user for an image to show. Hope this helps.

import javax.swing.*;
import java.awt.*;
import javax.imageio.*;
import java.io.*;

//panel used to draw image on
public class ImagePanel extends JPanel
{
	//path of image
	private String path;
	
	//image object
	private Image img;
	
	public ImagePanel(String path)	 throws IOException
	{
		//save path
		this.path = path;	
		
		//load image
		img = ImageIO.read(new File(path));
		
	}
	
	//override paint method of panel
	public void paint(Graphics g)
	{
		//draw the image
		if( img != null)
			g.drawImage(img,0,0, this);
	}
	
}

//example of using image panel
class ImageFrame
{
	public static void main(String[] args) throws IOException
	{
		
		try
		{
		
			//create frame
			JFrame f = new JFrame();
			
			//ask for image file
			JFileChooser chooser = new JFileChooser();
			chooser.showOpenDialog(f);
			
			//create panel with selected file
			ImagePanel panel = new ImagePanel( chooser.getSelectedFile().getPath() );
			
			//add panel to pane
			f.getContentPane().add(panel);
			
			
			//show frame
			f.setBounds(0,0,800,800);
			f.setVisible(true);
		}
		catch(Exception e)
		{
			System.out.println ( "Please verify that you selected a valid image file");	
		}		
	}
}

For more help, www.NeedProgrammingHelp.com

is there a way to get the image without asking for user input?

You don't have to ask the user for input. Just pass the name as a string. Here is how I do it:

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.net.MalformedURLException;
import java.net.URL;

public class ImageApplet extends Applet
{
  public static final int IDEAL_WIDTH = 320;
  public static final int IDEAL_HEIGHT = 240;

  private Image loadImage(String name)
  {
    Image result = null;
    MediaTracker tracker = new MediaTracker(this);

    Toolkit toolkit = Toolkit.getDefaultToolkit();

    result = toolkit.getImage(name);
    tracker.addImage(result, 0);
    try
    {
      tracker.waitForAll();
    } catch (InterruptedException e)
    {
      return null;
    }

    return result;
  }

  public void paint(Graphics g)
  {
    Image sea = loadImage("sea.jpg");
    g.drawImage(sea, 0, 0, null);
  }

  public static void main(String args[])
  {
    Applet applet = new ImageApplet();
    Frame frame = new Frame();
    frame.addWindowListener(new WindowAdapter()
    {
      public void windowClosing(WindowEvent e)
      {
        System.exit(0);
      }
    });

    frame.add(applet);
    frame.setSize(IDEAL_WIDTH, IDEAL_HEIGHT);
    frame.show();
  }
}

Here is the full article the above code is from:

http://www.heatonresearch.com/articles/20/page5.html

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.