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

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

Оглавление

Назад

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

public class MemoryStream extends Applet
  implements ActionListener
{
  Button btnClear;
  Button btnSave;
  Button btnRestore;
  DrawPanel dp;
  
  // ============================================
  // init
  // ============================================
  public void init()
  {
    btnClear = new Button("Clear");
    btnSave = new Button("Save");
    btnRestore = new Button("Restore");
    
    add(btnClear);
    add(btnSave);
    add(btnRestore);
    
    btnClear.addActionListener(this);
    btnSave.addActionListener(this);
    btnRestore.addActionListener(this);
    
    dp = new DrawPanel();
    add(dp);
  }

  // ============================================
  // actionPerformed
  // ============================================
  public void actionPerformed(ActionEvent e)
  {
    if(e.getSource().equals(btnClear))
    {
      if(dp != null)
      {
        this.remove(dp);
        dp = new DrawPanel();
	add(dp);
	doLayout();
      }  
    }
    
    else if(e.getSource().equals(btnSave))
    {
      dp.save();
    }
      
    else if(e.getSource().equals(btnRestore))
    {
      dp.restore();
    }
      
    repaint();
  }
  
  // ============================================
  // getAppletInfo
  // ============================================
  public String getAppletInfo()
  {
    return "Name: MemoryStream";
  }
}

// =======================================
// Class DrawPanel
// =======================================
class DrawPanel extends Panel
  implements MouseListener, MouseMotionListener
{
  Dimension dmDown = new Dimension(0, 0);
  Dimension dmUp = new Dimension(0, 0);  
  Dimension dmPrev = new Dimension(0, 0);  
  Dimension dimMinSize;
  
  boolean bDrawing = false;
  Vector lines;
  
  static byte[] bSaveBuffer;
  ByteArrayOutputStream baos;
  
  // ============================================
  // DrawPanel
  // ============================================
  public DrawPanel()
  {
    dimMinSize = new Dimension(400, 300);
    
    bDrawing = false;
    lines = new Vector();    
    
    this.addMouseMotionListener(this);
    this.addMouseListener(this);
  }

  // ============================================
  // save
  // ============================================
  public void save()
  {
    baos = new ByteArrayOutputStream();
    ObjectOutputStream oos;
     
    try
    {
      oos = new ObjectOutputStream(baos);
      oos.writeObject(lines);    
      oos.close();
      
      bSaveBuffer = baos.toByteArray();
    }
    catch (IOException ex)
    {
      System.out.println(ex.toString());
    }
  }  
      
  // ============================================
  // restore
  // ============================================
  public void restore()
  {
    ByteArrayInputStream bais;
    bais = new ByteArrayInputStream(bSaveBuffer);
    
    ObjectInputStream ois;
    try
    {
      ois = new ObjectInputStream(bais);
      lines = (Vector)ois.readObject();    
      ois.close();
    }
    catch (IOException ex)
    {
      System.out.println(ex.toString());
    }
    catch (ClassNotFoundException ex)
    {
      System.out.println(ex.toString());
    }
    repaint();
  }  
      
  // ============================================
  // paint
  // ============================================
  public void paint(Graphics g)
  {
    Dimension dimAppWndDimension = getSize();

    setBackground(Color.yellow);
    g.setColor(Color.black);

    for (int i=0; i < lines.size(); i++) 
    {
      Rectangle p = 
        (Rectangle)lines.elementAt(i);
      g.drawLine(p.width, p.height,
        p.x, p.y);
    }
    bDrawing = false;
  }

  // ============================================
  // mousePressed
  // ============================================
  public void mousePressed(MouseEvent e)
  {
    int x = e.getX();
    int y = e.getY();
    
    if(e.getClickCount() > 1)
    {
      lines.removeAllElements();
      repaint();
      return;
    }

    dmDown = new Dimension(x, y);
    dmPrev = new Dimension(x, y);
    bDrawing = false;  }

  // ============================================
  // mouseReleased
  // ============================================
  public void mouseReleased(MouseEvent e)
  {
    int x = e.getX();
    int y = e.getY();
    
    if(bDrawing)    
    {
      dmUp = new Dimension(x, y);
      lines.addElement(
        new Rectangle(dmDown.width,         
	dmDown.height, x, y));
      repaint();      
      bDrawing = false;    
    }  
  }
  
  // ============================================
  // mouseClicked
  // ============================================
  public void mouseClicked(MouseEvent e){}
  
  // ============================================
  // mouseEntered
  // ============================================
  public void mouseEntered(MouseEvent e){}
  
  // ============================================
  // mouseExited
  // ============================================
  public void mouseExited(MouseEvent e){}
  
  // ============================================
  // mouseMoved
  // ============================================
  public void mouseMoved(MouseEvent e)
  {
    bDrawing = false;    
  }
  
  // ============================================
  // mouseDragged
  // ============================================
  public void mouseDragged(MouseEvent e)
  {
    int x = e.getX();
    int y = e.getY();

    Graphics g = getGraphics();
    bDrawing = true;

    g.setColor(Color.yellow);
    g.drawLine(dmDown.width, dmDown.height, 
      dmPrev.width, dmPrev.height);

    g.setColor(Color.black);
    g.drawLine(dmDown.width, 
      dmDown.height, x, y);

    dmPrev = new Dimension(x, y);
  }
  
  // ===========================================
  // getPreferredSize
  // ===========================================
  public Dimension getPreferredSize()
  {
    return dimMinSize;
  }
  
  // ===========================================
  // getMinimumSize
  // ===========================================
  public Dimension getMinimumSize()
  {
    return dimMinSize;
  }
  
  // ===========================================
  // preferredSize
  // ===========================================
  public Dimension preferredSize()
  {
    return dimMinSize;
  }
  
  // ===========================================
  // minimumSize
  // ===========================================
  public Dimension minimumSize()
  {
    return dimMinSize;
  }
}

[Назад]