// JXOR - XOR drawing with 1.2 and Swing
// Simulated screen capture

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;


public class JXOR extends JFrame
                  implements MouseListener,
                             MouseMotionListener,
                             WindowListener
{
  boolean isFirst = false,
          isDragging = false;

  BufferedImage image = null;

  Graphics2D g2 = null;

  int oldX,     oldY,
      currentX, currentY,
      txtHeight;

  JLabel loc,
         label;
  JPanel jpNorth,
         jpSouth;

  RepaintManager rm = null;


  public JXOR() 
  {
    super( "XOR Drag and Drawing" );
    Container cp = getContentPane();
    addWindowListener( this );
    addMouseListener( this );
    addMouseMotionListener( this );

    loc = new JLabel("0,0");
    jpNorth = new JPanel();
    jpNorth.add( loc );

    label = new JLabel("Drag anywhere.");
    jpSouth = new JPanel();
    jpSouth.add( label );

    cp.add( jpNorth, BorderLayout.NORTH );
    cp.add( jpSouth, BorderLayout.SOUTH );

    setSize(500, 500);
    setVisible(true);

    rm = RepaintManager.currentManager( this );

    // create an image for dragging
    g2 = (Graphics2D)getGraphics();
    if( g2 != null )
    {
      FontMetrics fm = g2.getFontMetrics();
      int iWidth = fm.stringWidth("Hey, Bungalow Bill!");
      txtHeight = fm.getHeight();
      image = (BufferedImage)createImage( iWidth, txtHeight );
      g2.dispose();

      g2 = image.createGraphics();
      if( g2 != null )
      {
        g2.setColor( Color.red );
        g2.drawString( "Hey, Bungalow Bill!", 0, 15 );
        g2.dispose();
        g2 = null;
      }
    }
  } // end constructor


// MouseMotionListener implementation

  public void mouseDragged(MouseEvent e) 
  {
    if( isFirst )
    {
      rm.setDoubleBufferingEnabled(false);
      isDragging = true;
    }

    currentX = e.getX();
    currentY = e.getY() - txtHeight;

    if( g2 == null )
    {
      g2 = (Graphics2D)getGraphics();
      g2.setXORMode( getBackground() );
    }

    if( g2 != null )
    {
      if ( !isFirst ) { g2.drawImage(image, oldX, oldY, this); }

      loc.setText(currentX + "," + currentY);
      Dimension dim = loc.getSize();
      loc.paintImmediately( 0, 0, dim.width, dim.height );
      rm.markCompletelyClean( loc );

      g2.drawImage(image, currentX, currentY, this);
    }

    isFirst = false;
    oldX = currentX;
    oldY = currentY;
  } // end mouseDragged

  public void mouseMoved(MouseEvent e) {}
// end MouseMotionListener implementation  


// MouseMotionListener implementation
  public void mouseClicked(MouseEvent e) {}

  public void mousePressed(MouseEvent e) { isFirst = true; }
    
  public void mouseReleased(MouseEvent e) 
  {
    isFirst = false;
    if( !isDragging ) { return; }

    if( g2 != null )
    {
      g2.drawImage(image, currentX, currentY, this);

      g2.dispose();
      g2 = null;
    }

    isDragging = false;
    rm.setDoubleBufferingEnabled(true);
  } // end mouseReleased

  public void mouseEntered(MouseEvent e) {}
  public void mouseExited(MouseEvent e) {}
// end MouseMotionListener implementation


// WindowListener implementation
    public void windowOpened(WindowEvent e) {}

    public void windowClosing(WindowEvent e)
    {
      dispose();
      System.exit(0);
    }

    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) {}
// end WindowListener implementation


  public static void main(String[] args) 
  {
    JXOR JXORapp = new JXOR();
  }

} // end class JXOR
