All pastes #835678 Raw Edit

Unnamed

public text v1 · immutable
#835678 ·published 2007-12-28 23:57 UTC
rendered paste body

import javax.swing.*;
import java.awt.*;
import java.awt.Image;
import java.util.concurrent.ExecutionException;
import java.io.*;

public class Images extends SwingWorker<Image[], Integer> {
  private final int PER_IMG_COMPLETION_AMT = 6;
  public static final int NUM_IMGS = 16;
  public static final int WATER1 = 0;
  public static final int WATER2 = 1;
  public static final int WATER3 = 2;
  public static final int HIT    = 3;
  public static final int MISS   = 4;
  public static final int CON_TOP = 5;
  public static final int CON_BTM = 6;
  public static final int CON_SPC = 7;
  public static final int DSP_QD1 = 8;
  public static final int DSP_QD2 = 9;
  public static final int DSP_QD3 = 10;
  public static final int DSP_QD4 = 11;
  public static final int DSP_SPCL= 12;
  public static final int DSP_SPCR= 13;
  public static final int DSP_SPCT= 14;
  public static final int DSP_SPCB= 15;
  
  private Image[] images = new Image[NUM_IMGS];
  /*
  |-----|-----|---|
  | QD2 | QD1 |C_T|
  |     |     |   |
  |-----|-----|---|
  | QD3 | QD4 |C_B|
  |     |     |   |
  |-----|-----|---|
  */
  
  public Images() {
    super();
  }
  
  /**
   * Main task
   */
  public Image[] doInBackground() throws IOException {
    Image[] dibImgs = new Image[NUM_IMGS];
    MediaTracker loadTracker = new MediaTracker( null );

    dibImgs[0] = load( "res/water1.PNG" );
    dibImgs[1] = load( "res/water2.PNG" );
    dibImgs[2] = load( "res/water3.PNG" );

    dibImgs[3] = load( "res/hit.PNG" );
    dibImgs[4] = load( "res/miss.PNG" );

    dibImgs[5] = load( "res/conTop.PNG" );
    dibImgs[6] = load( "res/conBtm.PNG" );
    dibImgs[7] = load( "res/conSpacer.PNG" );

    dibImgs[8] = load( "res/dispTopRight.PNG" );
    dibImgs[9] = load( "res/dispTopLeft.PNG" );
    dibImgs[10] = load( "res/dispBtmLeft.PNG" );
    dibImgs[11] = load( "res/dispBtmRight.PNG" );

    dibImgs[12] = load( "res/dispSpacerLeft.PNG" );
    dibImgs[13] = load( "res/dispSpacerRight.PNG" );
    dibImgs[14] = load( "res/dispSpacerTop.PNG" );
    dibImgs[15] = load( "res/dispSpacerBtm.PNG" );
    
    for ( int i = 0; i < NUM_IMGS; i++ ) {
      loadTracker.addImage( dibImgs[i], i );
    }
    
    System.err.println( "Before try");
    try { 
      for ( int i = 0; i < NUM_IMGS; i++ ) {
         
        loadTracker.waitForID( i );
        
        setProgress( ( getProgress() + PER_IMG_COMPLETION_AMT ) );
        publish( getProgress() );
        System.err.println( "In try");
      }
    }catch ( InterruptedException e ) {
       //todo
    }
    System.err.println( "After try" );
                
    while ( loadTracker.statusAll( true ) != MediaTracker.COMPLETE ) {
        loadTracker.statusAll( true );
    }
    
    
    return dibImgs;
  }
  
  /**
   * Executed in dispatching thread
   */ 
  public void done() {
    
    try{
      images = get();
    }catch ( java.util.concurrent.ExecutionException e ) {
        
    }catch ( InterruptedException e ) {
        
    }

  }
	
  private Image load( String path ) throws IOException {
    Toolkit tools;
    File img = new File( path );
    
    if ( !img.exists() ) {
        throw new IOException();
    }
    Image tmpImg = null;
    
    try {
      tools = Toolkit.getDefaultToolkit(); //throws AWTerror
      tmpImg = tools.createImage( getClass().getResource( path ) );
    }catch ( AWTError e ){
        System.err.println( e.getMessage() );
    }
    return tmpImg;
  }

}