Results 1 to 2 of 2
Thread: Help with load image
- 08-01-2007, 05:58 AM #1
Member
- Join Date
- Jul 2007
- Posts
- 40
- Rep Power
- 0
- 08-01-2007, 07:16 AM #2
Yes. There are many ways to load images.
The Class getResource method looks for the "path" on your class path.
If you are not in static context, eg, inside the main method, you could write
Java Code:URL url = getClass().getResource(path);
Java Code:import java.awt.image.BufferedImage; import java.io.IOException; import java.net.URL; import javax.imageio.ImageIO; import javax.swing.*; public class ImageLoading { private JScrollPane getContent(BufferedImage image) { ImageIcon icon = new ImageIcon(image); JLabel label = new JLabel(icon); label.setHorizontalAlignment(JLabel.CENTER); return new JScrollPane(label); } public static void main(String[] args) throws IOException { String path = "images/cougar.jpg"; URL url = ImageLoading.class.getResource(path); BufferedImage image = ImageIO.read(url); JFrame f = new JFrame(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setContentPane(new ImageLoading().getContent(image)); f.setSize(400,400); f.setLocation(200,200); f.setVisible(true); } }
Similar Threads
-
Load URL that contains spaces?
By barkster in forum Java AppletsReplies: 0Last Post: 01-30-2008, 09:40 PM -
Converting multiple banded image into single banded image... Image enhancement
By archanajathan in forum Advanced JavaReplies: 0Last Post: 01-08-2008, 05:29 PM -
how to load a lot of files?
By gabriel in forum Java ServletReplies: 1Last Post: 08-07-2007, 06:04 PM -
how to load a html document
By boy22 in forum Java ServletReplies: 1Last Post: 08-05-2007, 04:12 AM -
Load song according to its Genre
By ahlai in forum AWT / SwingReplies: 1Last Post: 07-24-2007, 02:57 AM


LinkBack URL
About LinkBacks

Bookmarks