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

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

Оглавление

Назад

// ==========================================
// Pipes.java
// (C) Alexandr Frolov, 1998
// E-mail: frolov@glasnet.ru
// Web:    http://www.glasnet.ru/~frolov 
// ==========================================
import java.applet.*;
import java.awt.*;
import java.io.*;

public class Pipes extends Applet
{
  PushThread pushThread = null;
  PopThread popThread = null;
  
  PipedOutputStream pout;
  PipedInputStream pin;

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

  // ============================================
  // init
  // ============================================
  public void init()
  {
    try
    {
      pout = new PipedOutputStream();
      pin = new PipedInputStream(pout);
    }
    catch (IOException ex)
    {
      System.out.println(ex.toString());
    }
  }

  // ============================================
  // destroy
  // ============================================
  public void destroy()
  {
    try
    {
      pout.close();
      pin.close();
    }
    catch (IOException ex)
    {
      System.out.println(ex.toString());
    }
  }
  
  // ============================================
  // start
  // ============================================
  public void start()
  {
    if(pushThread == null)
    {
      pushThread = new PushThread(pout, this);
      pushThread.setPriority(Thread.MIN_PRIORITY);
      pushThread.start();
    }
    
    if(popThread == null)
    {
      popThread = new PopThread(pin, this);
      popThread.start();
    }
  }
	
  // ============================================
  // stop
  // ============================================
  public void stop()
  {
    if(pushThread != null)
    {
      pushThread.stop();
      pushThread = null;
    }
    
    if(popThread != null)
    {
      popThread.stop();
      popThread = null;
    }
  }
}

// ============================================
// Class Sinusoid
// ============================================
class Sinusoid
  implements Serializable
{
  int[] xPoints;
  int[] yPoints;
  Color clr;
  int nPoints;
  
  // ============================================
  // paint
  // ============================================
  public void paint(Graphics g)
  {  
    g.setColor(clr);
    g.fillPolygon(xPoints, yPoints, nPoints);
  }
  
  // ============================================
  // setRandomShape
  // ============================================
  public void setRandomShape(Dimension dm)
  {  
    int rColor, gColor, bColor;
    
    nPoints = dm.width;
    xPoints = new int[nPoints];
    yPoints = new int[nPoints];
    
    int maxAngle = (int)((30 * Math.PI) *
      Math.random());
      
    int Amplitude = (int)(dm.height * 
      Math.random());
	
    int Offset = (int)(dm.height * 
      Math.random());
	
    double deltaFi = (Math.PI / 2) * 
      Math.random();
      
    for(int i=0; i < nPoints; i++)
    {
      xPoints[i] = i;
      yPoints[i] = Offset + (int)(Amplitude * 
	Math.sin(deltaFi + 
        (double)((maxAngle * i)) / nPoints)); 
    }
      
    rColor = (int)(255 * Math.random());
    gColor = (int)(255 * Math.random());
    bColor = (int)(255 * Math.random());
    clr = new Color(rColor, gColor, bColor);
  }  
}

// ============================================
// Class PushThread
// ============================================
class PushThread extends Thread
{
  ObjectOutputStream oos;
  Dimension dm;
  
  // ============================================
  // PushThread
  // ============================================
  public PushThread(PipedOutputStream pout,
    Applet appl)
  {
    dm = appl.getSize();
    
    try
    {
      oos = new ObjectOutputStream(pout);
    }
    catch (IOException ex)
    {
      System.out.println(ex.toString());
    }
  }
  
  // ============================================
  // run
  // ============================================
  public void run()
  {
    int nPoints = dm.width;
    
    while (true)
    {
      Sinusoid s = new Sinusoid();
      s.setRandomShape(dm);      
      
      try
      {
        oos.writeObject(s); 
	oos.flush();   
      }
      catch (IOException ex)
      {
        System.out.println(ex.toString());
      }
      
      try
      {
	Thread.sleep(30);
      }
      catch (InterruptedException e)
      {
	stop();
      }
    }  
  }  
}

// ============================================
// Class PopThread
// ============================================
class PopThread extends Thread
{
  Graphics g;
  ObjectInputStream ois;
  Sinusoid s;  

  // ============================================
  // PopThread
  // ============================================
  public PopThread(PipedInputStream pis,
   Applet Appl)
  {
    g = Appl.getGraphics();
    try
    {
      ois = new ObjectInputStream(pis);
    }
    catch (IOException ex)
    {
      System.out.println(ex.toString());
    }
  }

  // ============================================
  // run
  // ============================================
  public void run()
  {
    while (true)
    {
      try
      {
	s = (Sinusoid)ois.readObject();    
      }
      catch (IOException ex)
      {
        System.out.println(ex.toString());
      }
      catch (ClassNotFoundException ex)
      {
        System.out.println(ex.toString());
      }
    
      s.paint(g);
    }
  }
}

[Назад]