Share this page 

Have a simple Image browserTag(s): AWT


[imagelist.txt]
jht.gif|JAVA How-to
jsht.gif|Javascript How-to
pht.gif|Powerbuilder How-to
[application version]
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;

public class ImageBrowser {
  public static void main(String s[]) {
    AFrame f = new AFrame();
    }
 }
 
 class AFrame extends Frame implements ActionListener {
   List lbx;
   MyCanvas can;
   String url[] = new String[50];
   
   public AFrame() {
     setTitle("Image selection, double click to display");
     setLayout(new GridLayout(1,2));
     setSize(800,600);
     lbx = new List();
     can = new MyCanvas();
     add(lbx); add(can);
     initLbx();
     // action on listbox double click
     lbx.addActionListener(this);
     // to close the Frame
     addWindowListener
       (new WindowAdapter() {
         public void windowClosing(WindowEvent e) {
           System.exit(0);
           }
         }
       );
     setVisible(true);  
     }
     
   public void actionPerformed (ActionEvent ae) {
     String theUrl = url[lbx.getSelectedIndex()];
     MediaTracker media = new MediaTracker(this);
     Image image = 
        Toolkit.getDefaultToolkit().getImage(theUrl);
     media.addImage(image, 0);
     try {
       media.waitForID(0);  
       can.setImage(image);
       }
     catch (Exception e) { e.printStacktrace();}
     }
   
   public void initLbx() {
     int i = 0;
     try {
       String aLine = "";
       BufferedReader in
         = new BufferedReader(new FileReader("imagelist.txt"));
       while(null != (aLine = in.readLine())) {
          java.util.StringTokenizer st = 
             new java.util.StringTokenizer(aLine, "|");
         url[i++] = st.nextToken();
         // lbx.addItem(st.nextToken());         
         lbx.add(st.nextToken());
         }
       }
     catch(Exception e) { e.printStackTrace();}
     }
  }

class MyCanvas extends Canvas {
  private Image image;
  public MyCanvas() {
    super();
    }

  public void setImage(Image i) {
    image = i;
    repaint();
    }

  public void paint(Graphics g) {
    if (image != null)
       g.drawImage(image, 0,0, this);
    }
}
NOTE : You can download this example here

[applet version]

import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;

public class ImageBrowserApplet extends Applet {
  public void init() {  
    APanel p = new APanel(this);
    setLayout(new BorderLayout());
    add(p, "Center");
    }
 }
 
 class APanel extends Panel implements ActionListener {
   private List lbx;
   private MyCanvas can;
   private Applet parent = null;
   private String url[] = new String[50];
   
   public APanel(Applet a) {
     parent = a;
     setLayout(new GridLayout(1,2));
     lbx = new List();
     can = new MyCanvas();
     add(lbx); add(can);
     initLbx();
     // action on listbox double click
     lbx.addActionListener(this);
     setBackground(new Color(0).white);
     }
     
   public void actionPerformed (ActionEvent ae) {
     try {
       URL theUrl = 
         new URL(parent.getCodeBase(), url[lbx.getSelectedIndex()]);
       MediaTracker media = new MediaTracker(this);
       Image image = 
         Toolkit.getDefaultToolkit().getImage(theUrl);
       media.addImage(image, 0);
       media.waitForID(0);  
       can.setImage(image);
       }
     catch (Exception e) { e.printStackTrace();}
     }
   
   public void initLbx() {
     int i = 0;
     try {
       String aLine = "";
        URL source = 
          new URL(parent.getCodeBase(), "imagelist.txt");
       BufferedReader in
         = new BufferedReader
            (new InputStreamReader(source.openStream()));;
       while(null != (aLine = in.readLine())) {
          java.util.StringTokenizer st = 
             new java.util.StringTokenizer(aLine, "|");
         url[i++] = st.nextToken();
         // lbx.addItem(st.nextToken());         
         lbx.add(st.nextToken());
         }
       }
     catch(Exception e) { e.printStackTrace();}
     }
  }

class MyCanvas extends Canvas {
  private Image image;
  public MyCanvas() {
    super();
    }

  public void setImage(Image i) {
    image = i;
    repaint();
    }

  public void paint(Graphics g) {
    if (image != null)
       g.drawImage(image, 0,0, this);
    }
}
NOTE : You can try this example here