ImageIcon probably offers the shortest way. Most newbies don't have their images located next to the class (not source code, class -- the compiled program). Here is something from my library. Images are inside my class folder, inside a folder called resources.
import java.awt.Image;
import java.io.File;
import java.net.URL;
import java.util.Hashtable;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
/**
* Loads images into a Hashtable icons
* then, to get an icon use
* example, in ur pgrm... LoadIcons icons = new LoadIcons();
* icons.getIcon( ';name';);
*/
public class LoadIcons {
public Hashtable%26lt;String,ImageIcon%26gt; icons;
// String[] filenames;
public LoadIcons() {
icons = loadIcons();
}
private Hashtable%26lt;String,ImageIcon%26gt; loadIcons() {
String sep = System.getProperty(';file.separator';);
Image image = null;
icons = new Hashtable%26lt;String,ImageIcon%26gt;();
try {
URL url = this.getClass().getResource(';resources'; + sep + ';pencil.png';);
image = ImageIO.read(url);
ImageIcon icon = new ImageIcon(image);
icons.put(';pencil.png';,icon);
}
catch (Exception e) { System.out.println(';cannot find pencil';);
}
try{
URL url = this.getClass().getResource( ';resources'; + sep + ';www.png';);
image = ImageIO.read(url);
ImageIcon icon = new ImageIcon(image);
icons.put(';www.png';,icon);
}
catch (Exception e) {System.out.println( ';cannot find www.png';);
}
return icons;
}
}Swing method how is import picture pls send to coding sir in java?
And please ask your question in simple but clear words.
What do you mean by ';import picture'; with swing?
You want to display a picture in a swing component ( button, panel, etc )???
Or what?
Meanwhile, read the java tutorial available in sun websites also take a look at this java code examples website :
http://java.sun.com/docs/books/tutorial/鈥?/a>
http://www.exampledepot.com
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment