/*
 * %W% %E%
 *
 * Copyright (c) 2000-2004 Sun Microsystems, Inc. All rights reserved.
 * PROPRIETARY/CONFIDENTIAL
 * Use is subject to license terms
 */
package example.SoccerQ;

import javax.microedition.lcdui.*;
import javax.microedition.midlet.MIDlet;
/**
 *
 * @version 2.0
 */
//public class CustomItemDemo extends MIDlet implements Runnable, CommandListener {

public class CustomItemDemo extends MIDlet
    implements CommandListener {


    private final static Command CMD_EXIT =
	    new Command("Back", Command.EXIT, 1);
    private final static Command CMD_PLAY =
	    new Command("Play!", Command.ITEM, 1);

    private Display display;

    private boolean firstTime;
    private Form mainForm;

    public CustomItemDemo() {
        initialize();
    }

    protected void startApp() {
        if (firstTime) {
            display = Display.getDisplay(this);

 //         mainForm.append(new TextField("Upper Item", null, 10, 0));
         //   mainForm.append(new Table("Soccer Video Shots - Key Frames", Display.getDisplay(this)));


            String [] imagename = new String [6];
//            imagename[0] = "/images/1_fifa03_Ger_Rus_02_7_51662.jpg";
//            imagename[1] = "/images/2_20021023_dk_feyenord_01b_38745.jpg";
//            imagename[2] = "/images/3_korea_iran_02_57710.jpg";
 //           imagename[3] = "/images/4_19980905_ukraine_russia_01_35538.jpg";
 //           imagename[4] = "/images/5_20021218_real_world_01c_11774.jpg";
 //           imagename[5] = "/images/6_fifa03_Can_Usa_02_2_40742.jpg";
//

            imagename[0] = "/images/france_england_02_72194.jpg";
            imagename[1] = "/images/france_england_02_42544.jpg";
            imagename[2] = "/images/milan_chel_01_41554.jpg";
            imagename[3] = "/images/sweden_bulgaria_02b_1481.jpg";
            imagename[4] = "/images/jub_bar_01_41351.jpg";
            imagename[5] = "/images/korea_iran_02_55913.jpg";

            Image [] image = new Image [6];
    //        Image test;
            int i=0;

            try{
  //            test = Image.createImage(112, 68);
    //          Graphics g = test.getGraphics();
              for(i=0; i<6; i++)
              {
                image[i] = Image.createImage(imagename[i]);
                image[i] = createThumbnail(image[i]);
                mainForm.append(image[i]);

              }


          }

        catch (Exception ex) {

}




 //         mainForm.append(new TextField("Lower Item", null, 10, 0));
//		mainForm.append(new Gauge("Personalized    ***Results***    Generalized", true, 50, 25));
            mainForm.addCommand(CMD_EXIT);
            mainForm.addCommand(CMD_PLAY);
            mainForm.setCommandListener(this);
            firstTime = false;
        }
        display.setCurrent(mainForm);
    }

    public void commandAction(Command c, Displayable d) {
        if (c == CMD_EXIT) {
            destroyApp(false);
            notifyDestroyed();
        }
    }

    protected void destroyApp(boolean unconditional) {
    }

    protected void pauseApp() {
    }

    void initialize() {

      firstTime = true;
        mainForm = new Form("MoVR: Retrieval Results");
    }

    private Image createThumbnail(Image image)
    {
      int sourceWidth = image.getWidth();
      int sourceHeight = image.getHeight();
      int thumbWidth = 114;
      int thumbHeight = 85;

      if (thumbHeight == -1)
        thumbHeight = thumbWidth * sourceHeight / sourceWidth;

      Image thumb = Image.createImage(thumbWidth, thumbHeight);
      Graphics g = thumb.getGraphics();

      for (int y = 0; y < thumbHeight; y++)
      {
        for (int x = 0; x < thumbWidth; x++)
        {
          g.setClip(x, y, 1, 1);
          int dx = x * sourceWidth / thumbWidth;
          int dy = y * sourceHeight / thumbHeight;
          g.drawImage(image, x - dx, y - dy, Graphics.LEFT | Graphics.TOP);
        }
      }

   Image immutableThumb = Image.createImage(thumb);

   return immutableThumb;
}

}
