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

import java.awt.event.*;

public class Gummiband2 extends JFrame {


  public Gummiband2(String caption) {
    setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
    setTitle(caption);
    this.getContentPane().add( new CCanvas(), BorderLayout.CENTER );
    setLocation(500,50);
  }

 



    public static void main(String args[]) {
      Gummiband2 f = new Gummiband2("2. Beispiel Gummiband");
     	f.setSize(500,400);
    	f.setVisible(true);
    }


}



class CCanvas extends JPanel {

    private int xStart, yStart;
    private int xold, yold;
    private boolean move;

  public CCanvas() {
    this.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {

       // wenn Maus gedrueckt ist
      public void mouseDragged(MouseEvent e) {
        this_mouseDragged(e);
      }

       // wenn Maus nicht gedrueckt ist
      public void mouseMoved(MouseEvent e) {
        // this_mouseMoved(e);
      }
    });
    this.addMouseListener(new java.awt.event.MouseAdapter() {

      public void mouseClicked(MouseEvent e) {
        //this_mouseClicked(e);
      }

      public void mouseEntered(MouseEvent e) {
        //this_mouseEntered(e);
      }

      public void mouseExited(MouseEvent e) {
        //this_mouseExited(e);
      }

      public void mousePressed(MouseEvent e) {
        this_mousePressed(e);
      }

      public void mouseReleased(MouseEvent e) {
        this_mouseReleased(e);
      }
    });
  }

 public void paint (Graphics g)
  {
    int x, y;  // Mittelpunkt
    int w, h;  // Breite Höhe

	g.setColor(Color.white);
     g.fillRect(0,0, getWidth(), getHeight() ); 
     System.out.println("in paint");
      g.setColor(Color.BLUE);
      g.fillRect(10,10,120,55);

      g.setColor(Color.RED);
      g.fillRect(127,200,126,199);
      g.setColor(Color.GREEN);
      g.fillRect(100,150,220,88);
  }

  void this_mousePressed(MouseEvent e) {
    System.out.println("mousePressed: x:" + e.getX()+"  y:" + e.getY());
    move=false;
    xStart=e.getX();
    yStart=e.getY();
  }

  void this_mouseDragged(MouseEvent e) {
    System.out.println("mouseDragged: x:" + e.getX()+"  y:" + e.getY());
    Graphics g = this.getGraphics();
    g.setColor(Color.BLACK);
    g.drawRect(xStart, yStart, e.getX(), e.getY());
    xold=e.getX();
    yold=e.getY();

  }

  void this_mouseReleased(MouseEvent e) {
    System.out.println("mouseReleased: x:" + e.getX()+"  y:" + e.getY());
    move=false;
  }

}

