Электронная библиотека книг Александра Фролова и Григория Фролова.
Shop2You.ru Создайте свой интернет-магазин
Библиотека
Братьев
Фроловых
[Назад]

Исходный текст программы CropFilter.java

Оглавление

Назад

// ==========================================
// CropFilter.java
//(C) Alexandr Frolov, 1998
// E-mail: frolov@glasnet.ru
// Web:    http://www.glasnet.ru/~frolov 
// ==========================================

import java.applet.*;
import java.awt.*;
import java.awt.image.*;

public class CropFilter extends Applet 
  implements Runnable
{
  Thread m_CDRotation = null;
  private Image m_Images[];
  private int m_nCurrImage;
  private int m_nImgWidth  = 34;
  private int m_nImgHeight = 34;
  private final int NUM_IMAGES = 11;

  // ===========================================
  // getAppletInfo
  // ===========================================
  public String getAppletInfo()
  {
    return "Name: CropFilter";
  }

  // ===========================================
  // displayImage
  // ===========================================
  private void displayImage(Graphics g)
  {
    g.drawImage(m_Images[m_nCurrImage],
       (size().width - m_nImgWidth)   / 2,
       (size().height - m_nImgHeight) / 2, 
       null);
  }

  // ===========================================
  // paint
  // ===========================================
  public void paint(Graphics g)
  {
    Dimension dimAppWndDimension = size();
    g.setColor(Color.white);
    g.fillRect(0, 0, 
      dimAppWndDimension.width  - 1, 
      dimAppWndDimension.height - 1);
    g.setColor(Color.black);
    g.drawRect(0, 0, 
      dimAppWndDimension.width  - 1, 
      dimAppWndDimension.height - 1);
  }

  // ===========================================
  // start
  // ===========================================
  public void start()
  {
    if (m_CDRotation == null)
    {
      m_CDRotation = new Thread(this);
      m_CDRotation.start();
    }
  }
	
  // ===========================================
  // stop
  // ===========================================
  public void stop()
  {
    if (m_CDRotation != null)
    {
      m_CDRotation.stop();
      m_CDRotation = null;
    }
  }

  // ===========================================
  // run
  // ===========================================
  public void run()
  {
    m_Images = loadImages();
    Graphics m_Graphics = getGraphics();
    m_nCurrImage = 0;
		
    while (true)
    {
      displayImage(m_Graphics);
      m_nCurrImage++;

      if(m_nCurrImage == NUM_IMAGES)
	 m_nCurrImage = 0;

      try
      {
        Thread.sleep(100);
      }
      catch (InterruptedException e)
      {
	stop();
      }
    }
  }
  
  // ===========================================
  // loadImages
  // ===========================================
  Image[] loadImages()
  {
    Image[] im = new Image[NUM_IMAGES];
    Image imgSrc;
    
    imgSrc = getImage(getDocumentBase(), 
      "cdrom.gif");
      
    MediaTracker mt = new MediaTracker(this);
    mt.addImage(imgSrc, 0);
    
    try
    {
      mt.waitForAll();
    }
    catch (InterruptedException e)
    {
      return null;
    }
      
    CropImageFilter cf;
    FilteredImageSource is;
          
    mt = new MediaTracker(this);
    
    for(int i = 0; i < NUM_IMAGES; i++)
    {
      cf = new CropImageFilter(
        34 * i, 0, 34, 34);

      is = new FilteredImageSource(
        imgSrc.getSource(), cf);

      im[i] = createImage(is);
      mt.addImage(im[i], 0);
    }
    
    try
    {
      mt.waitForAll();
    }
    catch (InterruptedException e)
    {
      return null;
    }
    
    return im;
  }
}

[Назад]