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

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

Оглавление

Назад

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

public class DrawAndSave
{
  public static void main(String args[])
  {
    FrameWindow frame;
    frame =  new FrameWindow("Draw and save");
    frame.setVisible(true);
  }
}

// =======================================
// Class FrameWindow
// =======================================
class FrameWindow extends Frame
  implements ActionListener, WindowListener, 
  MouseListener, MouseMotionListener
{
  MenuBar mb;
  
  Menu mFile;
  MenuItem miOpen;
  MenuItem miSave;
  MenuItem miExit;
  
  FileDialog fdlg;
  
  Dimension dmDown = new Dimension(0, 0);
  Dimension dmUp = new Dimension(0, 0);  
  Dimension dmPrev = new Dimension(0, 0);  
  
  boolean bDrawing = false;
  Vector lines;
  
  // ============================================
  // FrameWindow
  // ============================================
  public FrameWindow(String szTitle)
  {
    super(szTitle);
    setSize(400, 300);
    
    mb = new MenuBar();
    mFile = new Menu("File");
    
    miOpen = new MenuItem("Open...");
    mFile.add(miOpen);
    
    miSave = new MenuItem("Save As...");
    mFile.add(miSave);
    
    mFile.add("-");
    
    miExit = new MenuItem("Exit");
    mFile.add(miExit);
    
    mb.add(mFile);
    
    miOpen.addActionListener(this);
    miSave.addActionListener(this);
    miExit.addActionListener(this);
    
    setMenuBar(mb);
    
    this.addWindowListener(this);
    
    bDrawing = false;
    lines = new Vector();    
    
    this.addMouseMotionListener(this);
    this.addMouseListener(this);
  }

  // ============================================
  // paint
  // ============================================
  public void paint(Graphics g)
  {
    Dimension dimAppWndDimension = getSize();

    setBackground(Color.yellow);
    g.setColor(Color.black);
    g.drawRect(0, 0, 
      dimAppWndDimension.width  - 1, 
      dimAppWndDimension.height - 1);    

    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;
  }

  // ============================================
  // actionPerformed
  // ============================================
  public void actionPerformed(ActionEvent e)
  {
    if(e.getSource().equals(miOpen))
    {
      fdlg = new FileDialog(this, "Open file", 
	FileDialog.LOAD);
      fdlg.show();
      
      ObjectInputStream ois;
     
      try
      {
        ois = new ObjectInputStream(
          new FileInputStream(
            fdlg.getDirectory() +
            fdlg.getFile()));
	    
	lines = (Vector)ois.readObject();    
	ois.close();
      }
      catch (IOException ex)
      {
        System.out.println(ex.toString());
      }
      catch (ClassNotFoundException ex)
      {
        System.out.println(ex.toString());
      }
      
      repaint();
    }
    
    else if(e.getSource().equals(miSave))
    {
      fdlg = new FileDialog(this,
        "Save drawing as...", 
	FileDialog.SAVE);
      fdlg.show();
      
      ObjectOutputStream oos;
     
      try
      {
        oos = new ObjectOutputStream(
          new FileOutputStream(
            fdlg.getDirectory() +
            fdlg.getFile()));
	    
	oos.writeObject(lines);    
	oos.flush();
	oos.close();
      }
      catch (IOException ex)
      {
        System.out.println(ex.toString());
      }
    }
      
    else if(e.getSource().equals(miExit))
    {
      setVisible(false);
      System.exit(0);
    }
  }
  
  // ============================================
  // windowClosing
  // ============================================
  public void windowClosing(WindowEvent e)
  {
    setVisible(false);
    System.exit(0);
  }
  
  public void windowOpened(WindowEvent e) {}
  public void windowClosed(WindowEvent e) {}
  public void windowIconified(WindowEvent e) {}
  public void windowDeiconified(WindowEvent e) {}
  public void windowActivated(WindowEvent e) {}
  public void windowDeactivated(WindowEvent e) {}
  
  // ============================================
  // 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);
  }
}

[Назад]