Using Downloaded Images in Frame-based Applications

We thank Dharmesh Tailor for this impressive demonstration. We hope that by omitting a screenshot we will persuade you to try it yourself. We needed to remove the instruction imageApp.setResizable(False) in order to make the application work on our Raspberry Pi.

The code in image_download.pas

namespace image_download;
{
    Author: Dharmesh Tailor
    Application demonstrating opening an image from a given
    URL address
 }
interface

uses
  java.awt.*,  java.io.*, java.net.*, javax.imageio.*, javax.swing.*;

type
  ImageApp = public class(JFrame)
  public
    class method Main(args : array of String);
  end;

implementation

class method ImageApp.Main(args : array of String);
begin
  var image : Image := nil;
  try
    var url : URL := new URL('http://nssdc.gsfc.nasa.gov/image/planetary/earth/apollo17_earth.jpg');
    //ImageIO method to read image
    image := ImageIO.read(url);
    {Alternatively for reading a local file:
     var sourceImage : File := new File('c://file');
     image := ImageIO.read(sourceImage);
    }
  //In the event that the URL is invalid...
  except
    on e : IOException do
      begin
        e.printStackTrace;
      end;
  end;

  //Creates an instance (object) of the class, ImageApp
  var imageApp : ImageApp := new ImageApp;
  imageApp.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  //Initialises a label which contains the image
  var label : JLabel := new JLabel(new ImageIcon(image));
  //This adds the label (image) to the frame
  imageApp.add(label);
  imageApp.setVisible(True);
  //This scales the frame size to that of its components
  imageApp.pack;
  //Prevents the user resizing the window
  imageApp.setResizable(False);
  imageApp.setTitle('Image App');
end;

end.

Programming - a skill for life!

How to use widgets such as JButtons, JRadioButtons, JLists, JTables and JDesktopPanes in Oxygene for Java Applications