
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.util.Vector;
import java.lang.*;


public class Game1 extends JFrame {

  double _x;
  double _y;
  JPanel pnIcon;

  //Frame konstruieren
  public Game1() {
    setGUI();
  }


  //Initialisierung der Komponente
  private void setGUI()  {
    this.setSize(new Dimension(500, 300));
    this.setTitle("Game1");

    JButton bnOk = new JButton("Ok");
    JPanel pn = new JPanel();
    pn.add(bnOk);
    pn.setLayout( new FlowLayout() );

    this.getContentPane().add(pn, BorderLayout.SOUTH);

    JPanel pnGame = new JPanel();
    pnGame.setLayout( null );
    pnGame.setBackground(Color.red);

    this.getContentPane().add(pnGame, BorderLayout.CENTER);

    pnIcon = new JPanel();
    pnIcon.setPreferredSize( new Dimension(100,300));
    pnIcon.setBackground(Color.green);
    pnGame.add(pnIcon);
    pnIcon.setBounds(40, 30, 50, 30); // x/y   width height



    pnIcon.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {

      public void mouseDragged(MouseEvent e) {
        pnIcon_mouseDragged(e);
      }

      public void mouseMoved(MouseEvent e) {
        pnIcon_mouseMoved(e);
      }
    });


    pnIcon.addMouseListener(new java.awt.event.MouseAdapter() {

      public void mouseClicked(MouseEvent e) {
        pnIcon_mouseClicked(e);
      }

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

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


  }  // setGUI

    public void pnIcon_mouseDragged(MouseEvent e) {
       double x= e.getPoint().getX();
       double y= e.getPoint().getY();
      System.out.println( "mouseDragged:  x:"+x+"  y:"+y );
    }

    public void pnIcon_mouseMoved(MouseEvent e){
       double x= e.getPoint().getX();
       double y= e.getPoint().getY();
      System.out.println( "mouseMove:  x:"+x+"  y:"+y );
    }


    public void pnIcon_mouseClicked(MouseEvent e) {
       double x= e.getPoint().getX();
       double y= e.getPoint().getY();
      System.out.println( "mouseClicked:  x:"+x+"  y:"+y );
    }

    public void pnIcon_mousePressed(MouseEvent e) {
       double x= e.getPoint().getX();
       double y= e.getPoint().getY();
       _x = x; // relativ
       _y = y; // relativ
      System.out.println( "mousePressed:  x:"+x+"  y:"+y );
    }

    public void pnIcon_mouseReleased(MouseEvent e) {
       double x= e.getPoint().getX();  // relative Position
       double y= e.getPoint().getY();  // relative Position
      System.out.println( "mouseRelease:  _x:"+_x+"  _y:"+_y );
      System.out.println( "mouseRelease:  x:"+x+"  y:"+y );
      int newx, newy;
      int xold, yold;
      xold = (int) pnIcon.getLocation().getX();
      yold = (int) pnIcon.getLocation().getY();
      newx = (int)(xold+ x -_x);
      newy = (int)(yold+ y -_y);
      // setLocation ist besser
      pnIcon.setBounds(newx, newy, 50, 30); // x/y   width height
    }

  //Überschreiben, damit das Programm bei Herunterfahren des Systems beendet werden kann
  protected void processWindowEvent(WindowEvent e) {
    super.processWindowEvent(e);
    if(e.getID() == WindowEvent.WINDOW_CLOSING) {
      System.exit(0);
    }
  }




  public static void main(String[] args) {
    Game1 frame = new Game1();
    frame.setVisible(true);
  }

}
