Results 1 to 3 of 3
Thread: Loading An Image Help Please!
- 11-20-2007, 02:07 PM #1
Member
- Join Date
- Nov 2007
- Posts
- 13
- Rep Power
- 0
Loading An Image Help Please!
I am fairly new to programming and am having trouble loading an image. i have looked at many online tutorials but they are all different. If someone could please tell me the actual code needed to read a file into my program and store it as a buffered image. Thankyou very much to anyone who can help.
- 11-20-2007, 03:42 PM #2
Supported image formats: You can get these with static methods in the ImageIO class. Generally it is reading gif, jpg, png and writing jpg and png up through j2se 1.4. Add support for reading and writing bmp and wbmp in j2se 1.5 and for writing gif in j2se 1.6
Java Code:import java.awt.*; import java.awt.image.BufferedImage; import java.io.*; import java.net.URL; import javax.imageio.ImageIO; import javax.swing.*; public class LoadImages { private JScrollPane getContent(BufferedImage image) { JPanel panel = new JPanel(new GridLayout(1,0)); panel.add(new JLabel(new ImageIcon(image))); panel.add(new JLabel(new ImageIcon(getImage()))); return new JScrollPane(panel); } private BufferedImage getImage() { String path = "images/bclynx.jpg"; // ClassLoader looks up resource which must // be located/available in the classpath. URL url = getClass().getResource(path); BufferedImage image = null; try { image = ImageIO.read(url); } catch(IOException e) { System.out.println("Read error: " + e.getMessage()); } return image; } public static void main(String[] args) throws IOException { String path = "images/mtngoat.jpg"; BufferedImage image = ImageIO.read(new File(path)); JFrame f = new JFrame(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.getContentPane().add(new LoadImages().getContent(image)); f.setSize(400,400); f.setLocation(200,200); f.setVisible(true); } }
- 01-09-2008, 08:14 AM #3
Member
- Join Date
- Jan 2008
- Posts
- 7
- Rep Power
- 0
Similar Threads
-
Dynamic image loading in jsp
By ramakrishna k m in forum JavaServer Pages (JSP) and JSTLReplies: 5Last Post: 10-10-2011, 06:08 AM -
Loading Image from local directory
By nancyhung in forum Advanced JavaReplies: 0Last Post: 01-29-2008, 03:46 PM -
Loading Images - Imp
By Thulasiraman in forum Advanced JavaReplies: 0Last Post: 01-28-2008, 09:33 AM -
Converting multiple banded image into single banded image... Image enhancement
By archanajathan in forum Advanced JavaReplies: 0Last Post: 01-08-2008, 05:29 PM -
problems when loading an image in servlet
By oregon in forum Java ServletReplies: 1Last Post: 08-05-2007, 06:02 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks