Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
Getting Image from a URL 
 

In the example given below, we are creating the no argument constructor, then calling the super class constructor and then setting the size of the window

 

Getting Image from a URL

                         

This example shows how to get an image from the given URL.

Description of program: 

In the example given below, we are creating the no argument constructor, then calling the super class constructor and then setting the size of the window. Now we are getting content pane of the frame class, then setting the layout manager as the BorderLayout. But It is not necessary to set the layout manager in case of BorderLayout as it is the default layout manager for the frame's content pane. Now we are retrieving the Image from the specified location then creating an object of the ImagePanel class that displays the image and exit button. Now we register the window listener that closes the application whenever window gets closed. We register the action listener for listening the events done by the user by using the button. Then paints the background and draws the image with its default size.

Here is the code of this program:

import java.net.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class GettingImageFromURL extends JFrame implements ActionListener {

    private JButton  exitButton = null;
    private URL url    = null;        

    public GettingImageFromURL() {        
        super("Getting an Image from a specified URL");
        this.setSize(170, 130);
    
        Container contentPane = this.getContentPane();
        contentPane.setLayout(new BorderLayout());
        
        try {
            url = new URL("http://www.roseindia.net/javacodeexamples/index.shtml/Gettin1.gif");
        }
        catch (MalformedURLException e) {
            e.printStackTrace();
        }
        
        Image img = Toolkit.getDefaultToolkit().getDefaultToolkit().createImage(url);
        
        exitButton = new JButton("Exit");
        exitButton.addActionListener(this);
        
        ImagePanel imagePanel = new ImagePanel(img);
        contentPane.add(imagePanel, BorderLayout.CENTER);
        contentPane.add(exitButton, BorderLayout.SOUTH);
        
        this.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }
    
    public void actionPerformed(ActionEvent actionevent) {

        String action = actionevent.getActionCommand();
                
        if (action.equals("Exit")) {
            dispose();
            System.out.println("Exiting.");
            System.exit(0);
        } 
        else {
            System.out.println(action);
        }
    }
  
    public static void main(String[] args) {
        GettingImageFromURL mainFrame = new GettingImageFromURL();
        mainFrame.setVisible(true);        
    }
}

class ImagePanel extends JPanel {

    Image img;
    public ImagePanel(Image img) {
        this.img = img;
    }

    public void paintComponent(Graphics g) {
    
        super.paintComponent(g);        
        g.drawImage(img, 0, 0, this); 
    }
}

Here is the output of the above program:

C:\Examples\URL>java GettingImageFromURL
 


Download of this program.

 

                         

» View all related tutorials
Related Tags: c com forms server orm form network socket io sed request return opera ai for work wait to base pos

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

1 comments so far (
post your own) View All Comments Latest 10 Comments:

This example doesn't work if I'm accessing internet thru proxy. Works fine for local (intranet) urls. What should I do to make it work with proxy?

Posted by av on Wednesday, 09.10.08 @ 17:53pm | #78518

Training Courses
Tell A Friend
Your Friend Name
Software Solutions
Least Viewed
Most Rated
Recently Viewed
Search Tutorials

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.